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 _*_

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)



# s = [int(x) for x in list(input())]
#
# state = 1
# cnt = 0
# l = []


#         l.append(cnt)
#         cnt = 1
#         state = e
#     else:
#         cnt += 1
#
# if cnt != 0:
#     l.append(cnt)
#
#
# if len(l) % 2 == 1:
#     l.append(0)
#


#


#
# answer = 0

#     left = i


#     answer = max(tmp, answer)
#
# print(answer)


# _*_ coding: utf-8 _*_

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)

# _*_ coding: utf-8 _*_

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
flag = s[0]
reverse = 0

tmp = s[0]
cnt = 0
for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)

if flag == 0:
    n_zero = (len(cnts) + 1) // 2
else:
    n_zero = len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)
else:
    if flag == 0:
        ans = 0
        for inx in range(0, len(cnts), 2):
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[inx: end])
            if tmp > ans:
                ans = tmp
    else:
        ans = 0
        for inx in range(1, len(cnts), 2):
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[inx-1:end])
            if tmp >= ans:
                ans = tmp

print(ans)

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)

# _*_ coding: utf-8 _*_

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)


n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)

else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = min(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
# print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
cumsum = []
for i in range(len(cnts)):
    if i == 0:
        cumsum.append(cnts[0])
    else:
        cumsum.append(cumsum[-1] + cnts[i])

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 2)
        end = min(inx + 2 * k -1 , len(cnts))
        tmp = cumsum[end] - cumsum[start]
        ans = max(tmp, ans)

print(ans)

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
  if s[i] == tmp:
    cnt += 1
    else: # here tmp should be cleared
      cnts.append(cnt)
      cnt = 1
      tmp = s[i]
      cnts.append(cnt)
      print(cnts)

      n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
      # print("# of zeros block: ", n_zero)
      if n_zero <= k:
        ans = sum(cnts)
        else:
          ans = 0
          for inx in range(flag, len(cnts), 2):
            start = max(0, inx - 1)
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[start: end])
            ans = max(tmp, ans)

            print(ans)

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
# print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
if n_zero <= k:
     ans = sum(cnts)

else:
    ans = 0
    cumsum = [0]
    for i in range(len(cnts)):
        cumsum.append(cumsum[-1] + cnts[i])

    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k , len(cnts))
        tmp = cumsum[end] - cumsum[start]
        ans = max(tmp, ans)

print(ans)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Accepted