Problem p03074 (python)

Fixed submission:
n, k = map(int, input().split())
s = [int(x) for x in list(input())]

state = 1
cnt = 0
l = []
for e in s:
    if e != state:
        l.append(cnt)
        cnt = 1
        state = e
    else:
        cnt += 1

if cnt != 0:
    l.append(cnt)


if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)

print(answer)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().strip().split())
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
# print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
cumsum = []
for i in range(len(cnts)):
    if i == 0:
        cumsum.append(cnts[0])
    else:
        cumsum.append(cumsum[-1] + cnts[i])

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 2)
        end = min(inx + 2 * k -1 , len(cnts))
        tmp = cumsum[end] - cumsum[start]
        ans = max(tmp, ans)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
  if s[i] == tmp:
    cnt += 1
    else: # here tmp should be cleared
      cnts.append(cnt)
      cnt = 1
      tmp = s[i]
      cnts.append(cnt)
      print(cnts)

      n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
      # print("# of zeros block: ", n_zero)
      if n_zero <= k:
        ans = sum(cnts)
        else:
          ans = 0
          for inx in range(flag, len(cnts), 2):
            start = max(0, inx - 1)
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[start: end])
            ans = max(tmp, ans)

            print(ans)

Buggy submission (verdict: Runtime Error):
# _*_ coding: utf-8 _*_
while True:
    n, k = [int(e) for e in input().strip().split(' ')]
    s = input().strip()

    cnts = []
    ans = 0
    # denote whether the first block is 0 or 1
    tmp, cnt = s[0], 0
    flag = int(s[0])

    for i in range(n):
        if s[i] == tmp:
            cnt += 1
        else: # here tmp should be cleared
            cnts.append(cnt)
            cnt = 1
        tmp = s[i]
    cnts.append(cnt)

    n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

    if n_zero <= k:
         ans = sum(cnts)
    else:
        ans = 0
        for inx in range(flag, len(cnts), 2):
            start = max(0, inx - 1)
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[start: end])
            if tmp > ans:
                ans = tmp

    print(ans)

Buggy submission (verdict: Runtime Error):
# _*_ coding: utf-8 _*_

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)

Buggy submission (verdict: Runtime Error):
# _*_ coding: utf-8 _*_

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)


n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)

else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = min(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)