Problem p03074 (python)

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

before = S[0]
cnt = 0
same_nums = []
if S[0] == '0':
  same_nums.append(0)
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[-1] == '0':
  same_nums.append(0)
  
sum_nums = sum(same_nums[:2*K+1])
ans = sum_nums
for i in range(0, len(same_nums)-2*K, 2):
  sum_nums = sum_nums - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
  ans = max(ans, sum_nums)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
    ans = max(ans, sum_temp)
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  sum_temp = sum_temp - same_nums[1] + sum(same_nums[2*K:2*K+2])
  ans = max(ans, sum_temp)
  for i in range(3, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*K+2])
    ans = max(ans, sum_temp)
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

before = S[0]
zeros = []
ones = []
zero_cnt = 0
one_cnt = 0
for char in S:
  if char == before:
    if char == '0':
      zero_cnt += 1
    else:
      one_cnt += 1
    before = char
  else:
    if char == '0':
      ones.append(one_cnt)
      zero_cnt = 1
    else:
      zeros.append(zero_cnt)
      one_cnt = 1
    before = char
if S[-1] == '0':
  zeros.append(zero_cnt)
elif S[-1] == '1':
  ones.append(one_cnt)
if len(zeros) <= K:
  print(N)
else:
  if S[1] == '0':
    sum_temp = sum(zeros[:K]) + sum(ones[:K])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)
  else:
    sum_temp = sum(zeros[:K]) + sum(ones[:K+1])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K+1]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
R = []
before = S[0]
cnt_zero = 0
cnt_one = 0
len_l = 0
len_r = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt_zero += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt_zero)
        len_l += 1
        cnt_zero = 0
        cnt_one += 1
    elif before == '1' and S[i] == '0':
        before = '0'
        R.append(cnt_one)
        len_r += 1
        cnt_zero += 1
        cnt_one = 0
    else:
    	cnt_one += 1
if cnt_zero > 0:
    L.append(cnt_zero)
    len_l += 1

if cnt_one > 0:
	R.append(cnt_one)
	len_r += 1
ans = 0
sum_one = 0
if S[0] == '0':
	sum_one = sum(L[:K]) + sum(R[:K])
else:
	sum_one = sum(L[:K]) + sum(R[:K+1])

ans = sum_one
for i in range(len_l)[1:]:
    if i + K > len_l:
        break
    elif i + K == len_l:
        if i + K > len_r:
            sum_one += (L[K+i-1] - L[i-1] - R[i-1])
            ans = max(ans, sum_one)
            break
        else:
            sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
            ans = max(ans, sum_one)
            break
    if S[0] == '0' and i == 1:
        sum_one += (L[K] + R[K] - L[0])
    else:
        sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
    ans = max(ans, sum_one)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
R = []
before = S[0]
cnt_zero = 0
cnt_one = 0
len_l = 0
len_r = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt_zero += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt_zero)
        len_l += 1
        cnt_zero = 0
        cnt_one += 1
    elif before == '1' and S[i] == '0':
        before = '0'
        R.append(cnt_one)
        len_r += 1
        cnt_zero += 1
        cnt_one = 0
    else:
    	cnt_one += 1
if cnt_zero > 0:
    L.append(cnt_zero)
    len_l += 1

if cnt_one > 0:
	R.append(cnt_one)
	len_r += 1
ans = 0
sum_one = 0
if S[0] == '0':
	sum_one = sum(L[:K]) + sum(R[:K])
else:
	sum_one = sum(L[:K]) + sum(R[:K+1])

ans = sum_one
for i in range(len_l)[1:]:
    if i + K > len_l:
        break
    elif i + K == len_l:
        if i + K > len_r:
            sum_one += (L[K+i-1] - L[i-1] - R[i-1])
            ans = max(ans, sum_one)
            break
    if S[0] == '0' and i == 1:
        sum_one += (L[K] + R[K] - L[0])
    else:
        sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
    ans = max(ans, sum_one)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
R = []
before = '1'
cnt_zero = 0
cnt_one = 0
len_l = 0
len_r = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt_zero += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt_zero)
        len_l += 1
        cnt_zero = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        R.append(cnt_one)
        cnt_zero += 1
        cnt_one = 0
	else:
    	cnt_one += 1
      
if cnt_zero != 0:
    L.append(cnt_zero)
    len_l += 1

if cnt_one != 0:
	R.append(cnt_one)
	len_r = 0
ans = 0
sum_one = 0
if S[0] == '0'
	sum_one = sum(L[:K]) + sum(R[:K])
else:
	sum_one = sum(L[:K]) + sum(R[:K+1])

ans = sum_one

for i in range(len_l)[1:]:
  	if i + K > len_r:
		break
	if S[0] == '0' and i == 1:
		sum_one += (L[K] + R[K] - L[0])
    else:
		sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
	ans = max(ans, sum_one)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
R = []
before = '1'
cnt_zero = 0
cnt_one = 0
len_l = 0
len_r = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt_zero += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt_zero)
        len_l += 1
        cnt_zero = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        R.append(cnt_one)
        cnt_zero += 1
        cnt_one = 0
	else:
    	cnt_one += 1
      
if cnt_zero != 0:
    L.append(cnt_zero)
    len_l += 1

if cnt_one != 0:
	R.append(cnt_one)
	len_r = 0
ans = 0
sum_one = 0
if S[0] == '0'
	sum_one = sum(L[:K]) + sum(R[:K])
else:
	sum_one = sum(L[:K]) + sum(R[:K+1])

ans = sum_one

for i in range(len_l)[1:]:
  	if i + K > len_r:
		break
	if S[0] == '0' and i == 1:
      sum_one += (L[K] + R[K] - L[0])
    else:
      sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
	ans = max(ans, sum_one)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt)
        len_l += 1
        cnt = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        cnt += 1
if cnt != 0:
    L.append(cnt)
    n_l += 1

L.sort(reverse=True)

if len_l > K:
    print(sum(L[:K])
else:
    print(sum(L))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
  if before == '0' and S[i] == '0':
  	cnt += 1
  elif before == '0' and S[i] == '1':
  	before = '1'
    L.append(cnt)
    len_l += 1
    cnt = 0
  elif before == '1' and S[i] == '0':
    before == '0'
    cnt += 1
if cnt != 0:
  L.append(cnt)
  len_l += 1

L.sort(reverse=True)

if len_l > K:
  print(sum(L[:K])
else:
  print(sum(L))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
  if before == '0' and S[i] == '0'
  	cnt += 1
  elif before == '0' and S[i] == '1'
  	before = '1'
    L.append(cnt)
    len_l += 1
    cnt = 0
  elif before == '1' and S[i] == '0'
    before == '0'
    cnt += 1
if cnt != 0:
  L.append(cnt)
  len_l += 1

L.sort(reverse=True)

if len_l > K:
  print(sum(L[:K])
else:
  print(sum(L))