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):
q = list(map(int, input().split()))
a = [i for i in input()]

for s in range(q[1]):
    target_list = []
    for i, n in enumerate(a):
        cnt = 0
        if i != '0':
            target_list.append(cnt)
            break
        else:
            for j, m in enumerate(a[i + 1:len(a)]):
                init = True
                if m == '0' and init:
                    cnt += 1
                    break
                if m == '1' and init:
                    cnt += 1
                    init = False
            for k, l in enumerate(reversed(a[:i + 1])):
                if l == '1':
                    cnt += 1
    maxval = max(target_list)
    for x, y in enumerate(a[target_list.index(maxval):len(a)]):
        if y != '1':
            a[x] = '1'

target_list2 = []
for i, n in enumerate(a):
    cnt = 0
    if i != '0':
        target_list2.append(cnt)
        break
    else:
        for j, m in enumerate(a[i + 1:len(a)]):
            init = True
            if m == '0' and init:
                cnt += 1
                break
            if m == '1' and init:
                cnt += 1
                init = False
        for k, l in enumerate(reversed(a[:i + 1])):
            if l == '1':
                cnt += 1
print(target_list2)
print(max(target_list2))


q, k = list(map(int, input().split()))
list = [int(i) for i in input()]


sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

print(sum01)
Add = 2 * k + 1
answer = 0
for i in range(0, len(sum01), 2):
    temp = 0
    left = i
    right = min(i + Add, len(sum01))
    for j in range(left, right, 1):
        temp += sum01[j]
    answer = max(temp, answer)

print(answer)

'''

q, k = list(map(int, input().split()))
list = [int(i) for i in input()]


sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

# print(sum01)
Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(sum01):
    ruiseki.append(ruiseki[i] + sum01[i])


answer = 0
for i in range(0, len(ruiseki), 2):
    left = i
    right = min(i + Add, len(sum01))
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)

N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(s_count_list[i] + s)

for i in range(0,len(ruisekiwa),2):
    left = i
    right = min((i + (2 * K) + 1),len(ruisekiwa))
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)

q, k = list(map(int, input().split()))
list = [int(i) for i in input()]


sum01 = []
pre = 0
cnt = 1
# 111010
# 001010
for i, e in enumerate(list):
    if i == len(list) - 1:
        sum01.append(cnt)
        break;
    if e != list[i + 1]:
        sum01.append(cnt)
        cnt = 1
    else:
        cnt += 1
#print(sum01)

ruisekiwa = []
for i, e in enumerate(sum01):
    if i == 0:
        ruisekiwa.append(e)
    else:
        ruisekiwa.append(ruisekiwa[i - 1] + e)

#print(ruisekiwa)

result = 1
for i, e in enumerate(ruisekiwa):
    if 2 * i + 1 > len(ruisekiwa) - 1:
        break;
    last = 2 * (i - k) if 2 * (i - k) > 0 else 0
    if list[0] == 1:
        if last == 0:
            result = max(result, ruisekiwa[2 * i + 1] - 0)
        result = max(result, ruisekiwa[2 * i + 1] - ruisekiwa[last])
    if list[0] == 0:
        if last == 0:
            result = max(result, ruisekiwa[2 * i] - 0)
        result = max(result, ruisekiwa[2 * i] - ruisekiwa[last])

print(result)

N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
    s_count_list.insert(0,0)
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(ruisekiwa[i] + s)

if len(ruisekiwa) %2 ==0:
    ruisekiwa.append(0)

answer =  1
#print(s_count_list)

for i in range(0,len(s_count_list),2):
    left = i
    right = min((i + (2 * K) + 1),len(s_count_list))
    #print(left,right)
    #print(ruisekiwa[right])
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)





Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Accepted