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 = str(input())

print(N)
print(K)

# make seq_cnt_list
seq_cnt_list = []
seq_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        #print("S[%d]=%s is change" % (i, S[i]))
        if seq_cnt > 0:
            if last_man == "0":
                seq_cnt *= -1
            seq_cnt_list.append(seq_cnt)
        seq_cnt = 1
    else:
        #print("S[%d]=%s is same" % (i, S[i]))
        seq_cnt += 1
    last_man = cur_man
if seq_cnt > 0:
    if last_man == "0":
        seq_cnt *= -1
    seq_cnt_list.append(seq_cnt)

print(seq_cnt_list)

for k in range(K):
    seq_cnt_list_size = len(seq_cnt_list)
    if (seq_cnt_list_size < 2) or (seq_cnt_list_size == 3 and seq_cnt_list[0] > 0):
        seq_cnt_list = [sum(map(abs, seq_cnt_list))]
        break
    else:
        max_pos = 0
        max_val = 0
        for i in range(seq_cnt_list_size):
            if seq_cnt_list[i] < 0:
                val = seq_cnt_list[i] * -1
                if i > 0:
                    val += seq_cnt_list[i - 1]
                if i < seq_cnt_list_size - 1:
                    val += seq_cnt_list[i + 1]
                if val > max_val:
                    max_val = val
                    max_pos = i
        start_pos = max_pos - 1 if max_pos > 0 else max_pos
        end_pos = max_pos + 1 if max_pos < seq_cnt_list_size - 1 else max_pos
        seq_cnt_list[start_pos: end_pos + 1] = [max_val]

print(max(seq_cnt_list))








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

# make seq_cnt_list
seq_cnt_list = []
seq_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        #print("S[%d]=%s is change" % (i, S[i]))
        if seq_cnt > 0:
            if last_man == "0":
                seq_cnt *= -1
            seq_cnt_list.append(seq_cnt)
        seq_cnt = 1
    else:
        #print("S[%d]=%s is same" % (i, S[i]))
        seq_cnt += 1
    last_man = cur_man
if seq_cnt > 0:
    if last_man == "0":
        seq_cnt *= -1
    seq_cnt_list.append(seq_cnt)

#print(seq_cnt_list)

print(max(seq_cnt_list))








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

# count sequence man and memo the cnt in group
man_groups = []
same_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        if same_cnt > 0:
            if last_man == "0":
                same_cnt *= -1
            man_groups.append(same_cnt)
        same_cnt = 1
    else:
        same_cnt += 1
    last_man = cur_man
if same_cnt > 0:
    if last_man == "0":
        same_cnt *= -1
    man_groups.append(same_cnt)

# count max sequence man from evey pos
sum_list = []
end_pos = 2 * K if man_groups[0] > 0 else 2 * K - 1
max_man_groups_idx = len(man_groups) - 1
end_pos = min(end_pos, max_man_groups_idx)
last_max = sum(map(abs, man_groups[0: end_pos + 1]))
sum_list.append(last_max)

start_pos = 2 if man_groups[0] > 0 else 1
for i in range(start_pos, len(man_groups), 2):
    if end_pos >= max_man_groups_idx:
        break
    for j in range(2):
        idx = i - j - 1
        if idx >= 0:
            last_max -= abs(man_groups[idx])
    for j in range(2):
        end_pos += 1
        if end_pos <= max_man_groups_idx:
            last_max += abs(man_groups[end_pos])
    sum_list.append(last_max)

#print(man_groups)
#print(sum_list)
print(max(sum_list))







Verdicts: Wrong Answer

Wrong Answer

Accepted