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()
intS = int(S)

calc_list = [1 if S[0] == '0' else 0]
for i in range(1, len(S)):
    if S[i] == '0':
        if S[i-1] == '1':
            calc_list.append(1)
        elif S[i-1] == '0':
            calc_list.append(calc_list[i-1] + 1)
    else:
        calc_list.append(0)

print(calc_list)

def zeros(calc_list):
    zero_list = [1 if calc_list[0] == 0 else 0]
    for i in range(1, len(calc_list)):
        if calc_list[i] == 0:
            zero_list.append(zero_list[i-1] + 1)
        else:
            zero_list.append(0)

    return zero_list


max_times = max(calc_list)
for _ in range(K):
    for i in range(len(calc_list)):
        if calc_list.count(max_times) == 1:
            if calc_list[i] == max_times:
                for j in range(max_times):
                    calc_list[i-j] = 0
                break
        else:
            zero_list = zeros(calc_list))
            index = zero_list.index(max(zero_list))

    print('calc', calc_list)
    print('zeros', zeros(calc_list))
    max_times = max(calc_list)


N, K = map(int, input().split())
S = input()



now = 1
cnt = 0
nums = []
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        nums.append(cnt)
        cnt = 1
        now = 1 - now
nums.append(cnt)
if int(S[-1]) == 0:
    nums.append(0)

print(nums)

cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)

print(cumulative_sum)

ans = 0
start = 0
final = 2 * K
answers = []


if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        print(start, final, ans)
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[0])

print(answers)


N, K = map(int, input().split())
S = input()



now = 1
cnt = 0
nums = []
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        nums.append(cnt)
        cnt = 1
        now = 1 - now
nums.append(cnt)
if int(S[-1]) == 0:
    nums.append(0)

# print(nums)

cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)



ans = 0
start = 0
final = 2 * K
answers = []


# if M < 2 * K + 1:
#     print(len(S))
# else:
if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        # print(start, final, ans)
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[-1])

# print(answers)


Verdicts: Runtime Error

Wrong Answer

Accepted