99052024-03-18 14:59:4042Szitakötő (50 pont)python3Accepted 50/50287ms39604 KiB
# O(N)

from sys import stdin
input=stdin.readline

mod=10**9+7

def solv():
    N,K = [int(x) for x in input().split()]
    W = [int(x) for x in input().split()]
    if N==1:
        print(2)
        return
    if K==1:
        print(0)
        return
    preW=[0]*N
    for i in range(N):
        preW[i]=preW[i-1]+W[i]
    i=K-2
    while preW[i]*2 > preW[K-1]:
        i-=1
    # 2^(i+1) lehetoseg balra
    if K==N:
        print(pow(2,i+2,mod))
        return
    # K<N:
    d=[-1]*N
    j=K
    while j<N and 2*preW[K-1] > preW[j]:
        j+=1
    if j==N:
        print(pow(2,i+1+N-K,mod))
        return
    if K==j:
        print(0)
        return
    d[K-1]=j
    for k in range(K,N):
        while j<N and 2*preW[k] > preW[j]:
            j+=1
        if j==N:
            break
        if k+1==j:
            print(0)
            return
        d[k]=j
    preW.append(0)
    W[N-1]=preW[N-1]=0
    W[N-2]=preW[N-2]=0
    for k in range(N-2,K-2,-1):
        if d[k] == -1:
            W[k]=pow(2,N-k-1,mod)
            preW[k]=(preW[k+1]+W[k])%mod
        else:
            W[k]=(preW[k+1]-preW[d[k]])%mod
            preW[k]=(preW[k+1]+W[k])%mod
    print((pow(2,i+1,mod)*W[K-1])%mod)
    
solv()
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/017ms11608 KiB
2Accepted0/0245ms37756 KiB
3Accepted1/118ms12276 KiB
4Accepted1/117ms12120 KiB
5Accepted1/118ms12500 KiB
6Accepted1/117ms12504 KiB
7Accepted1/118ms12868 KiB
8Accepted1/117ms13320 KiB
9Accepted1/117ms13504 KiB
10Accepted2/217ms13704 KiB
11Accepted2/217ms13776 KiB
12Accepted2/217ms13776 KiB
13Accepted2/219ms14328 KiB
14Accepted2/217ms14248 KiB
15Accepted2/219ms14600 KiB
16Accepted2/218ms14236 KiB
17Accepted2/219ms14512 KiB
18Accepted2/219ms14680 KiB
19Accepted2/219ms14164 KiB
20Accepted2/219ms14460 KiB
21Accepted1/118ms14212 KiB
22Accepted2/2237ms39076 KiB
23Accepted2/2216ms38220 KiB
24Accepted2/2238ms39604 KiB
25Accepted2/2186ms37120 KiB
26Accepted2/281ms35764 KiB
27Accepted2/2277ms35828 KiB
28Accepted2/2241ms39268 KiB
29Accepted2/2172ms35892 KiB
30Accepted2/2287ms39132 KiB
31Accepted2/283ms35784 KiB