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):
import math
N, K = map(int, input().split())
S = input()


cnt_list = []
flag = True if S[0] == '1' else False
cnt = 1
flag_flag = True
for i in range(1, N):
    if S[i] == '1':
        if i == N - 1:
            flag_flag = True
        if flag:
            cnt += 1
        else:
            flag = True
            cnt_list.append([cnt, -1])
            cnt = 1
    if S[i] == '0':
        if i == N - 1:
            flag_flag = False
        if flag:
            flag = False
            cnt_list.append([-1, cnt])
            cnt = 1
        else:
            cnt += 1
if not flag_flag:
    cnt_list.append([cnt, -1])
else:
    cnt_list.append([-1, cnt])

n = len(cnt_list)
if cnt_list[0][0] == -1:
    if K >= math.ceil(n/2):
        print(N)
        exit()
    k = K
else:
    if K >= n//2:
        print(N)
        exit()
    k = K



cumsum = [0] * (n+1)
for i in range(n):
    cumsum[i+1] = cumsum[i] + max(cnt_list[i])

ans = 0
if cnt_list[0][0] == -1:        # 1 start
    if cnt_list[-1][0] == -1:   # 1 end
        for i in range(1, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                        # 0 end
        for i in range(1, n - 2*k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])
    
else:                           # 0 start
    if cnt_list[-1][0] == -1:   # 1 end
        ans = cumsum[2*k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                       # 0 end
        ans = cumsum[2 * k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[ 2* k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])
##print(cumsum)
print(ans)

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


cnt_list = []
flag = True if S[0] == '1' else False
cnt = 1
flag_flag = True
for i in range(1, N):
    if S[i] == '1':
        if i == N - 1:
            flag_flag = True
        if flag:
            cnt += 1
        else:
            flag = True
            cnt_list.append([cnt, -1])
            cnt = 1
    if S[i] == '0':
        if i == N - 1:
            flag_flag = False
        if flag:
            flag = False
            cnt_list.append([-1, cnt])
            cnt = 1
        else:
            cnt += 1
if not flag_flag:
    cnt_list.append([cnt, -1])
else:
    cnt_list.append([-1, cnt])

n = len(cnt_list)
if cnt_list[0][0] == -1:    
    if K >= n//2:
        print(N)
        exit()
    k = K
else:                       
    if K >= math.ceil(n/2):
        print(N)
        exit()
    k = K



cumsum = [0] * (n+1)
for i in range(n):
    cumsum[i+1] = cumsum[i] + max(cnt_list[i])

ans = 0
if cnt_list[0][0] == -1:        # 1 start
    if cnt_list[-1][0] == -1:   # 1 end
        for i in range(1, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                        # 0 end
        for i in range(1, n - 2*k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])
    
else:                           # 0 start
    if cnt_list[-1][0] == -1:   # 1 end
        ans = cumsum[2*k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                       # 0 end
        ans = cumsum[2 * k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[ 2* k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])
##print(cumsum)
print(ans)

Verdicts: Wrong Answer

Accepted