Problem: N people are arranged in a row from left to right.  You are given a string S of length N consisting of `0` and `1`, and a positive integer K.  The i-th person from the left is standing on feet if the i-th character of S is `0`, and standing on hands if that character is `1`.  You will give the following direction at most K times (possibly zero):  **Direction** : Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.  Find the maximum possible number of **consecutive** people standing on hands after at most K directions.

Submission attempts (in order):
from itertools import accumulate

n,k = list(map(int,input().split(" ")))
s = input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in s:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2 * k + 1

    if len(l) <=w:
        return n

    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[i+w] - acc[i])

    return ans
print(solve())


from itertools import accumulate

n, k = list(map(int,input().split(" ")))
s = input()


def solve():
    cnt=0
    l=[]
    prev = "1"

    for i in s:
        if prev ==i:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev=i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2*k+1

    if len(l)<=w:
        return n

    acc = [0]+list(accumulate(l))
    ans = 0
    for i in range(0, len(l)-w+1,2):
        ans = max(ans,acc[i+w]-acc[i])
    return ans
print(solve())


from itertools import accumulate

N,K=list(map(int,input().split(" ")))
S=input()
def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in S:
        if prev ==i:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")
    w = 2*K+1
    if len(l) <=w:
        return N
    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l)-w+1,2):
        ans = max(ans,acc[w+i]-acc[i])

    return ans
print(solve())

from itertools import accumulate

N,K=list(map(int,input().split(" ")))
S=input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in S:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")
    
    w = 2 * K + 1
    
    if len(l) <=w:
        return N
    
    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[w+i] - acc[i])

    return ans
print(solve())

from itertools import accumulate

n, k = list(map(int,input().split(" ")))
s = input()


def solve():
    l=[]
    prev = '1'
    cnt = 0
    for c in s:
        if prev ==c:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev=c
    l.append(cnt)
    if prev=='0':
        l.append(0)

    w = 2*k+1

    if len(l)<=w:
        return n

    acc = [0]+list(accumulate(l))
    ans = 0
    for i in range(0, len(l)-w+1,2):
        ans = max(ans,acc[i+w]-acc[i])
    return ans

from itertools import accumulate

n,k = list(map(int,input().split(" ")))
s = input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in s:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2 * k + 1

    if len(l) <=w:
        return n

    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[i+w] - acc[i])

    return ans
print(solve())

from itertools import accumulate

n, k = list(map(int, input().split()))
s = input()


def solve():
    l = []
    prev = '1'
    cnt = 0
    for c in s:
        if prev == c:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev = c
    l.append(cnt)
    if prev == '0':
        l.append(0)
    print(l)

    w = 2 * k + 1

    if len(l) <= w:
        return n

    acc = [0] + list(accumulate(l))
    ans = 0
    for i in range(0, len(l) - w + 1, 2):
        ans = max(ans, acc[i + w] - acc[i])
    return ans


print(solve())


from itertools import accumulate

n,k = list(map(int,input().split(" ")))
s = input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in s:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2 * k + 1

    if len(l) <=w:
        return n

    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[i+w] - acc[i])

    return ans
print(solve())


from itertools import accumulate

n, k = list(map(int,input().split(" ")))
s = input()


def solve():
    cnt=0
    l=[]
    prev = "1"

    for i in s:
        if prev ==i:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev=i
    l.append(cnt)
    if prev=="0":
        l.append(0)

    w = 2*k+1

    if len(l)<=w:
        return n

    acc = [0]+list(accumulate(l))
    ans = 0
    for i in range(0, len(l)-w+1,2):
        ans = max(ans,acc[i+w]-acc[i])
    return ans
print(solve())


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Accepted