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 re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''


for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)



count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = []
handstands = 0


if ('0' in teams_group[0]):
#  print('start is 0')
  handstands=len(teams_group[0])
  k=1
  for k in range((K-1)*2+1):
    handstands += len(teams_group[k])
else:
#  print('start is 1')
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)

for i in range(1,len(teams_group)-K+1):

  handstands = sum_handstand[i-1] - len(teams_group[i-1]) + len(teams_group[i+K-1])
  sum_handstand.append(handstands)

print(sum_handstand)

def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    #print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        #print(f'{i} : {c}')
        c_a.append(c)

    return max(c_a)

def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        print(f'{i} : {c}')
        c_a.append(c)

    return max(c_a)

import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''


for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)



count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] 
handstands = 0     




if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)


for i in range(1,len(teams_group)-(K*2)):
  
  if ('0' in teams_group[i]):
    handstands = sum_handstand[i-1]
  else:
    if i == 1:
      handstands = sum_handstand[i-1] - len(teams_group[i-1]) + (len(teams_group[i+2*K-1])+len(teams_group[i+2*K]))
    else:
      handstands = sum_handstand[i-1] - (len(teams_group[i-1])+len(teams_group[i-2])) + (len(teams_group[i+2*K-1])+len(teams_group[i+2*K]))
  sum_handstand.append(handstands)

handstands = 0

if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    handstands += len(teams_group[len(teams_group)-2*K+k])
  sum_handstand.append(handstands)


#print(sum_handstand)
print(max(sum_handstand))

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted