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

on = []
off = []

acnt = 0
bcnt = 0
for i in s:
  if i == '0':
    if bcnt > 0:
      off.append(bcnt)
      bcnt = 0
    acnt += 1
  else:
    if acnt > 0:
      on.append(acnt)
      acnt = 0
    bcnt += 1
if bcnt > 0:
  off.append(bcnt)
  bcnt = 0
if acnt > 0:
  on.append(acnt)
  acnt = 0
ans = 0
if s[0] == '1':
  for i in range(len(on)-k+1):
    s = 0
    if i == 0:
      a = sum(on[i:i+k])
      b = sum(off[i:i+k+1])
    else:
      a += on[i+k-1]
      a -= on[i-1]
      if i+k <= len(off)-1:
        b += off[i+k]
      b -= off[i-1]
    # print (a,b)
    s = a+b
    ans = max(ans,s)
else:
  for i in range(len(on)-k+1):
    if i == 0:
      a = sum(on[i:i+k])
      b = sum(off[i:i+k])
    elif i == 1:
      a = sum(on[i:i+k])
      b = sum(off[i-1:i+k])
    else:
      a += on[i+k-1]
      a -= on[i-1]
      if i+k-1 <= len(off)-1:
        b += off[i+k-1]
      b -= off[i-2]
    s = a+b
    print (a,b)
    ans = max(ans,s)
if ans == 0:
  print (n)
else:
  print (ans)


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

on = []
off = []

acnt = 0
bcnt = 0
for i in s:
  if i == '0':
    if bcnt > 0:
      off.append(bcnt)
      bcnt = 0
    acnt += 1
  else:
    if acnt > 0:
      on.append(acnt)
      acnt = 0
    bcnt += 1
if bcnt > 0:
  off.append(bcnt)
  bcnt = 0
if acnt > 0:
  on.append(acnt)
  acnt = 0
ans = 0
if s[0] == '1':
  for i in range(len(on)-k+1):
    s = sum(on[i:i+k]) + sum(off[i:i+k+1])
    # print (sum(on[i:i+k]),sum(off[i:i+k+1]))
    ans = max(ans,s)
else:
  for i in range(len(on)-k+1):
    if i == 0:
      s = sum(on[i:i+k]) + sum(off[i:i+k])
      # print (sum(on[i:i+k]),sum(off[i:i+k]))
    else:
      s = sum(on[i:i+k]) + sum(off[i-1:i+k])
      # print (sum(on[i:i+k]),sum(off[i-1:i+k]))
    ans = max(ans,s)
if ans == 0:
  print (n)


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

on = []
off = []

acnt = 0
bcnt = 0
for i in s:
  if i == '0':
    if bcnt > 0:
      off.append(bcnt)
      bcnt = 0
    acnt += 1
  else:
    if acnt > 0:
      on.append(acnt)
      acnt = 0
    bcnt += 1
if bcnt > 0:
  off.append(bcnt)
  bcnt = 0
if acnt > 0:
  on.append(acnt)
  acnt = 0
ans = 0
if s[0] == '1':
  for i in range(len(on)-k+1):
    s = 0
    if i == 0:
      a = sum(on[i:i+k])
      b = sum(off[i:i+k+1])
    else:
      a += on[i+k-1]
      a -= on[i-1]
      if i+k <= len(off)-1:
        b += off[i+k]
      b -= off[i-1]
    # print (a,b)
    s = a+b
    ans = max(ans,s)
else:
  for i in range(len(on)-k+1):
    if i == 0:
      a = sum(on[i:i+k])
      b = sum(off[i:i+k])
    elif i == 1:
      a = sum(on[i:i+k])
      b = sum(off[i-1:i+k])
    else:
      a += on[i+k-1]
      a -= on[i-1]
      if i+k-1 <= len(off)-1:
        b += off[i+k-1]
      b -= off[i-2]
    s = a+b
    # print (a,b)
    ans = max(ans,s)
if ans == 0:
  print (n)
else:
  print (ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted