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

nums = []
if S[0] != '1':
  nums.append(0)

current = S[0]
count = 1
for i, s in enumerate(list(S)):
  if i == len(S)-1:
    nums.append(count)
    break
  if S[i+1] != s:
    nums.append(count)
    count = 1
    current = s
  else:
    count += 1

if S[len(S)-1] != '1':
  nums.append(0)

sums = [0]
for index, i in enumerate(nums):
  sums.append(sums[index] + i)
  
res = 0
for left in range(0, len(sums), 2):
  right = left + K*2 + 1
  if right > len(sums):
    right = len(sums) - 1
  print(left, right)
  res = max(res, sums[right] - sums[left])
  
print(res)

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

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

nums = []
if S[0] != '1':
  nums.append(0)

current = S[0]
count = 1
for i, s in enumerate(list(S)):
  if i == len(S)-1:
    nums.append(count)
    break
  if S[i+1] != s:
    nums.append(count)
    count = 1
    current = s
  else:
    count += 1

if S[len(S)-1] != '1':
  nums.append(0)

sums = [0]
for index, i in enumerate(nums):
  sums.append(sums[index] + i)
  
res = 0
for left in range(0, len(sums), 2):
  right = left + K*2 + 1
  if right > len(sums):
    right = len(sums) - 1
  res = max(res, sums[right] - sums[left])
  
print(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=list(input())
T=[int(a) for a in S]
U=[]

tmp=T[0]
cnt=1
for i in range(1,N):
  if i==N-1:
    if T[i]==T[i-1]:
      cnt+=1
      U.append((tmp,cnt) )
    else:
      U.append((tmp,cnt) )
      U.append((T[i],1))
  else:
    if T[i]==T[i-1]:
      cnt+=1
    else:
      U.append((tmp,cnt) )
      tmp=T[i]
      cnt=1
    
print(U)

for _ in range(K):
  maxi=0
  for i,kumi in enumerate(U):
    if kumi[0]==0:
      if i==0:
        if len(U)>1:
          tmp = kumi[1]+U[i+1][1]
        else:
          tmp= kumi[1]
      elif 0<i<len(U)-1:
        tmp = U[i-1][1] + kumi[1]+ U[i+1][1]
      else: # i=len(U)-1
        tmp= U[i-1][1] + kumi[1]
      maxi=max(maxi,tmp)
  print("search:",maxi)
  for i,kumi in enumerate(U):
    if kumi[0]==0 and kumi[1]==maxi:
      if i==0:
        if len(U)>1:
          U[1][1] += kumi[1]
          U.pop(0)
        else:
          kumi[0]=1
      elif 0<i<len(U)-1:
        U[i-1][1] += kumi[1]+U[i+1][1]
        U.pop(i)
        U.pop(i)
      else: # i=len(U)-1
        U[i-1][1] += kumi[1]
        U.pop()
  print(U)

N,K=map(int,input().split())
S=list(input())
T=[int(a) for a in S]
U=[]

if N==1:
  U=[ [T[0],1] ]
else:
  tmp=T[0]
  cnt=1
  for i in range(1,N):
    if i==N-1:
      if T[i]==T[i-1]:
        cnt+=1
        U.append([tmp,cnt] )
      else:
        U.append([tmp,cnt] )
        U.append([T[i],1])
    else:
      if T[i]==T[i-1]:
        cnt+=1
      else:
        U.append([tmp,cnt] )
        tmp=T[i]
        cnt=1

summ=0
for j in range(len(U)):
  summ += U[j][1]
  U[j][1]=summ
if U[0][0]==0:
  U.insert(0,[1,0])
  U.insert(0,[0,0])
else:
  U.insert(0,[0,0])

#print(U)
maxi=0
  
for p,q in enumerate(U):
  if q[0]==1:
    pass
  else:
    right=min(p+K*2+1,len(U)-1)
    diff=U[right][1]-U[p][1]
    maxi=max(diff,maxi)
    #print(p,right,diff)
print(maxi)

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())

cnt = 1
cntlist = []
for i in range(1, N):
    if s[i - 1] != s[i]:
        cntlist.append(cnt)
        cnt = 1
    else:
        cnt += 1

cntlist.append(cnt)
if s[0] == "0":
    cntlist = [0] + cntlist

ans = 0
for i in range(2 * K + 1):
    if i >= len(cntlist):
        break
    ans += cntlist[i]
anslist = [ans]
s = 0
g = 2 * K + 1
while g < len(cntlist):
    ans -= cntlist[s]
    ans -= cntlist[s + 1]
    s += 2
    ans += cntlist[g]
    g += 1

    ans += cntlist[g]
    if g >= len(cntlist):
        break
    g += 1
    anslist.append(ans)

print(max(anslist))

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

cnt = 1
cntlist = []
for i in range(1, N):
    if s[i - 1] != s[i]:
        cntlist.append(cnt)
        cnt = 1
    else:
        cnt += 1

cntlist.append(cnt)
if s[0] == "0":
    cntlist = [0] + cntlist

ans = 0
for i in range(2 * K + 1):
    if i >= len(cntlist):
        break
    ans += cntlist[i]
anslist = [ans]
s = 0
g = 2 * K + 1
while g < len(cntlist):
    ans -= cntlist[s]
    ans -= cntlist[s + 1]
    s += 2
    ans += cntlist[g]
    g += 1
    if g >= len(cntlist):
        anslist.append(ans)
        break
    ans += cntlist[g]
    g += 1
    anslist.append(ans)

print(max(anslist))

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()
s_split_0 = s.split("1")
s_split_1 = s.split("0")

s_split_1= [len(i) for i in s_split_1 if i!=""]
s_split_0= [len(i) for i in s_split_0 if i!=""]

if s[0]=="1":
    print(max([sum(s_split_0[i:i+k])+sum(s_split_1[i:i+k+1]) for i in range(len(s_split_0)-k+1)]))
else:
    print(max([sum(s_split_0[i:i+k+1])+sum(s_split_1[i:i+k]) for i in range(len(s_split_1)-k+1)]))


import sys
input =  sys.stdin.readline

n, k = map(int, input().split())
s = input()+'-1'

cnt = 0
cnt_list = []
for i in range(n):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt+1)
        cnt = 0

# print(cnt_list)


l = 0
r = 2*k
ma = 0
su = 0
if s[0] == '0':
    r = 2*k
else:
    r = 2*k+1
su = sum(cnt_list[l:r])
while l < r:
    if ma < su:
        ma = su
    if l == 0 and r == 2*k:
        l += 1
        su -= cnt_list[l-1]
    else:
        l += 2
        su -= sum(cnt_list[l-2:l])
    r += 2
    su += sum(cnt_list[r-2:r])
    if r >= len(cnt_list):
        r = len(cnt_list)
    
print(ma)


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):
import numpy as np

N,K = map(int, input().split())
S = [int(x) for x in input()]

memo = np.zeros(10**5+1, dtype = np.int64)
W = min(2*K, 10**5)

current = 1 
p = 0

for x in S:
  if x == current:
    memo[x,p] += 1
  else:
    current = 1 - current
    p += 1

ans = memo[:W+1].sum()
x = ans
for r in range(2,10**5+1,2):
  if r+W > 10**5:
    break
  x = x-memo[r-2]-memo[r-1]+memo[r+W]+memo[r+W-1]
  if x > ans:
    ans = x

print(ans)

import numpy as np

N,K = map(int, input().split())
S = [int(x) for x in input()]

memo = np.zeros(10**5+1, dtype = np.int64)
W = min(2*K, 10**5)

current = 1 
p = 0

for x in S:
  if x == current:
    memo[p] += 1
  else:
    current = 1 - current
    p += 1
    memo[p] += 1

ans = memo[:W+1].sum()
x = ans
for r in range(2,10**5+1,2):
  if r+W > 10**5:
    break
  x = x-memo[r-2]-memo[r-1]+memo[r+W]+memo[r+W-1]
  if x > ans:
    ans = x

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 = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(K,lle):
  if 2*k+1 > lle:
    break
  cd = su[2*k+1] - su[2*k-2*K]
  if cd > mxm:
    mxm = cd
print(mxm)

N,K = map(int,input().split())
S = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(1,lle):
  if 2*k+1 >= lle:
    break
  cd = su[2*k+1] - su[2*k-2]
  if cd > mxm:
    mxm = cd
print(divide,zero,su,mxm)

n,k = map(int,input().split())
s = input()
tmp = 1
spl = []
for i in range(1,n):
  if s[i-1] == s[i]:
    tmp += 1
  else:
    spl.append(tmp)
    tmp = 1
spl.append(tmp)
#print(spl)
l = len(spl)
acu = [0]
for m in range(l):
  acu.append(acu[-1]+spl[m])
acu = [0] + acu + [acu[-1]]
#print(acu)
can = []
if s[0] == '0':
  st = 0
else:
  st = 1
while True:
  try:
    can.append(acu[st+2*k+1]-acu[st])
    st += 2
  except:
    break
try:
  print(max(can))
except:
  print(n)

Verdicts: 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):
k = int(input().split(" ")[1])
s = input()

m = 0

for i in range(len(s)):
  if i>0:
    if s[i] == s[i-1]: continue
  j = i
  count = 0
  while j<len(s) and count <= k:
    if j==i:
      if s[j]=="0": 
        count+=1
    elif s[j-1]=="1" and s[j]=="0": 
      count+=1
    j +=1
  
  m = max(m,j-i)

print(m)

k = int(input().split(" ")[1])
s = input()

m = 0

for i in range(len(s)):
  if i>0:
    if s[i] == s[i-1]: continue
  j = i
  count = 0
  while j<len(s) and count <= k:
    if j==i:
      if s[j]=="0": 
        count+=1
    elif s[j-1]=="1" and s[j]=="0": 
      count+=1
    j +=1
  if j == len(s): j+=1
  m = max(m,j-i-1)

print(m)

k = int(input().split(" ")[1])
s = input()

arr = []
if s[0] is "0": 
  arr.append(0)
arr.append(1)
for i in range(1,len(s)):
  if s[i-1] is s[i]:
    arr[-1] += 1
  else:
    arr.append(1)
if s[-1] is "0": 
  arr.append(0)

sums = [0]
for i in range(len(arr)):
  sums.append(sums[i] + arr[i])
t = 2*k+1
m = 0
for i in range(0,len(arr),2):
  m = max(m,sums[min(len(sums)-1,i+t)]-sums[i])
print(m)


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() + "#"

pre, cons, comp = '#', 0, [(0, -1)]
for i in range(len(s)):
    if pre != '#' and pre != s[i]:
        if cons > 0:
            print(cons, s[i-1])
            comp.append((cons, s[i-1]))
            cons = 1
    else:
        cons += 1

    pre = s[i]

acc = [0 for _ in range(len(comp))]
for i in range(1, len(comp)):
    acc[i] = acc[i-1] + comp[i][0]

ans = 0
for i in range(len(comp) - 1):
    lim = len(comp) - 1
    if comp[i+1][1] == '1':
        lim = min(lim, i + 2*K + 1)
    else:
        lim = min(lim, i + 2*K)
    
    ans = max(ans, acc[lim] - acc[i])

print(ans)


import sys

N, K = map(int, input().split())
s = input() + "#"

pre, cons, comp = '#', 0, [(0, -1)]
for i in range(len(s)):
    if pre != '#' and pre != s[i]:
        if cons > 0:
            print(cons, s[i-1], file=sys.stderr)
            comp.append((cons, s[i-1]))
            cons = 1
    else:
        cons += 1

    pre = s[i]

acc = [0 for _ in range(len(comp))]
for i in range(1, len(comp)):
    acc[i] = acc[i-1] + comp[i][0]

ans = 0
for i in range(len(comp) - 1):
    lim = len(comp) - 1
    if comp[i+1][1] == '1':
        lim = min(lim, i + 2*K + 1)
    else:
        lim = min(lim, i + 2*K)
    print("i = {}, lim = {}".format(i, lim), file=sys.stderr)
    ans = max(ans, acc[lim] - acc[i])

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())
sss = input()

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if len(x) % 2 == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

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

print(ans)

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if sss[-1] == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])

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

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=(int(x) for x in input().split())
S=input()

def Solve(S,N,K):
    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]
    LB=[]
    k=-1
    current="0"
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                LB.append(k)
                current="0"
                k=0
            else:
                LA.append(k)
                current="1"
                k=0

    if current=="1":
        LB.append(k+1)
    else:
        LA.append(k+1)
    
    
    LB.append(0)
    mx=0
    for i in range(len(LA)-K+1):
        result=sum(LA[i:i+K])+sum(LB[i-1:i+K])
        mx=max(mx,result)

    return mx

print(Solve(S,N,K))


import numpy
N,K=(int(x) for x in input().split())
S=input()

def Solve(S,K):

    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]
    LB=[]
    k=-1
    current="0"
    appendLA=LA.append
    appendLB=LB.append
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                appendLA(k)
                current="0"
                k=0
            else:
                appendLB(k)
                current="1"
                k=0
    
    if current=="1":
        appendLA(k+1)
    else:
        appendLB(k+1)
        appendLA(0)
    mx=0
    if len(LA)<=K:
        return len(S)
    if K==0:
        return max(LA)

    sum_LA = list(numpy.cumsum(LA))
    sum_LB=list(numpy.cumsum(LB))
    

    for i in range(len(LA)-K+1):
        if i==0:
            result=sum_LA[K-1]+sum_LB[K-1]
        elif i==1:
            result=sum_LA[i+K-1]+sum_LB[i+K-1]-sum_LB[i-1]
        else:
            result=sum_LA[i+K-1]-sum_LA[i-2]+sum_LB[i+K-1]-sum_LB[i-1]
        mx=max(mx,result)

    return mx

print(Solve(S,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 collections
n, k = map(int, input().split())
s = [int(i) for i in input()]
c = []
prv = 1
cnt = 0
for i in s:
    if prv != i:
        c.append(cnt)
        cnt = 1
        prv = i
    else:
        cnt += 1
if cnt > 0:
    c.append(cnt)
if prv == 0:
    c.append(0)
que = collections.deque(c[0])
res =c[0]
ans = c[0]
m = len(c) // 2
for i in range(m):
    if len(que) == 2*k + 1:
        res -= que.popleft() + que.popleft()
    que.append(c[2*i +1])
    que.append(c[2*i +2])
    res += c[2*i+1] + c[2*i+2]
    ans = max(res, ans)
print(ans)
        

import collections
n, k = map(int, input().split())
s = [int(i) for i in input()]
c = []
prv = 1
cnt = 0
for i in s:
    if prv != i:
        c.append(cnt)
        cnt = 1
        prv = i
    else:
        cnt += 1
if cnt > 0:
    c.append(cnt)
if prv == 0:
    c.append(0)
que = collections.deque([c[0]])
res =c[0]
ans = c[0]
m = len(c) // 2
for i in range(m):
    if len(que) == 2*k + 1:
        res -= que.popleft() + que.popleft()
    que.append(c[2*i +1])
    que.append(c[2*i +2])
    res += c[2*i+1] + c[2*i+2]
    ans = max(res, ans)
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):
def cul(lst, m):
    r = 0
    for i in lst:
        r  += i if i < m else m
    return r

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

a = 1
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        a += 1
    else:
        val.append(a)
        a = 1
val.append(a)
ans = []
now = int(s[0])
for i in range(len(s)-k):
    lst = val[i:i+k*2+1]
    if now == 1:
        ans.append(cul(lst, n))
    now = 1 if now == 0 else 0

print(max(ans) if not len(ans) == 0 else 1)

def cul(lst, m):
    r = 0
    for i in lst:
        r  += i if i < m else m
    return r

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

if len(s) == 1:
    print(1)

a = 1
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        a += 1
    else:
        val.append(a)
        a = 1
val.append(a)
ans = []
now = int(s[0])
for i in range(len(s)-k):
    lst = val[i:i+k*2+1]
    if now == 1:
        ans.append(cul(lst, n))
    now = 1 if now == 0 else 0
print(max(ans))

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

now = 1
cnt = 0
for i in range(len(s)):
    if s[i] == str(now): cnt += 1
    else:
        nums.append(cnt)
        now = 1 - now
        cnt = 1
if not cnt == 0: nums.append(cnt)
if len(nums) % 2 == 0: nums.append(0)

summ = [0]
for i in range(len(nums)):
    summ.append(summ[i]+nums[i])

ans = 0
for i in range(len(nums))[::2]:
    left = i
    right = min(i+k*2+1, len(nums))
    tmp = summ[right] - summ[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,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)

if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])


b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
n=len(b)-1
for i in range(len(b),2):
    ans = max(ans,b[min(i+l,n)]-b[max(0,i)])
print(ans)


n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
b.append(b[-1])
n = len(b)
l = 2*k+1
st=int(s[0])
ans = 0
for i in range(st,n,2):
    ans = max(ans,b[i]-b[max(0,i-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 = list(map(int, input().split()))
s = input()

end = []
start = []

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

for i in range(n-1):
	if s[i] == "1" and s[i+1] == "0":
		end.append(i)
for i in range(1, n):
	if s[i-1] == "0" and s[i] == "1":
		start.append(i)

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

ans = 0

print(start, end, ans)
print(len(start), len(end), ans)

for j in range(len(start) - k):
	temp = end[j+k] - start[j] + 1 
	if temp > ans:
		ans = temp

if len(start) <= k:
	ans = n

print(start, end, ans)


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

end = []
start = []

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

for i in range(n-1):
	if s[i] == "1" and s[i+1] == "0":
		end.append(i)
for i in range(1, n):
	if s[i-1] == "0" and s[i] == "1":
		start.append(i)

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

ans = 0

"""print(start, end, ans)
print(len(start), len(end), ans)"""

for j in range(len(start) - k):
	temp = end[j+k] - start[j] + 1 
	if temp > ans:
		ans = temp

if len(start) <= k:
	ans = n

print(start, end, ans)


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

end = []
start = []

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

for i in range(n-1):
	if s[i] == "1" and s[i+1] == "0":
		end.append(i)
for i in range(1, n):
	if s[i-1] == "0" and s[i] == "1":
		start.append(i)

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

ans = 0

"""print(start, end, ans)
print(len(start), len(end), ans)"""

for j in range(len(start) - k):
	temp = end[j+k] - start[j] + 1 
	if temp > ans:
		ans = temp

if len(start) <= k:
	ans = n

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 copy import deepcopy
from itertools import accumulate
n,k=map(int,input().split())
s=input()
l=[]
cnt=1
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
  else:
    l.append(cnt)
    cnt=1
l.append(cnt)
al=deepcopy(l)
al=[0]+al
al=list(accumulate(al))
print(al)
ans=0
for i in range(len(al)):
  if s[0]=='0':
    if i%2:
      ans=max(ans,al[min(i+2*k-1,len(al)-1)]-al[i-1])
  else:
    if i%2:
      ans=max(ans,al[min(i+2*k,len(al)-1)]-al[i-1])
print(ans)

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

N,K = map(int,input().split())
S = input()
L = [0]
for i in range(N):
    if S[i] == '0'and a != S[i]:
        L.append(i)
    a = S[i]
L.append(N)
for i in range(len(L)-3):
    ans = (L[i+3]-1)-(L[i])
if N == 1:
    ans = 1
elif N == 2 or S == '010':
    ans = 2
elif N == 3:
    ans = 3
    
print(ans)

N,K = map(int,input().split())
S = input()
L = [0]
for i in range(N):
    if S[i] == '0'and a != S[i]:
        L.append(i)
    a = S[i]
L.append(N)
for i in range(len(L)-(K+1)):
    ans = (L[i+(K+1)]-1)-(L[i])
if N == 1:
    ans = 1
elif N == 2 or S == '010':
    ans = 2
elif N == 3:
    ans = 3
    
print(ans)

N,K = map(int,input().split())
S = '9'+input()+'9'
S = [int(s) for s in S]
a = S[0]
L = []
for s  in S:
    if s != a:
        L.append(1)
        a = s
    else:
        L.append(0)
        a = S[0]
for i in range(N):
    if N == 1:
        ans = 1
        break
    elif N == 2:
        ans = 2
        break
    if a != S[i]:
        for j in range(i,N):
            if sum(S[i:j]) > K*2:
                ans = j-i
                break

print(ans)

def func():
    N,K = map(int,input().split())
    S = input()
    L = []
    L.append(0) if S[0] == '0' else 0
    num = '2'
    for i in range(N):
        if num != S[i]:
            L.append(i)
            num = S[i]
    L.append(N)
    L.append(N)
    ans = 0
    for i in range(0,len(L)-K*2-1,2):
        ans = max(ans,L[i+K*2+1]-L[i])
    return N if N < 3 or K >= len(L)//2 else ans
print(func())

Verdicts: Runtime Error

Runtime Error

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

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

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

n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    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)

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

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

import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s)-1)
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    li_1.append(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_0)-k):
        Sum_0 = Sum_0 - li_0[i] + li_0[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)








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 = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(sum_list)-2*k, 1)
ans = 0
for i in range(0,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(cnt_list)-2*k, 1)
ans = 0
for i in range(1,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)

sum_list = []
for i in range(0,len(cnt_list)-2*k):
    sum_list.append(sum(cnt_list[i:i+2*k+1]))
print(max(sum_list))

n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
ans = 0
for i in range(0,len(sum_list),2):
    right = min(i+2*k+1,len(sum_list)-1)
    ans = max(ans,sum_list[right]-sum_list[i])
print(ans)


Verdicts: Runtime Error

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):
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
lis = []
if(S[0] == "0"):
    lis.append(0)
tmp = S[0]

for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))

if("0" in tmp):
    lis.append(0)
# print(lis)


L = len(lis)
# print(L)
asum = [0]*(L+1)
asum[0] = 0
for i in range(1,L+1):
    asum[i] = asum[i-1] + lis[i-1]
# print(asum)


l = 0
ans = max(lis) if l+2*K+1 < L+1 else sum(lis)
for r in range(l+2*K+1 , L+1):
    # print(l,r)
    
    ans = max(asum[r] - asum[l],ans)
    l+=1
print(ans)

# 14 2
# 11101010110011
# 10 1
# 0000000001


from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
lis = []
if(S[0] == "0"):
    lis.append(0)
tmp = S[0]

for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))

if("0" in tmp):
    lis.append(0)
# print(lis)


L = len(lis)
# print(L)
asum = [0]*(L+1)
asum[0] = 0
for i in range(1,L+1):
    asum[i] = asum[i-1] + lis[i-1]
# print(asum)


l = 0
ans = max(lis) if l+2*K+1 < L+1 else sum(lis)
for r in range(l+2*K+1 , L+1,2):
    # print(l,r)
    
    ans = max(asum[r] - asum[l],ans)
    l+=2
print(ans)

# 14 2
# 11101010110011
# 10 1
# 0000000001


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
import time
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    S = input()
    L = len(S)
    count = []
    tmp = 0
    pre = S[0]
    for i in range(L):
        if S[i] == pre:
            tmp += 1
        else:
            count.append(tmp)
            tmp = 1
            pre = S[i]
    count.append(tmp)
    count = [0] + count + [0]
    cumsum = np.cumsum(np.array(count))
    diff = 2*K +1
    ans = 0
    if diff>=len(cumsum):
        ans = cumsum[-1]
    else:
        for i in range(len(cumsum)-diff):
            tmp = cumsum[i+diff]-cumsum[i]
            ans = max(ans,tmp)
    print(ans)


if __name__ == '__main__':
    main()

import sys
import time
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    S = input()
    L = len(S)
    count = []
    tmp = 0
    pre = S[0]
    for i in range(L):
        if S[i] == pre:
            tmp += 1
        else:
            count.append(tmp)
            tmp = 1
            pre = S[i]
    count.append(tmp)
    count = [0] + count + [0]
    cumsum = np.cumsum(np.array(count))
    ans = 0
    diff = 2*K+1
    for i in range(max(len(count)-diff,1)):
        j = min(i+diff, len(cumsum)-1)
        tmp = cumsum[j] - cumsum[i]
        ans = max(ans,tmp)
    print(ans)


if __name__ == '__main__':
    main()

import numpy as np

N,K = map(int, input().split())
S = [int(x) for x in input()]

memo = np.zeros(10**5+1, dtype = np.int64)
W = min(2*K, 10**5)

current = 1 
p = 0

for x in S:
  if x == current:
    memo[p] += 1
  else:
    current = 1 - current
    p += 1
    memo[p] += 1

ans = memo[:W+1].sum()
x = ans
for r in range(2,10**5+1,2):
  if r+W > 10**5:
    break
  x = x-memo[r-2]-memo[r-1]+memo[r+W]+memo[r+W-1]
  if x > ans:
    ans = x

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()

ruiwa = [0]

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        ruiwa.append(i)
ruiwa.append(n)

if s[0] == "1":
    ans = ruiwa[1]
    for i in range(((len(ruiwa)-1)//2)-k+1):
        ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
else:
    ans = 0
    for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 -k + 1):
        ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[min(0,2*i-1)])

print(ans)

    n,k=map(int, input().split())
    s=input()
    
    ruiwa = [0]

    for i in range(1,len(s)):
        if s[i] != s[i-1]:
            ruiwa.append(i)
    ruiwa.append(n)

    if s[0] == "1":
        ans = ruiwa[1]
        if ((len(ruiwa) - 1) // 2) <= k:
            ans = n
        else:
            for i in range(((len(ruiwa)-1)//2)-k+1):
                ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
    else:
        ans = 0
        if len(ruiwa) - 1 - (len(ruiwa) - 1) // 2 <= k:
            ans = n
        else:
            for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 - k + 1):
                ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[max(0,2*i-1)])

    print(ans)


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

ruiwa = [0]

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        ruiwa.append(i)
ruiwa.append(n)

if s[0] == "1":
    ans = ruiwa[1]
    if ((len(ruiwa) - 1) // 2) <= k:
        ans = n
    else:
        for i in range(((len(ruiwa)-1)//2)-k+1):
            ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
else:
    ans = 0
    if len(ruiwa) - 1 - (len(ruiwa) - 1) // 2 <= k:
        ans = n
    else:
        for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 - k + 1):
            ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[max(0,2*i-1)])

print(ans)

Verdicts: Wrong Answer

Runtime Error

Accepted