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+='a'
l=[]
a=s[0]

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

if len(l)%2==0:
  pass
else:
  l=l+[0]

l2=[]

if f==1:
  pass
else:
  l2=[0]+l2

for i in range(len(l)):
  if i%2==0:
    l2.append(l[i]+l[i+1])

mx=sum(l2[:2*(k-1)+1])
a=l2[0]
for i in range(len(l2)-1):
  print(i)
  x=l2[i+k+1]
  if a<x:
    mx+=x
    mx-=a
    a=l2[i]

print(mx)

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

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

  
  if s[-1]=='0':
    l+=[0]

  l=[0]+l

  # print(l)

  ans=0
  left=0
  while True:
    right=min(len(l)-1,left+2*k+1)
    if left>=len(l):
      return ans

    # print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2

# main()
print(main())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list
def summarize_list(l):
  sl=l

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)

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

  mx=-inf
  if s[0]=='1':
    haba=2*k+1
    for i in range(len(l)-haba+1):
      if i%2==0:
        mx=max(mx,l[i+haba]-l[i])
  else:
    a

  return mx

# main()
print(main())


n,k=map(int,input().split())
s=input()
s+='a'
l=[]
a=s[0]

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

if len(l)%2==0:
  pass
else:
  l=l+[0]

l2=[]

if f==1:
  pass
else:
  l2=[0]+l2

for i in range(len(l)):
  if i%2==0:
    l2.append(l[i]+l[i+1])

mx=sum(l2[:2*(k-1)+1])
a=l2[0]
for i in range(1,len(l2)):
  x=l2[i]
  if a<x:
    mx+=x
    mx-=a
    a=x

print(mx-1)

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

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

  
  if s[0]=='0':
    l=[0]+l

  
  if s[-1]=='0':
    l+=[l[-1]+1]

  l=[0]+l

  ans=0
  left=0
  right=min(len(l)-1,2*k+1)
  
  while True:
    if right>=len(l):
      return ans

    print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2
    right+=2

# main()
print(main())


import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

  
  if s[0]=='0':
    l=[0]+l

  
  if s[-1]=='0':
    l+=[0]

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

  l=[0]+l

  # print(l)

  ans=0
  left=0
  while True:
    right=min(len(l)-1,left+2*k+1)
    if left>=len(l):
      return ans

    # print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2

# main()
print(main())


Verdicts: Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted