Problem p03074 (python)

Fixed submission:
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)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
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     ## 各累積和を一時的に格納する変数


## 最初の累積和を計算する処理
## 先頭が0か1かによって処理を分割している
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)):
  ## 先頭が0か1かによって処理を分割している
  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))

Buggy submission (verdict: Runtime Error):
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)