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):
# coding: utf-8
# Your code here!

import time

t = time.time()
n,k = map(int,input().split())

s = input()

s = "10"*10**5

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

ans = 0

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

# coding: utf-8
# Your code here!

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

s = input()

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
        
    tmp+=1
h.append(tmp)
h.append(0)
h.append(0)

ans = 0

for i in range(0,len(h)-k-1,2):
    ans = max(ans,sum(h[i:i+k+3]))


for i in range(1,len(h)-k-1,2):
    ans = max(ans,sum(h[i:i+k+1]))

print(ans)

# coding: utf-8
# Your code here!

import numpy as np

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

s = input()

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

h = np.array(h)

ans = 0

for i in range(0,len(h),2):
    ans = max(ans,np.sum(h[i:i+k*2+1]))
    
print(ans)

Verdicts: Wrong Answer

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()
Nums = [0]
now = 1 
cnt = 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

for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i + Add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, ans)

print(ans)

N, K = map(int, input().split())
S = input()
Nums = [0]
now = 1 
cnt = 0 
for i in range(N):
    if s == 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

for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i + Add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, ans)

print(ans)

N, K = map(int, input().split())
S = input()
Nums = []
now = 1 
cnt = 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

# 0 1 2 3 4 5 6
#  0 1 2 3 4 5
sum = [0] * (len(Nums) + 1)
for i in range(len(Nums)):
    sum[i + 1] = sum[i] + Nums[i]
ans = 0

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

print(ans)

Verdicts: Wrong Answer

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 = int(input())
S = input()
n = list(map(int, str(S)))
count = 1
mas = []
for i in range(N-1):
    if n[i] == n[i + 1]:
        count += 1
    else:
        mas.append(count)
        count = 1
        if i == N-2:
            mas.append(1)
        
l = len(mas)
mas2 = []
for m in K:
    

    if l%2==0:
            for k in range(l//2):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k+2])
        else:
            for k in range(l/2-1):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k+2])
            mas.append(mas[l-2] + mas[l-1])
    
    else:
        else:
            mas2.append(mas[0] + mas[1])
            for k in range(1, l//2):
                mas2.append(mas[2*k-1] + mas[2*k] + mas[2*k+1])
        else:
            mas2.append(mas[0] + mas[1])
            for k in range(1, l//2-1):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k-1])
            mas.append(mas[l-2] + mas[l-1])

            
            
print(max(mas2))

from itertools import accumulate

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

S = input()
n = list(map(int, str(S)))
count = 1
mas = []
for i in range(N-1):
    if n[i] == n[i + 1]:
        count += 1
        if i == N-2:
            mas.append(count)
    else:
        mas.append(count)
        count = 1
        if i == N-2:
            mas.append(1)
        

mas2 = []
if n[0] == 0:
    mas.insert(0, 0)
    
if n[N-1] == 0:
    mas.append(0)
    
l=len(mas)
mas2=[0] + list(accumulate(mas))

#    mas2.append(sum(mas[:i]))
    
if K >= (l-1)/2:
    print(N)
else:
    mas3 = []
    k = int(((l-1)/2)-K+1)
    for i in range(k):
        mas3.append(mas2[K*2 + 2*i+1]-mas2[0 + 2*i])
    print(max(mas3))
        

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):
from collections import deque

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

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


if len(cnt_01) <= 2*k:
    print(n)
    exit()

first_que = cnt_01[0:2*k]
que = deque(first_que)

curr_sum = sum(first_que)
ans = curr_sum
for i in range(2*k, len(cnt_01)):
    poped = que.popleft()
    curr_sum -= poped
    que.append(cnt_01[i])
    curr_sum += cnt_01[i]
    ans = max(curr_sum, ans)

print(ans)

from collections import deque

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

cnt_01 = []
prev = s[0]
cnt = 0
for c in s:
    if c != prev:
        cnt_01.append(cnt)
        cnt = 1
    else: 
        cnt += 1
    prev = c
else:
    cnt_01.append(cnt)


if len(cnt_01) <= 2*k:
    print(n)
    exit()


if s[0] == '0':
    ans_candi = sum(cnt_01[0:2*k])
    cnt_01 = cnt_01[1:]
else:
    ans_candi = 0


first_que = cnt_01[0:2*k+1]
que = deque(first_que)
curr_sum = sum(first_que)
ans = curr_sum
for i in range(k, len(cnt_01)-1):
    break_flag = False
    poped1 = que.popleft()
    poped2 = que.popleft()
    curr_sum -= (poped1 + poped2)
    if 2*i+1 < len(cnt_01):
        que.append(cnt_01[2*i+1])
        curr_sum += cnt_01[2*i+1]
    if 2*i+2 < len(cnt_01):
        que.append(cnt_01[2*i+2])
        curr_sum += cnt_01[2*i+2]
    else:
        break_flag = True
    ans = max(curr_sum, ans)
    if break_flag: break

print(max(ans,ans_candi))


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 = list(input())
S.append('X')
list_1 = []
list_0 = []
List = []
start = 0
ans = 0
for i in range(1,N+1):
    if (S[i-1] != S[i]):
        if S[i-1] == '0':
            list_0.append(i-start)
        else:
            list_1.append(i-start)
        start = i


for i in range(min(len(list_0),len(list_1))):
    List.append(list_1[i])
    List.append(list_0[i])
    

            
if len(list_1)>min(len(list_0),len(list_1)):
    List.append(list_1[-1])
else:
    List.append(list_0[-1])

Ruisekiwa = [0]
for i in range(len(List)):
    Ruisekiwa.append(sum(List[:i+1]))

add = 2*K+1
for i in range(0,len(List),2):
    left = i
    right = min(i+add,len(List))
    tmp = Ruisekiwa[right] - Ruisekiwa[left]

    ans = max(tmp,ans)

print(List)
print(ans)


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

List = []
ans = 0
now,count = 1,0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        List.append(count)
        now = 1-now
        count = 1
if count != 0:
    List.append(count)

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

Ruisekiwa = [0]*(len(List)+1)
for i in range(len(List)):
    Ruisekiwa[i+1] = Ruisekiwa[i]+List[i]

add = 2*K+1
for i in range(0,len(List),2):
    left = i
    right = min(i+add,len(List))
    tmp = Ruisekiwa[right] - Ruisekiwa[left]

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

S += '2'
l = []
cnt = 1
for i in range(N):
  if S[i] == S[i+1]:
  	cnt += 1
  else:
    l.append(cnt)
    cnt = 1

if S[0] == '0':
  cur = sum(l[:2*K])
  ans = cur
  for j in range(1, len(l), 2):
    cur += -sum(l[j-2: j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
else:
  cur = sum(l[:2*K+1])
  ans = cur
  for j in range(2, len(l), 2):
    cur += -sum(l[j-2: j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
print(ans)

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

S += '2'
l = []
cnt = 1
for i in range(N):
  if S[i] == S[i+1]:
  	cnt += 1
  else:
    l.append(cnt)
    cnt = 1

if S[0] == '0':
  cur = sum(l[:2*K])
  ans = cur
  for j in range(1, len(l), 2):
    cur += -sum(l[max(0, j-2): j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
else:
  cur = sum(l[:2*K+1])
  ans = cur
  for j in range(2, len(l), 2):
    cur += -sum(l[max(0, j-2): j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
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 numpy as np
N,K = map(int,input().split())
S = input()
stands = []
temp = ''
first_handstand = False
for i,s in enumerate(S):
    if i == 0:
        temp += s
        continue
    if temp[-1] == s:
        temp += s
    else:
        stands.append(len(temp))
        temp = s
stands.append(len(temp)) 
if S[0] == '0':
    stands.insert(0,0)
if S[-1] == '0':
    stands.append(0)
print(stands)


cumsum_stands = np.cumsum(np.array(stands))
cumsum_stands = np.insert(cumsum_stands,0,0)

ans = 0

for l in range(0,len(cumsum_stands),2):
    r = min(l + 2*K+1,len(cumsum_stands)-1)
    ans = max(ans, cumsum_stands[r] - cumsum_stands[l])
print(ans)








import numpy as np
N,K = map(int,input().split())
S = input()
stands = []
temp = ''
first_handstand = False
for i,s in enumerate(S):
    if i == 0:
        temp += s
        continue
    if temp[-1] == s:
        temp += s
    else:
        stands.append(len(temp))
        temp = s
stands.append(len(temp)) 
if S[0] == '0':
    stands.insert(0,0)
if S[-1] == '0':
    stands.append(0)
cumsum_stands = np.cumsum(np.array(stands))
cumsum_stands = np.insert(cumsum_stands,0,0)

ans = 0

for l in range(0,len(cumsum_stands),2):
    r = min(l + 2*K+1,len(cumsum_stands)-1)
    ans = max(ans, cumsum_stands[r] - cumsum_stands[l])
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):
n, k = [int(i) for i in input().split()]
s = [1] + [int(i) for i in list(input())] + [1,0]
one2zero = [i for i in range(n+2) if s[i] == 1 and s[i+1] == 0]
print(one2zero)
ans = 1
for i in range(len(one2zero)-k):
  if one2zero[i + k] - one2zero[i] > ans:
    ans = one2zero[i + k] - one2zero[i]
print(ans)

n, k = [int(i) for i in input().split()]
s = [0,1] + [int(i) for i in list(input())] + [1,0]
down = [i for i in range(n+3) if s[i] == 1 and s[i+1] == 0]
up = [i for i in range(n+3) if s[i] == 0 and s[i+1] == 1]
#print(s)
#print(down)
#print(up)
if k >= len(up):
  ans = n
else:
  if down[k] - up[0] - 1 > ans:
    ans = down[k] - up[0] - 1
  for i in range(1,len(up)-k-1):
    if down[i + k] - up[i] > ans:
      ans = down[i + k] - up[i]
  if down[len(up)-1] - up[len(up)-k -1] - 1 > ans:
    ans = down[len(up)-1] - up[len(up)-k -1] - 1
print(ans)

n, k = [int(i) for i in input().split()]
s = [0,1] + [int(i) for i in list(input())] + [1,0]
down = [i for i in range(n+3) if s[i] == 1 and s[i+1] == 0]
up = [i for i in range(n+3) if s[i] == 0 and s[i+1] == 1]
#print(s)
#print(down)
#print(up)
ans = 1
if k >= len(up):
  ans = n
else:
  if down[k] - up[0] - 1 > ans:
    ans = down[k] - up[0] - 1
  for i in range(1,len(up)-k-1):
    if down[i + k] - up[i] > ans:
      ans = down[i + k] - up[i]
  if down[len(up)-1] - up[len(up)-k -1] - 1 > ans:
    ans = down[len(up)-1] - up[len(up)-k -1] - 1
print(ans)

Verdicts: Wrong Answer

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())
t = list(str(input()))
t = [int(s) for s in t]
last = t[0]
temp_c = 0
s = []
con_c = 0
temp_con_c = 0
corrent_p = 0
sum_t = []
for i in range(n):
    if last == t[i]:
        temp_c += 1
    else:
        last = t[i]
        s.append(temp_c)
        temp_c = 1
s.append(temp_c)

l = len(s)
sum_t.append(0)
for i in range(l):
    sum_t.append(sum_t[i]+s[i])
    
ans = 0
Add = 2*k+1
for i in range(len(sum_t)):
    if i%2==0:
        left = i
        right = min(i+Add,len(sum_t)-1)
        tmp = sum_t[right]-sum_t[left]
        ans = max(tmp,ans)
    
print(ans)

'''
WA
'''
n,k = map(int,input().split()) 
t = list(str(input()))
t = [int(s) for s in t]
last = t[0]
temp_c = 0
s = []
con_c = 0
temp_con_c = 0
corrent_p = 0
sum_t = []
for i in range(n):
    if last == t[i]:
        temp_c += 1
    else:
        last = t[i]
        s.append(temp_c)
        temp_c = 1
s.append(temp_c)
if t[n-1] == 0:
    s.append(0)
l = len(s)
if k*2 <= l:
    sum_t.append(0)
    for i in range(l):
        sum_t.append(sum_t[i]+s[i])
    l = len(sum_t)
    if t[0] == 0:
        for i in range(l-k*2-1):
            if i%2 == 0:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
    else:
        for i in range(l-k*2-1):
            if i%2 ==0:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
else:
    con_c = n

print(con_c)

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] = input().split(" ")
s = input()

cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        for j in int range(0,zero_num-k+1): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        for j in range(0,zero_num-k+1): #j is 0's order
            sum_0 = sum_0 - h[j*2-1] - h[j*2] + h[(j+k)*2] + h[(j+k)*2+1]
            if(ans<=sum_0):
                ans = sum_0

printf(ans)

n,k = map(int, input().split())
s = input()
cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        h = h +[0,0,0]
        for j in range(0,zero_num-k): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        h = [0] + h +[0,0,0]
        for j in range(0,zero_num-k):#j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

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,m=map(int,input().split())
l1=list(input())
l=[]
lm=[]
c=0
d=l1[0]
for i in l1:
  if i==d:
    c+=1
  else:
    d=i
    l.append(c)
    c=1
l.append(c)
if l1[0]=="1":
  if len(l)//2<=m:
    print(n)
  else:
    j=0
    for i in range((len(l)-2*m-1)//2+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))
else:
  if (len(l)+1)//2<=m:
    print(n)
  else:
    j=1
    for i in range((len(l)-2*m-2)//2+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))

n,k=map(int,input().split())
w=input()
l=[]
c=0
w="1"+w
for i in range(1,1+n):
  if w[i]!=w[i-1]:
    l.append(c)
    c=1
  else:
    c+=1
l.append(c)
if w[-1]=="0":
  l.append(0)
s=l[0]
for i in range(1,1+k):
  s+=l[2*i]+l[2*i-1]
s1=s
c1=l[0]+l[1]
for i in range(1,(len(l)+1)/2):
  s1=s1-l[2*i-2]-l[2*i-1]+l[2*i-2+2*k+1]+l[2*i+2*k]
  if s1>s:
    s=s1
print(s)
  



n,m=map(int,input().split())
l1=list(input())
l=[]
lm=[]
c=0
d=l1[0]
for i in l1:
  if i==d:
    c+=1
  else:
    d=i
    l.append(c)
    c=1
l.append(c)
if l1[0]=="1":
  if len(l)//2<=m:
    print(n)
  else:
    j=0
    for i in range(m+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))
else:
  if (len(l)+1)//2<=m:
    print(n)
  else:
    j=1
    for i in range(m):
      lm.append(sum(l[j:j+2*m]))
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))

n,k=map(int,input().split())
w=input()
l=[]
c=0
w="1"+w
for i in range(1,1+n):
  if w[i]!=w[i-1]:
    l.append(c)
    c=1
  else:
    c+=1
l.append(c)
if k>=len(l)//2:
  print(n)
else:
  if w[-1]=="0":
    l.append(0)
  s=l[0]
  for i in range(1,1+k):
    s+=l[2*i]+l[2*i-1]
  s1=s
  c1=l[0]+l[1]
  for i in range(1,(len(l)+1)//2-k):
    s1=s1-l[2*i-2]-l[2*i-1]+l[2*i-1+2*k]+l[2*i+2*k]
    if s1>s:
      s=s1
  print(s)

Verdicts: Runtime Error

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(N):
    cnt = 0
    change = 0
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

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

nums = []
now = 1
cnt = 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

sums = [0 for _ in range(len(nums) + 1)]
for i in range(len(nums)):
  sums[i + 1] = sums[i] + nums[i]

ans = 0
for i in range(len(nums)):
  if i % 2 == 1:
    continue
  
  left = i
  right = min(i + add, len(nums))
  tmp = sums[right] - sums[left]

  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):
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i])
    print(result)

N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if Y[0] < Z[0] else i-1])
    print(result)

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()
c = [int(s[i]) for i in range(n)]
ids = [0]
for i in range(1,n):
  if c[i] != c[i-1]:
    ids.append(i)
ids.append(n)
l = len(ids)
result = []
for i in range(l-1):
  if c[ids[i]] == 0:
    if i + 2*k < l:
      result.append(ids[i+2*k] - ids[i])
    else:
      result.append(n - ids[i])
  else:
    if i + 2*k < l:
      result.append(ids[i+2*k+1] - ids[i])
    else:
      result.append(n - ids[i])
print(max(result))

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

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()+"2"
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
print(_list)
C=len(_list)
K=min(C,K)
max([sum(_list[2*i:2*i+2*K+1]) for i in range(C-K+1)])

N,K=map(int,input().split())
S=input()+"2"
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
print(_list)
C=len(_list)
K=min(C,K)
print(max([sum(_list[2*i:2*i+2*K+1]) for i in range(C-K+1)]))

N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
print(_list)
print(__list)
C=len(__list)
K=min(C,K)
if C//2==K:
    print(_list[0])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range(C//2-K)]))

N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
C=len(__list)
K=min(C,K)
if (C+1)//2<=K:
    print(__list[-1])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range((C+1)//2-K)]))


Verdicts: Wrong Answer

Wrong Answer

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()
i,j = 0,0
onenumber = [0] * (K+1)
zeronumber = [0] * K
test, answer = 0,0
infront = ''

for x in S:
    if x == '0':
        if infront == '1':
            test += onenumber[j]
            answer = max(answer,test)
            print(answer)
            if j == K:
                j = -1
            j += 1
            if i == K:
                i = 0
            test -= onenumber[j]
            test -= zeronumber[i]
            onenumber[j],zeronumber[i] = 0, 0
        zeronumber[i] += 1
        infront = '0'
    if x == '1':
        if infront == '0':
            test += zeronumber[i]
            i += 1
        onenumber[j] += 1
        infront = '1'

if S[-1] == '0':
    test += zeronumber[i]
else:
    test += onenumber[j]
answer = max(answer,test)
print(answer)


N,K = map(int,input().split())
S= input()
i,j = 0,0
onenumber = [0] * (K+1)
zeronumber = [0] * K
test, answer = 0,0
infront = ''

for x in S:
    if x == '0':
        if infront == '1':
            test += onenumber[j]
            answer = max(answer,test)
            if j == K:
                j = -1
            j += 1
            if i == K:
                i = 0
            test -= onenumber[j]
            test -= zeronumber[i]
            onenumber[j],zeronumber[i] = 0, 0
        zeronumber[i] += 1
        infront = '0'
    if x == '1':
        if infront == '0':
            test += zeronumber[i]
            i += 1
        onenumber[j] += 1
        infront = '1'

if S[-1] == '0':
    test += zeronumber[i]
else:
    test += onenumber[j]
answer = max(answer,test)
print(answer)


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):
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
print(e)
for i in range(len(e)):
    g += abs(e[i])
    if f >= b:
        if e[i] > 0:
            h = max(h, g)
        if i-b*2 >= 0:
            g -= abs(e[i-b*2])
    if e[i] < 0:
        f += 1
print(h if len(e) != 1 else abs(e[0]))


a, b = map(int, input().split())
c = input()
d = []
e = 0
for i in range(1, a):
    e += 1
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 0
    if i == (a-1):
        e += 1
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
        e = 0
f = 0
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[i] > 0 and f >= b:
        h = max(h, g)
        if i-2*b >= 0:
            g -= abs(d[i-2*b])
    if d[i] < 0 and f >= b:
        g -= abs(d[i-2*b])
    if d[i] < 0 and f < b:
        f += 1
h = max(h, g)
print(h if h != 0 else a)


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 = [int(i) for i in input().split()]
s = input()

length_list = []

tmp_one_length = 0
tmp_zero_length = 0

for i in range(n):
    if s[i] == "1" and i > 0 and s[i-1] == "0":
        length_list.append([tmp_zero_length, tmp_one_length])
        tmp_one_length = 0
        tmp_zero_length = 0
    if s[i] == "1":
        tmp_one_length += 1
    elif s[i] == "0":
        tmp_zero_length += 1
if tmp_zero_length > 0:
    length_list.append([tmp_zero_length, 0])
suff = tmp_one_length
print(length_list)

ans_list = []
if len(length_list) > k: 
    start_idx = 0
    while start_idx + k < len(length_list):
        if start_idx == 0:
            length = 0
            for i in range(k):
                length += length_list[i][0]
                length += length_list[i][1]
        else:
            length = ans_list[len(ans_list)-1] - length_list[start_idx-1][0] - length_list[start_idx-1][1] + length_list[start_idx+k][0] + length_list[start_idx+k][1]
        if start_idx + k == len(length_list)-1:
            length += suff
        ans_list.append(length)
        start_idx += 1
else:
    ans_list.append(n)
        
print(max(ans_list))

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

s = '1' + s + '1'

cnt_1 = []
cnt_0 = []
stack_1 = 0
stack_0 = 0
for i in s:
    if i == '1':
        if stack_0 > 0:
            cnt_0.append(stack_0)
            stack_0 = 0
        stack_1 += 1
    else:
        if stack_1 > 0:
            cnt_1.append(stack_1)
            stack_1 = 0
        stack_0 += 1
if stack_1 > 0:
    cnt_1.append(stack_1)

if len(cnt_1) == 0 or len(cnt_0) == 0:
    print(n)
    exit()
cnt_1[0] -= 1
cnt_1[-1] -= 1

if k > len(cnt_0):
    k = len(cnt_0)
cum_1 = [sum(cnt_1[:k+1])]
cum_0 = [sum(cnt_0[:k])]
for i in range(1, len(cnt_1)-k):
    cum = cum_1[i-1] + cnt_1[i+k] - cnt_1[i-1]
    cum_1.append(cum)
for i in range(1, len(cnt_0)-k+1):
    cum = cum_0[i-1] + cnt_0[i+k-1] - cnt_0[i-1]
    cum_0.append(cum)
res = []

for v, w in zip(cum_1, cum_0):
    res.append(v+w)
print(max(res))

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()
from itertools import groupby
a=[len(list(j)) for i,j in groupby(s)]
if s[0]==0:
  a=[0]+a
if s[n-1]==0:
  a=a+[0]
w=2*k+1
if len(a)<=w:
  print(n)
  exit()
m=sum(a[:w])
x=m
for i in range(2,len(a)-2*k,2):
  m=m-a[i-1]-a[i-2]+a[i+k+1]+a[i+k+2]
  x=max(x,m)
print(x)

n,k=map(int,input().split())
s=input()
from itertools import groupby
a=[len(list(j)) for i,j in groupby(s)]
if s[0]=='0':
  a=[0]+a
if s[n-1]=='0':
  a=a+[0]
w=2*k+1
if len(a)<=w:
  print(n)
  exit()
m=sum(a[:w])
x=m
for i in range(2,len(a)-2*k,2):
  m=m-a[i-1]-a[i-2]+a[i+w-1]+a[i+w-2]
  x=max(x,m)
print(x)

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()
if '0' not in s:
    print(len(s))
else:
    s2 = []
    count = 1
    for i in range(1,len(s)):
        if s[i-1] == s[i]:
            count += 1
        if s[i-1] != s[i]:
            s2.append(count)
            count = 1
    s2.append(count)
    if s[0] != '1':
        s2.insert(0,0)
    if s[-1] != '1':
        s2.append(0)
    ans2 = 0
    K2 = 2*K+1
    for i in range(max(1,len(s2) - K2 +1)):
        ans2 = max(ans2,sum(s2[i:i+K2]))
    print(ans2)

N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    ans = ''
    count = 1
    for i in range(1,len(s)):
        if s[i-1] == s[i]:
            count += 1
        if s[i-1] != s[i]:
            ans += s[i-1] + str(count)
            count = 1
    s2 = ans+s[-1]+str(count)
    if s2[0] != '1':
        s2 = '10'+s2
    if s2[-2] != '1':
        s2 += '10'
    s3 = [int(s2[i]) for i in range(len(s2)) if i%2 == 1]
    ans2 = 0
    K2 = 2*K+1
    for i in range(len(s3) - K2 +1):
        ans2 = max(ans2,sum(s3[i:i+K2]))
    print(ans2)

N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    s2 = [0]
    count = 0
    for i in range(len(s)-1):
        count += 1
        if s[i] != s[i+1]:
            s2.append(count)
    s2.append(count+1)
    if s[0] == '0':
        s2.insert(0,0)
    ans = 0
    for i in range(0,len(s2)-1,2):
        ans = max(ans,s2[min(len(s2)-1,i+2*K+1)] - s2[i])
    print(ans)

Verdicts: Wrong Answer

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):
from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)
if tmp == "0":
    l.append(0) 

# print(nums)


acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = 2*K + 1

if len(nums) <= w:
    print(N)
else:
    ans = 0
    for i in range(0,n-w+1,2): 
        ans = max(ans,acc[i+w]-acc[i])

    print(ans)

from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)


acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = min(2*K + 1)

ans = 0
for i in range(0,n-w+1,2): 
    ans = max(ans,acc[i+w]-acc[i])
print(ans)

from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)


acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = min(2*K + 1, N//2)

ans = 0
for i in range(0,n-w+1,2): 
    ans = max(ans,acc[i+w]-acc[i])
print(ans)

from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)


acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = min(2*K + 1)

ans = 0
for i in range(0,n-w+1,2): 
    ans = max(ans,acc[i+w]-acc[i])
print(ans)

from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)
if tmp == "0":
    nums.append(0) 

# print(nums)


acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = 2*K + 1

if len(nums) <= w:
    print(N)
else:
    ans = 0
    for i in range(0,n-w+1,2): 
        ans = max(ans,acc[i+w]-acc[i])

    print(ans)

Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Accepted