Problem p03074 (python)

Fixed submission:
N, K = map(int, input().split())
s = input()
  
group = []
if s[0] == '0' :
  group = [0]
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]
if s[-1] == '0':
  group += [0]

m = len(group)//2
if K >= m:
  print(N)
else:
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i]  - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K+2]]
  
  print(max(score))

Buggy submission (verdict: Runtime Error):
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i]  - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K]]
  else :
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K]]
    score[-1] += group[-1]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Buggy submission (verdict: Runtime Error):
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
  else :
    0/0
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
    score[-1] += group[-1]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Buggy submission (verdict: Runtime Error):
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
  else :
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
    score[-1] += group[-1]
else:
  0/0
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0':
  score = [sum(group[:2*K])]
  m = (len(group)+1)//2
  for i in range(m-K):
    score += [score[-1] - group[2*i] + group[2*i+2*K]]
else:
  score = [sum(group[:2*K+1])]
  m = len(group)//2
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))