99012024-03-18 14:52:2842Szitakötő (50 pont)python3Runtime error 38/50310ms133032 KiB
# O(NlogN)

from sys import stdin, stdout
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={}
    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 k not in d:
            W[k]=pow(2,N-k-1,mod)
            preW[k]=preW[k+1]+W[k]
        else:
            W[k]=preW[k+1]-preW[d[k]]
            preW[k]=preW[k+1]+W[k]
    print((pow(2,i+1,mod)*W[K-1])%mod)
    
solv()
SubtaskSumTestVerdictTimeMemory
base38/50
1Accepted0/017ms11548 KiB
2Runtime error0/0256ms133032 KiB
3Accepted1/117ms12048 KiB
4Accepted1/117ms12492 KiB
5Accepted1/117ms12596 KiB
6Accepted1/118ms12620 KiB
7Accepted1/117ms13004 KiB
8Accepted1/117ms13176 KiB
9Accepted1/118ms13012 KiB
10Accepted2/218ms12980 KiB
11Accepted2/217ms13308 KiB
12Accepted2/217ms13012 KiB
13Accepted2/219ms13556 KiB
14Accepted2/218ms13408 KiB
15Accepted2/219ms13944 KiB
16Accepted2/218ms13884 KiB
17Accepted2/219ms14032 KiB
18Accepted2/218ms13900 KiB
19Accepted2/219ms13976 KiB
20Accepted2/218ms13992 KiB
21Accepted1/117ms14000 KiB
22Runtime error0/2261ms131092 KiB
23Runtime error0/2252ms130984 KiB
24Runtime error0/2254ms130904 KiB
25Accepted2/2229ms100732 KiB
26Accepted2/282ms36100 KiB
27Runtime error0/2305ms130720 KiB
28Runtime error0/2252ms130896 KiB
29Accepted2/2181ms47340 KiB
30Runtime error0/2310ms130744 KiB
31Accepted2/279ms35988 KiB