99032024-03-18 14:56:1342Szitakötő (50 pont)python3Runtime error 38/50310ms133088 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=[-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]
        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/017ms11444 KiB
2Runtime error0/0256ms133088 KiB
3Accepted1/117ms11856 KiB
4Accepted1/118ms12296 KiB
5Accepted1/117ms12052 KiB
6Accepted1/118ms12560 KiB
7Accepted1/118ms12656 KiB
8Accepted1/118ms12776 KiB
9Accepted1/118ms13032 KiB
10Accepted2/217ms12876 KiB
11Accepted2/218ms13156 KiB
12Accepted2/218ms13484 KiB
13Accepted2/219ms13828 KiB
14Accepted2/219ms13788 KiB
15Accepted2/219ms13848 KiB
16Accepted2/218ms13468 KiB
17Accepted2/220ms13564 KiB
18Accepted2/219ms13832 KiB
19Accepted2/219ms13744 KiB
20Accepted2/218ms14152 KiB
21Accepted1/118ms13976 KiB
22Runtime error0/2248ms131332 KiB
23Runtime error0/2238ms131060 KiB
24Runtime error0/2263ms130916 KiB
25Accepted2/2214ms99524 KiB
26Accepted2/281ms36100 KiB
27Runtime error0/2305ms130708 KiB
28Runtime error0/2250ms130676 KiB
29Accepted2/2181ms47904 KiB
30Runtime error0/2310ms130736 KiB
31Accepted2/279ms36304 KiB