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=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
odd=[]
even=[]
tmp=0
if s[0]==0:
  flag=False
else:
  flag=True
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      odd.append(tmp)
    else:
      even.append(tmp)
  else:
    if flag:
      even.append(tmp)
    else:
      odd.append(tmp)
ans=0
for i in range(len(even)-k-1):
  ans=max(ans,even[i+k+1]-even[i])
print(max(ans,1))
    

n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
odd=[]
even=[]
tmp=0
if s[0]==0:
  flag=False
else:
  flag=True
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      odd.append(tmp)
    else:
      even.append(tmp)
  else:
    if flag:
      even.append(tmp)
    else:
      odd.append(tmp)
ans=0
for i in range(len(even)-k-1):
  ans=max(ans,even[i+k+1]-even[i])
print(ans)
    

n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
acc=[]
acc2=[]
ans=0
if s[0]==1:
  Acc=stack[0]
  Acc2=0
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc[i+k]-acc2[i])
if s[0]==0:
  Acc=stack[0]
  Acc2=0
  acc.append(0)
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc2[i+k]-acc[i])
print(ans)

n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
if s[0]==1:
  if len(s)//2<=k:
    print(len(s))
    exit()
if s[0]==0:
  if (len(s)+1)//2<=k:
    print(len(s))
    exit()
acc=[]
acc2=[]
ans=0
if s[0]==1:
  Acc=stack[0]
  Acc2=0
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==0:
    Acc2+=stack[i+1]
    Acc2+=stack[i+2]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc[i+k]-acc2[i])
if s[0]==0:
  Acc=stack[0]
  Acc2=0
  acc.append(0)
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-1-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==0:
    Acc2+=stack[i+1]
    Acc2+=stack[i+2]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc2[i+k]-acc[i])
print(ans)

n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
left=[0]
right=[0]
tmp=0
if s[0]==0:
  flag=False
  if len(stack)%2==1:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
else:
  if len(stack)%2==0:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      right.append(tmp)
    else:
      left.append(tmp)
  else:
    if flag:
      left.append(tmp)
    else:
      right.append(tmp)
ans=0
if flag:
    for i in range(len(right)-k-1):
      ans=max(ans,right[i+k+1]-left[i])
else:
  for i in range(len(right)-k):
    ans=max(ans,right[i+k]-left[i])
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Accepted