TnT/Multi_CodeNet4Repair huggingface dataset:
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 collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
else:
	a=deque([])
	if s[0]=="1":
    	a.append([0])
	for i in range(1,n):
    	if s[i-1:i+1]=="01":
        	a.append([i])
    	elif s[i-1:i+1]=="10":
        	a[-1].append(i-1)
	if s[n-1]=="1":
    	a[-1].append(n-1)
	if a[0][0]!=0:
    	a.appendleft([0,0])
	if a[-1][1]!=n-1:
    	a.append([n-1,n-1])
	if len(a)-1<=k:
    	print(n)
	else:
    	t=0
    	for i in range(len(a)-k):
        	t=max(t,a[i+k][1]-a[i][0]+1)
    	print(t)

from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
else:
	a=deque([])
	if s[0]=="1":
    	a.append([0])
	for i in range(1,n):
    	if s[i-1:i+1]=="01":
        	a.append([i])
    	elif s[i-1:i+1]=="10":
        	a[-1].append(i-1)
	if s[n-1]=="1":
    	a[-1].append(n-1)
	if a[0][0]!=0:
    	a.appendleft([0,0])
	if a[-1][1]!=n-1:
    	a.append([n-1,n-1])
        
	if len(a)-1<=k:
    	print(n)
	else:
    	t=0
    	for i in range(len(a)-k):
        	t=max(t,a[i+k][1]-a[i][0]+1)
    	print(t)

#D
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
elif "1" not in s:
  	print(len(s))
else:
    a=deque([])
    if s[0]=="1":
        a.append([0])
    for i in range(1,n):
        if s[i-1:i+1]=="01":
            a.append([i])
        elif s[i-1:i+1]=="10":
            a[-1].append(i-1)
    if s[n-1]=="1":
        a[-1].append(n-1)
    if a[0][0]!=0:
        a.appendleft([0,0])
    if a[-1][1]!=n-1:
        a.append([n-1,n-1])
    if len(a)-1<=k:
        print(n)
    else:
        t=0
        for i in range(len(a)-k):
            t=max(t,a[i+k][1]-a[i][0]+1)
        print(t)



Verdicts: Runtime Error

Runtime Error

Accepted
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):
N,K=map(int,input().split())
S=input()
L=[0]
#even:1
#odd:0
tmp="0"
for i in range(N):
    if tmp!=S[i]:
        L.append(1)
    else:
        L[-1]+=1
    tmp=S[i]

if tmp=="0":
    L.append(0)


M=len(L)

if M<=2*K+1:
    print(N)
    exit()


X=[0 for i in range(M+1)]
for i in range(M):
    X[i+1]=L[i]+X[i]


ans=0
for i in range(0,M-2*K,2):
    #print(X[i],X[i+2*K+1])
    ans=max(ans,X[i+2*K+1]-X[i])
print(ans)

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

L = [0]
tmp = '1'



for s in S:
  if s == tmp:
    L[-1] += 1
  else:
    L.append(1)
  tmp = s

if tmp == '0':
  L.append(0)



M = len(L)
if M <= 2*K+1:
  ans = N
  print(ans)
  exit()




X = [0]*(M+1)
for i in range(M):
  X[i+1] = X[i] + L[i]


## answer
ans = 0
for m in range(0,M-2*K,2):
  _ = X[2*K+m+1]-X[m]
  ans = max(ans, _)


print(ans)

Verdicts: Wrong Answer

Accepted
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):
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


N, K = map(int, input().split())

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)











import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


N, K = map(int, input().split())

S = input()

if len(S) == 1:
    print('1')
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate

N, K = map(int, input().split())

S = input()


num = []

if S[0] == '0':
    num.append(0)

if S[-1] == '1':
    S += '0'
else:
    S += '1'    

cnt = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
    else:
        num.append(cnt)
        cnt = 1 
if S[-2] == '0':
    num.append(0)

num = [0] + num
cum_sum = list(accumulate(num))

ans = 0

for i in range(0, len(cum_sum), 2):
    right = i + K * 2 + 1
    if i + K * 2 >= len(cum_sum): 
        right = len(cum_sum) - 1

    ans = max(ans, cum_sum[right] - cum_sum[i])
print(ans)        


Verdicts: Runtime Error

Runtime Error

Accepted
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):
n, k = map(int, input().split())
s = input()

start = [0]
end = []

if s[0] == "0":
    end.append(0)

for i, (c0, c1) in enumerate(zip(s, s[1:])):
    print(c0, c1)
    if c0 == "1" and c1 == "0":
        end.append(i + 1)
    elif c0 == "0" and c1 == "1":
        start.append(i + 1)

if s[-1] == "1":
    end.append(n)
if n > 1 and s[-1] == "0":
    end.append(n)
    start.append(n)

print(start, end)

k = min(k, len(start) - 1)

print(max(en - st for st, en in zip(start, end[k:])))



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

start = [0]
end = []

if s[0] == "0":
    end.append(0)

for i, (c0, c1) in enumerate(zip(s, s[1:])):
    if c0 == "1" and c1 == "0":
        end.append(i + 1)
    elif c0 == "0" and c1 == "1":
        start.append(i + 1)

if s[-1] == "1":
    end.append(n)
if n > 1 and s[-1] == "0":
    end.append(n)
    start.append(n)

k = min(k, len(start) - 1)

print(max(en - st for st, en in zip(start, end[k:])))



Verdicts: Wrong Answer

Accepted
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):
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
s = ns()
ones = []
a = 0
now = '0'

for i in s:
    if i == now:
        a += 1
    else:
        ones.append(a)
        a = 1
        now = i

ones.append(a)

if (len(ones)+1)//2 <= k:
    print(sum(ones))
    exit()

ans = 0
for j in range((len(ones)+1)//2+1-k):
    print(ones[max(0,j*2-1):(j+k)*2])
    ans = max(ans, sum(ones[max(0,j*2-1):(j+k)*2]))
print(ans)

import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
n,k = na()
s = ns()
ones = []
a = 0
now = '0'
 
for i in s:
    if i == now:
        a += 1
    else:
        ones.append(a)
        a = 1
        now = i
 
ones.append(a)

if (len(ones)+1)//2 <= k:
    print(sum(ones))
    exit()

ssu = 0
d = []
for u in ones:
    ssu += u
    d.append(ssu)
d.append(d[-1])
ans = 0
for j in range((len(ones)+1)//2+1-k):
    q = max(0,j*2-1)
    w = 0 if q == 0 else d[q-1]
    ans = max(ans, d[(j+k)*2-1]-w)
 
print(ans)

Verdicts: Wrong Answer

Accepted
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):

_, k = map(int, input().split())
s = input()

a = []
cnt_one = 0
cnt_zero = 0

if s[0] == "0":
    a.append(0)

for i in s:
    if i == "1":
        cnt_one += + 1
        if cnt_zero != 0:
            a.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            a.append(cnt_one)
            cnt_one = 0

a.append(max(cnt_zero, cnt_one))

if s[-1] == "0":
    a.append(0)

n = len(a)
right = 2 * k + 1
up = sum(a[:right])
max_up = up

for i in range((n - right) // 2):
    new_up = up + a[right + i * 2] + a[right + i * 2 + 1] - a[i * 2] - a[i * 2 + 1]
    max_up = max(up, new_up)
    print(new_up)
    up = new_up

print(max_up)


_, k = map(int, input().split())
s = input()

a = []
cnt_one = 0
cnt_zero = 0

if s[0] == "0":
    a.append(0)

for i in s:
    if i == "1":
        cnt_one += + 1
        if cnt_zero != 0:
            a.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            a.append(cnt_one)
            cnt_one = 0

a.append(max(cnt_zero, cnt_one))

if s[-1] == "0":
    a.append(0)

n = len(a)
right = 2 * k + 1
up = sum(a[:right])
max_up = up

for i in range((n - right) // 2):
    new_up = up + a[right + i * 2] + a[right + i * 2 + 1] - a[i * 2] - a[i * 2 + 1]
    max_up = max(max_up, new_up)
    up = new_up

print(max_up)

Verdicts: Wrong Answer

Accepted
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):
def calc_seq(xs):
    seq = []
    count = 1
    for i in range(1, len(xs) - 1):
        if xs[i] == xs[i+1]:
            count += 1
        else:
            seq.append(count)
            count = 1
    seq.append(count)
    return seq

def form_seq(xs):
    seq = calc_seq(xs)
    if xs[0] == 0:
        seq.insert(0, 0)
    if len(seq) % 2 == 0:
        seq.append(0)
    return seq

def main(n, k, xs):
    seq = form_seq(xs)
    m = len(seq)
    i = 1
    if i + 2*k > m:
        print(n)
        return
    val = sum(seq[i-1:i-1+2*k])
    biggest = val
    i += 2
    while i + 2*k <= m:
        val += sum(seq[i-1+2*k:i-1+2*k]) - sum(seq[i-1-2:i-1-1])
        if biggest < val:
            biggest = val
        i += 2
    print(biggest)

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

main(n, k, xs)

def calc_seq(xs):
    seq = []
    count = 1
    for i in range(0, len(xs)-1):
        if xs[i] == xs[i+1]:
            count += 1
        else:
            seq.append(count)
            count = 1
    seq.append(count)
    return seq

def form_seq(xs):
    seq = calc_seq(xs)
    if xs[0] == 0:
        seq.insert(0, 0)
    if len(seq) % 2 == 0:
        seq.append(0)
    return seq

def main(n, k, xs):
    seq = form_seq(xs)
    m = len(seq)
    i = 1
    if i + 2*k > m:
        print(n)
        return
    j = i - 1
    val = sum(seq[j:j+2*k+1])
    biggest = val
    i += 2
    while i + 2*k <= m:
        j = i - 1
        val += seq[j+2*k-1] + seq[j+2*k] - seq[j-2] - seq[j-1]
        if biggest < val:
            biggest = val
        i += 2
    print(biggest)

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

main(n, k, xs)

Verdicts: Wrong Answer

Accepted
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):
N, K = map(int, input().split())
S = input()

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    cands = []
    L = len(arr)//2
    if S[0]=='1':
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K)+1, len(arr))]))
    else:
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K), len(arr))]))
    print(max(cands))

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    if S[0]=='1':
        L = (len(arr)+1)//2 - K +1
        l = 0
        tmp = sum(arr[2*l:2*(l+K)+1])
        ans = tmp
        for l in range(1, L):
            tmp = tmp - (arr[2*(l-1)]+arr[2*l-1]) + sum(arr[2*(l-1+K)+1 : min(2*(l+K)+1, len(arr))])
            ans = max(tmp, ans)
    else:
        L = (len(arr)+1)//2 - K + 1
        l = 0
        tmp = sum(arr[2*l:2*(l+K)])
        ans = tmp
        for l in range(1, L):
            tmp = tmp - sum(arr[max(0,2*l-3) : 2*l-1]) + sum(arr[2*(l+K-1): min(2*(l+K), len(arr))])
            ans = max(tmp, ans)
            
    print(ans)

Verdicts: Wrong Answer

Accepted
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):
'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0

# 	flip+=abs(S[i+1]-S[i])
# print(flip)


now=1
cnt=0
Nums=[]
for i in range(N):
	if S[i]==now:
		cnt+=1
	else:
		Nums.append(cnt)
		now+=1
		cnt=1

if cnt!=0:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0

# 	flip+=abs(S[i+1]-S[i])
# print(flip)


now=1
cnt=0
Nums=[]
for i in range(N):
	if S[i]==now:
		cnt+=1
	else:
		Nums.append(cnt)
		now=1-now
		cnt=1

if cnt!=0:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

Verdicts: Wrong Answer

Accepted
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):
l, accum = len(s), []
i = 0
while i < l:
    ch = s[i]
    count = 0
    while i < l and s[i] == ch:
        count += 1
        i += 1
    accum.append(count)

l = len(accum)
for i in range(1, l): accum[i] += accum[i-1]

mx = accum[min(2*k - (1-int(s[0])), l-1)]
i = 1
while l > 1:
    idx = min(2*k - (1-int(s[0])) + 2*i, l-1)
    mx = max(accum[idx] - accum[2*(i-1)+int(s[0])], mx)
    if idx >= l-1: break
    i += 1                                                                                                     
print(mx)

N, k = map(int, input().split())                                                                               
s = input()
l, accum = len(s), []
zeros, ones = [], []
if s[0] == '0':
    ones = [0]
i = 0
while i < l:
    ch = s[i]
    if ch == '0':
        count = 0
        while i < l and s[i] == ch:
            count += 1
            i += 1
        zeros.append(count)
    elif ch == '1':
        count = 0
        while i < l and s[i] == ch:
            count += 1
            i += 1
        ones.append(count)
mx = sum(zeros[:k]) + sum(ones[:k+1])
ones+=[0]
for i in range(1, len(zeros)): zeros[i] += zeros[i-1]
for i in range(1, len(ones)): ones[i] += ones[i-1]
for i in range(1, len(zeros)-k+1):
    mx = max(zeros[k+i-1] + ones[k+i] - zeros[i-1] - ones[i-1], mx)
print(mx)

Verdicts: Runtime Error

Accepted
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):
def solve(n, k, s):
    v = []
    if s[0] == "0":
        v.append(0)
    i = 0
    while i < n:
        num = 1
        while (i+1 < n) and (s[i+1] == s[i]):
            num += 1
            i += 1
        v.append(num)
        i += 1
    if s[n-1] == "0":
        v.append(0)
    m = len(v)
    w = [0] * m
    w[0] = v[0]
    for i in range(m-1):
        w[i+1] = w[i] + v[i+1]
    best = w[0]
    for i in range(m):
        left = i
        right = min(i + 2*k + 1, m-1)
        cur = w[right] - w[left]
        best = max(best, cur)
    return best

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

def solve(n, k, s):
    v = []
    if s[0] == "0":
        v.append(0)
    i = 0
    while i < n:
        num = 1
        while (i+1 < n) and (s[i+1] == s[i]):
            num += 1
            i += 1
        v.append(num)
        i += 1
    if s[n-1] == "0":
        v.append(0)
    m = len(v)
    w = [0] * m
    w[0] = v[0]
    for i in range(m-1):
        w[i+1] = w[i] + v[i+1]
    best = w[0]
    for left in range(0, m, 2):
        right = min(left + 2*k + 1, m)
        if left == 0:
            cur = w[right-1]
        else:
            cur = w[right-1] - w[left-1]
        best = max(best, cur)
    return best

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

Verdicts: Wrong Answer

Accepted
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):
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+1, len(A)) :
        calc += A[i]
        if i % 2 == 1 :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)
else :
    ans = sum(A[:2*K])
    for i in range(2*K,len(A)) :
        calc += A[i]
        if i == 2*K :
            calc -= A[0]
        elif i % 2 == 0  :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)


N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+1, len(A)) :
        calc += A[i]
        if i % 2 == 1 :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)
else :
    ans = sum(A[:2*K])
    calc = ans
    for i in range(2*K,len(A)) :
        calc += A[i]
        if i == 2*K :
            calc -= A[0]
        elif i % 2 == 0  :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)


Verdicts: Runtime Error

Accepted
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):
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
M = []
a = 0
while a <= len(A)-K*2-1:
    b = a + K*2 + 1
    M.append(A[b]-A[a])
    a += 2
print(max(M))

N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
M = []
if S == "0"*N:
    M.append(len(S))
elif S[0] == "0":
    if K > len(A)//2:
        K = len(A)//2
    M.append(A[K*2]-A[0])
    a = 1
    b = a + K*2 + 1
elif S[0] == "1":
    if K > (len(A)-1)//2:
        K = (len(A)-1)//2
    a = 0
    b = a + K*2 + 1
if S == "0"*N:
    pass
elif S[-1] == "1":
    while b <= len(A):
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
elif S[-1] == "0":
    while b <= len(A)-1:
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
    M.append(A[-1]-A[-1-K*2])
print(max(M))

Verdicts: Runtime Error

Accepted
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):
N, K = map(int, input().split())


S = input()
index_array = [0]
now_letter = "1"
for i, s in enumerate(S):
    if s != now_letter:
        now_letter = s
        index_array.append(i)
else:
    if now_letter == "1":
        index_array.append(N)
# print(index_array)
# print(index_array)
ans = 0
array_len = len(index_array)
if (K + 1) * 2 >= array_len:
    print(N)
    exit()
candidate = array_len // 2 - K
for i in range(candidate):
    
    ans = max(index_array[(i + K) * 2 + 1] - index_array[i * 2], ans)
print(int(ans))


N, K = map(int, input().split())


S = input()
index_array = [0]
now_letter = "1"
for i, s in enumerate(S):
    if s != now_letter:
        now_letter = s
        index_array.append(i)
else:
    if now_letter == "1":
        index_array.append(N)
    else:
        index_array.append(N)
        index_array.append(N)        

# print(index_array)
ans = 0
array_len = len(index_array)
if (K + 1) * 2 > array_len:
    print(N)
    exit()
candidate = array_len // 2 - K
for i in range(candidate):
    
    ans = max(index_array[(i + K) * 2 + 1] - index_array[i * 2], ans)
print(int(ans))


Verdicts: Wrong Answer

Accepted
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):
def main():
  n, k = map(int,input().split())
  s = input()

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
  num += [0]
  tmp += [0]

  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]
  print(wa, num, tmp)
  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

def main():
  from itertools import groupby

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

  num = []
  cot = []
  for k, n in groupby(S):
    num.append(k)
    cot.append(len(list(n)))

  if num[-1] == "0":
    cot += [0]

  X = [0]*(len(cot)+1)
  for i in range(len(cot)):
    X[i+1] = X[i]+cot[i]

  ans = 0
  for i in range(len(X)):
    x = K*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, X[i+x]-X[i])
    except:
      break
  if ans == 0:
    ans = len(S)
  print(ans)

if __name__ == '__main__':
  main()

def main():
  n, k = map(int,input().split())
  s = input()

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
    num += [0]
    tmp += [0]

  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

def main():
  n, k = map(int,input().split())
  s = input()

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
  num += [0]
  tmp += [0]

  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted
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):
N, K = tuple(map(int, input().split()))
S = "1"+input()+"1"

prev = S[0]
if prev == "0":
  fst_is_0 = True
else:
  fst_is_0 = False

cnt = 0
cnt_0 = 0

cnt_lst = []
for idx, s in enumerate(S):
  if s == prev:
    cnt += 1
  else:
    cnt_lst.append(cnt)
    if s == "0":
      cnt_0 += 1
    prev = s
else:
  cnt_lst.append(cnt)
#print(cnt_lst)


if cnt_0 <= K:
  print(len(S)-2)
else:
  l = [cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2] for idx in range( int( (len(cnt_lst)+1)/2) -K)   ]
  #print(l)
  ss = []
  for idx,sss in enumerate(l):
    if idx == 0:
      ss.append(sss-1)
    elif idx == len(l)-1:
      
      ss.append(sss-1)
    else:
      ss.append(sss)
      
  print(max(ss))

N, K = tuple(map(int, input().split()))
S = "1"+input()+"1"

prev = S[0]
if prev == "0":
  fst_is_0 = True
else:
  fst_is_0 = False

cnt = 0
cnt_0 = 0

cnt_lst = []
for idx, s in enumerate(S):
  if s == prev:
    cnt += 1
  else:
    cnt_lst.append(cnt)
    cnt = 1
    if s == "0":
      cnt_0 += 1
    prev = s
else:
  cnt_lst.append(cnt)
#print(cnt_lst)


if cnt_0 <= K:
  print(len(S)-2)
else:
  prev = "1"
  cnt_lst = [0]
  for idx,s in enumerate(S):
    if prev != s:
      cnt_lst.append(idx)
    prev=s
  cnt_lst.append(len(S))
  s_ =[]
  for idx in range( int( (len(cnt_lst)+1)/2) -K):
    #print(2*idx+2*K,idx*2,cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2])
    if idx==0 or idx==int( (len(cnt_lst)+1)/2) -K-1:
      s_.append(cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2]-1)
    else:
      s_.append(cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2])
  print(max(s_))

Verdicts: Runtime Error

Accepted
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):
n, k = map(int, input().split())
s = input()
l = [0]
for i in range(n - 1):
    if s[i] != s[i + 1]:
        l.append(i + 1)
l += [n]
le = len(l)
ans = 0
for i in range(le - 1):
    if s[l[i]] == "0":
        ans = max(ans, l[min(le, k * 2 + i)] - l[i])
    else:
        ans = max(ans, l[min(le, k * 2 + i + 1)] - l[i])
print(ans)

n, k = map(int, input().split())
s = input()
l = [0]
for i in range(n - 1):
    if s[i] != s[i + 1]:
        l.append(i + 1)
l += [n]
le = len(l)
ans = 0
for i in range(le - 1):
    if s[l[i]] == "0":
        ans = max(ans, l[min(le - 1, k * 2 + i)] - l[i])
    else:
        ans = max(ans, l[min(le - 1, k * 2 + i + 1)] - l[i])
print(ans)

Verdicts: Runtime Error

Accepted
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):
n,k=map(int,input().split())
s=list(input())
l,tmpl,cnt0,tmp,flag=[],0,0,s[0],0
if tmp=="0":cnt0+=1;flag=1
for i in s:
    if i==tmp:tmpl+=1
    else:
        if i=="0":cnt0+=1
        tmp=i
        l.append(tmpl)
        tmpl=1
l.append(tmpl)
if cnt0<=k:print(n);exit()
ans=max(sum(l[:2*k]),sum(l[::-1][:2*k]))
suml,tmp=[0],0
if flag==1:l=l[1:]
for i in l:
    tmp+=i
    suml.append(tmp)
for i in range((len(suml)-2*k-1)//2+1):
    tmp=suml[2*k+2*i+1]-suml[2*i]
    print(tmp)
    if tmp>ans:ans=tmp
print(ans)

n,k=map(int,input().split())
s=list(input())
l,tmpl,cnt0,tmp,flag=[],0,0,s[0],0
if tmp=="0":cnt0+=1;flag=1
for i in s:
    if i==tmp:tmpl+=1
    else:
        if i=="0":cnt0+=1
        tmp=i
        l.append(tmpl)
        tmpl=1
l.append(tmpl)
if cnt0<=k:print(n);exit()
ans=max(sum(l[:2*k]),sum(l[::-1][:2*k]))
suml,tmp=[0],0
if flag==1:l=l[1:]
for i in l:
    tmp+=i
    suml.append(tmp)
for i in range((len(suml)-2*k-1)//2+1):
    tmp=suml[2*k+2*i+1]-suml[2*i]
    if tmp>ans:ans=tmp
print(ans)

n,k=map(int,input().split())
s=list(input())
l,tmpl,cnt0,tmp,flag1,flag2=[],0,0,s[0],0,0
if tmp=="0":cnt0+=1;flag1=1
for i in s:
    if i==tmp:tmpl+=1
    else:
        if i=="0":cnt0+=1
        tmp=i
        l.append(tmpl)
        tmpl=1
if tmp=="0":flag2=1
l.append(tmpl)
if cnt0<=k:print(n);exit()
ans=max(sum(l[:2*k]),sum(l[::-1][:2*k]))
suml,tmp=[0],0
if flag1==1:l=l[1:]
if flag2==1:l=l[:len(l)-1]
for i in l:
    tmp+=i
    suml.append(tmp)
for i in range((len(suml)-2*k-1)//2+1):
    tmp=suml[2*k+2*i+1]-suml[2*i]
    if tmp>ans:ans=tmp
print(ans)

Verdicts: Runtime Error

Runtime Error

Accepted
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):
n,k = map(int,input().split())
s = input()

a = [0]
i = 0
p = "1"
for t in s:
  if t == p:
    a[i] += 1
  else:
    p = t
    i += 1
    a += [1]
# print(a)

if len(a) <= 2*k+1:
  print(n)
  quit()

x = sum(a[:2*k+1])
m = x 
for i in range(2*k+1,len(a)):
  # print(i)
  x = x + a[i] - a[i-(2*k+1)]
  m = max(m,x)
print(m)


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

a = [0]
i = 0
p = "1"
for t in s:
  if t == p:
    a[i] += 1
  else:
    p = t
    i += 1
    a += [1]
a += [0]
# print(a)

if len(a) <= 2*k+1:
  print(sum(a))
  quit()

x = sum(a[:2*k+1])
m = x 
for i in range(2*k+1,len(a)):
  # print(i, a[i] , a[i-(2*k+1)])
  x = x + a[i] - a[i-(2*k+1)]
  if i%2 == 0:
    m = max(m,x)
print(m)

Verdicts: Wrong Answer

Accepted
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):
# coding: utf-8
# Your code here!
N, K = [int(_) for _ in input().split()]
S = str(input())

Nums = []
now, cnt = 1, 0
for i in range(N):
    if S[i] == str(now): cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0: Nums.append(cnt)
if len(Nums) % 2 == 0: Nums.append(0)


# add = 2 * K + 1
# ans = 0

#     tmp = 0
#     left = i

#     j = left
#     while j < right:
#         tmp += Nums[j]
#         j += 1
#     ans = max(tmp, ans)
# print(ans)

#------------------------
add = 2 * K + 1
ans = 0
left = 0
right = 0
tmp = 0
for i in range(len(Nums)):
    nextLeft = i 
    nextRight = min(i + add, len(Nums))
    while nextLeft > left:
        tmp -= Nums[left]
        left += 1
    while nextRight > right:
        tmp += Nums[right]
        right += 1
    ans = max(ans, tmp)
        
print(ans)


# coding: utf-8
# Your code here!
N, K = [int(_) for _ in input().split()]
S = str(input())

Nums = []
now, cnt = 1, 0
for i in range(N):
    if S[i] == str(now): cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0: Nums.append(cnt)
if len(Nums) % 2 == 0: Nums.append(0)


# add = 2 * K + 1
# ans = 0

#     tmp = 0
#     left = i

#     j = left
#     while j < right:
#         tmp += Nums[j]
#         j += 1
#     ans = max(tmp, ans)
# print(ans)

#------------------------
# add = 2 * K + 1
# ans = 0
# left = 0
# right = 0
# tmp = 0



#     while nextLeft > left:
#         tmp -= Nums[left]
#         left += 1
#     while nextRight > right:
#         tmp += Nums[right]
#         right += 1

#     ans = max(tmp, ans)
        
# print(ans)
#------------------------
#print(Nums)
add = 2 * K + 1
sums = [0 for _ in range(len(Nums) + 1)]
for i in range(len(Nums)):
    sums[i+1] = sums[i] + Nums[i]
#print(sums)

ans = 0
for i in range(0, len(Nums), 2):
    left = i
    right = min(i+add, len(Nums))
    tmp = sums[right] - sums[left]
    ans = max(ans, tmp)
print(ans)

Verdicts: Wrong Answer

Accepted
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):
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
input = sys.stdin.readline




def main():
	N, K = map(int, input().split(" "))
	SS = input()
	SS_org = SS.rstrip("\n")
	S = SS.rstrip("\n")
	
	S_tmp = S.split("1")

	S2_tmp = S.split("0")

	if len(S_tmp) <= K:
		print(N)
		sys.exit()
	
	ans_tmp = 0
	for i in range(K):
		ans_tmp += len(S_tmp[i])

	print(S_tmp)
	print(S2_tmp)

	
	
if __name__ == "__main__":
	main()
	

# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left

def main():
	N, K = map(int, input().split(" "))
	S = input()
		
	S0 = S.split("0")
	S1 = S.split("1")
	
	S0 = [x for x in S0 if x]
	S1 = [x for x in S1 if x]
	
	if K == 0:
		ans = 0
		for val in S0:
			tmp = len(val)
			if tmp > ans:
				ans = tmp
		print(ans)
		sys.exit()
		
	if K >= len(S1):
		print(len(S))
		sys.exit()
	
	fst = []
	snd = []
	guu = False
	kii = False
	if S[0] == "1":
		fst = S0
		snd = S1
		kii = True
	else:
		fst = S1
		snd = S0
		guu = True
	
	ansList = []
	while True:
		if len(fst) != 0:
			ansList.append(fst.pop(0))
		if len(snd) != 0:
			ansList.append(snd.pop(0))
		if len(fst) == 0 and len(snd) == 0:
			break
			
	total = []
	for i, val in enumerate(ansList):
		if i == 0:
			total.append(len(val))
			continue
		total.append(total[i-1] + len(val))
	
	ans = 0
	if kii == True:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * K]
			elif i % 2 == 1:
				tmptmp = i+1+(K-1)*2
				if tmptmp >= len(total):
					tmptmp = len(total)-1
				tmp = total[tmptmp] - total[i-1]
			else:
				tmptmp = i+K*2
				if tmptmp >= len(total):
					tmptmp = len(total)-1
				tmp = total[tmptmp] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	else:
		for i, val in enumerate(total):
			tmp = 0
			if i == 0:
				tmp = total[2 * (K-1) + 1]
			elif i % 2 == 1:
				tmptmp = i + K*2
				if tmptmp >= len(total):
					tmptmp = len(total)-1
				tmp = total[tmptmp] - total[i-1]
			else:
				tmptmp = i+1+(K-1)*2
				if tmptmp >= len(total):
					tmptmp = len(total)-1
				tmp = total[tmptmp] - total[i-1]
			
			if ans < tmp:
				ans = tmp
	
	print(ans)
		
	
if __name__ == "__main__":
	main()


Verdicts: Wrong Answer

Accepted