Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03044 (python)

Fixed submission:
from collections import defaultdict


class Solution:
    def main(self):
        N = int(input())
        self.hashmap = defaultdict(set)
        for i in range(N-1):
            u, v, w = map(int, input().split())
            self.hashmap[u-1].add((v-1, w))
            self.hashmap[v-1].add((u-1, w))
        # print(self.hashmap)
        self.colors = [None] * N
        for i in range(N):
            stack = [(i, 0)]
            while stack:
                idx, color = stack.pop()
                if self.colors[idx] == None:
                    self.colors[idx] = color
                    for node, weight in self.hashmap[idx]:
                        if weight % 2 == 0:
                            stack.append((node, color))
                        else:
                            stack.append((node, 1 ^ color))
        for i in range(N):
            print(self.colors[i])

    def dfs(self, i, color):
        if self.colors[i] is None:
            self.colors[i] = color
            for node, weight in self.hashmap[i]:
                if weight % 2 == 0:
                    self.dfs(node, color)
                else:
                    self.dfs(node, 1 ^ color)


if __name__ == '__main__':
    Solution().main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict


class Solution:
    def main(self):
        N = int(input())
        self.hashmap = defaultdict(set)
        for i in range(N-1):
            u, v, w = map(int, input().split())
            self.hashmap[u-1].add((v-1, w))
            self.hashmap[v-1].add((u-1, w))
        # print(self.hashmap)
        self.colors = [None] * N
        for i in range(N):
            try:
                self.dfs(i, 0)
                print(self.colors[i])
            except IndexError:
                print('Error')
                return

    def dfs(self, i, color):
        if self.colors[i] is None:
            self.colors[i] = color
            for node, weight in self.hashmap[i]:
                if weight % 2 == 0:
                    self.dfs(node, color)
                else:
                    self.dfs(node, 1 ^ color)


if __name__ == '__main__':
    Solution().main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

class Solution:
    def main(self):
        N = int(input())
        self.hashmap = defaultdict(set)
        for i in range(N-1):
            u, v, w = map(int, input().split())
            self.hashmap[u-1].add((v-1, w))
            self.hashmap[v-1].add((u-1, w))
        self.colors = [None] * N
        for i in range(N):
            self.dfs(i, 0)
            print(self.colors[i])

    def dfs(self, i, color):
        if self.colors[i] is None:
            self.colors[i] = color
            for node, weight in self.hashmap[i]:
                if weight % 2 == 0:
                    self.dfs(node, color)
                else:
                    self.dfs(node, 1 ^ color)


if __name__ == '__main__':
    Solution().main()
Problem p03160 (python)

Fixed submission:
from functools import lru_cache

N = int(input())
h = list(map(int, input().split()))

cost = [None] * N
cost[0] = 0
cost[1] = abs(h[1]-h[0])
for i in range(2, N):
  cost[i] = min(cost[i-1]+abs(h[i]-h[i-1]), cost[i-2]+abs(h[i]-h[i-2]))
print(cost[N-1])
# print(cost)

"""
@lru_cache(maxsize=None)
def rec(n):
  # print(n)
  if n == 1:
    return 0
  elif n == 2:
    return abs(h[1]-h[0])
  else:
    return min(rec(n-1)+abs(h[n-1]-h[n-2]), rec(n-2)+abs(h[n-1]-h[n-3]))

print(rec(N))
"""

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

import sys
sys.setrecursionlimit(10000)

N = int(input())
h = list(map(int, input().split()))

@lru_cache(maxsize=None)
def rec(n):
  # print(n)
  if n == 1:
    return 0
  elif n == 2:
    return abs(h[1]-h[0])
  else:
    return min(rec(n-1)+abs(h[n-1]-h[n-2]), rec(n-2)+abs(h[n-1]-h[n-3]))

print(rec(N))

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

N = int(input())
h = list(map(int, input().split()))

@lru_cache(maxsize=None)
def rec(n):
  # print(n)
  if n == 1:
    return 0
  elif n == 2:
    return abs(h[1]-h[0])
  else:
    return min(rec(n-1)+abs(h[n-1]-h[n-2]), rec(n-2)+abs(h[n-1]-h[n-3]))

print(rec(N))
Problem p03339 (python)

Fixed submission:
n = int(input())
s = input()

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

for i in range(1, n):
    w[i] = w[i - 1] + 1 if s[i - 1] == 'W' else w[i - 1]
    e[-i - 1] = e[-i] + 1 if s[-i] == 'E' else e[-i]

res = [0 for _ in range(n)]

for i in range(n):
    res[i] = w[i] + e[i]

print(min(res))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

for i in range(1, n):
    w[i] = w[i - 1] + 1 if s[i - 1] == 'W' else w[i - 1]
    e[-i - 1] = e[-i] + 1 if s[-i] == 'E' else e[-i]

res = [0 for _ in range(n)]

for i in range(n):
    res = w[i] + e[i]

print(min(res))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()

e = [0 for _ in range(n)]
w = [0 for _ in range(n)]

for i in range(1, n):
    w[i] = w[i - 1] + 1 if S[i - 1] == 'W' else w[i - 1]
    e[-i - 1] = e[-i] + 1 if S[-i] == 'E' else e[-i]

res = [0 for _ in range(n)]

for i in range(n):
    res = w[i] + e[i]

print(min(res))
Problem p03353 (python)

Fixed submission:
import string


lowercase = string.ascii_lowercase

s = input()
K = int(input())

substrings = []

stack = list(lowercase)[::-1]

while len(substrings) < K:
    # print(stack)
    v = stack.pop()
    if v in s:
        substrings.append(v)
        for c in list(lowercase)[::-1]:
            stack.append(v+c)

print(substrings[K-1])

Buggy submission (verdict: Runtime Error):
import string


lowercase = string.ascii_lowercase

s = input()
K = int(input())

substrings = []

stack = list(lowercase)[::-1]

while len(substrings) < 5:
    # print(stack)
    v = stack.pop()
    if v in s:
        substrings.append(v)
        for c in list(lowercase)[::-1]:
            stack.append(v+c)

print(substrings[K-1])
Problem p02996 (python)

Fixed submission:
def dec(X, d):
    X[1] -= d
    return X


N = int(input())
task = [list(map(int, input().split())) for _ in range(N)]

task.sort(key=lambda x: x[1])
time = 0
for i in range(N):
    time += task[i][0]
    if time > task[i][1]:
        print("No")
        exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
C:\Users\ryo-2\Anaconda3\python.exe C:/Users/ryo-2/PycharmProjects/atcoder/ABC131/ABC131_D.py
30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720
Yes

Process finished with exit code 0
Problem p03007 (python)

Fixed submission:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = deque(A)
if A[0] <= 0 and A[-1] >= 0:
    print(sum(map(abs, A)))
    while len(d) > 2:
        while d[1] < 0:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        if d:
            print(x, y)
        else:
            print(y, x)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

elif A[0] > 0:
    print(sum(A) - 2 * A[0])
    x = d.popleft()
    y = d.pop()
    if not d:
        print(y, x)
    else:
        print(x, y)
    d.appendleft(x - y)
    while len(d) > 2:
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
else:
    print(-sum(A) + 2 * A[-1])
    x = d.pop()
    y = d.popleft()
    print(x, y)
    d.append(x - y)
    while len(d) > 2:
        while d[1] < 0 and len(d) > 2:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.pop()
        y = d.popleft()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

Buggy submission (verdict: Runtime Error):
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = deque(A)
if A[0] <= 0 and A[-1] > 0:
    print(sum(map(abs, A)))
    while len(d) > 2:
        while d[1] < 0 and len(d)>2:
            print(list(d))
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

elif A[0] > 0:
    print(sum(A) - 2 * A[0])
    x = d.popleft()
    y = d.pop()
    print(x, y)
    d.appendleft(x - y)
    while len(d) > 2:
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
else:
    print(-sum(A) + 2 * A[-1])
    x = d.pop()
    y = d.popleft()
    print(x, y)
    d.append(x - y)
    while len(d) > 2:
        while d[1] < 0 and len(d) > 2:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.pop()
        y = d.popleft()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
Problem p03061 (python)

Fixed submission:
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)


N = int(input())
A = list(map(int, input().split()))

L = [0] * (N + 1)
L[0] = 0
R = [0] * (N + 1)
R[0] = 0

for i in range(N):
    L[i + 1] = gcd(L[i], A[i])
    R[i + 1] = gcd(R[i], A[-i - 1])

ans = 0
for i in range(N):
    ans = max(ans, gcd(L[i], R[N - i - 1]))
print(ans)

Buggy submission (verdict: Runtime Error):
import functools
import math

N = int(input())
A = list(map(int, input().split()))

A.sort()
ans = 1

for i in range(N):
    B = A[:]
    B.pop(i)
    gcd = functools.reduce(math.gcd, B)
    ans = max(ans, gcd)

print(ans)

Buggy submission (verdict: Runtime Error):
import functools
import math
import copy

N = int(input())
A = list(map(int, input().split()))

A.sort()
ans = 1

for i in range(N):
    B = copy.copy(A)
    B.pop(i)
    gcd = functools.reduce(math.gcd, B)
    ans = max(ans, gcd)

print(ans)
Problem p03161 (python)

Fixed submission:
import sys
import numpy as np

input = sys.stdin.readline

N, K = map(int, input().split())
h = np.array(list(map(int, (input().split()))))

dp = np.full(N,sys.maxsize,dtype=int)

dp[0] = 0

for i in range(1, N):
    s = max(0, i - K)
    dp[i] = min(dp[s:i] + abs(h[i] - h[s:i]))

print(dp[N - 1])

Buggy submission (verdict: Runtime Error):
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
h = list(map(int, (input().split())))

dp = [sys.maxsize] * N

dp[0] = 0

for i in range(1, N):
    s = max(0, i - K)
    dp[i] = min(dp[s:i] + abs(h[s:i] - h[i]))

print(dp[N - 1])
Problem p03574 (python)

Fixed submission:
H, W = map(int, input().split())
S = ["." * (W + 2)]
for _ in range(H):
    S += ["." + input() + "."]
S += ["." * (W + 2)]

ans = [[-1] * W for _ in range(H)]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == "#":
            continue
        cnt = 0
        if S[i - 1][j - 1] == "#":
            cnt += 1
        if S[i][j - 1] == "#":
            cnt += 1
        if S[i + 1][j - 1] == "#":
            cnt += 1
        if S[i - 1][j] == "#":
            cnt += 1
        if S[i + 1][j] == "#":
            cnt += 1
        if S[i - 1][j + 1] == "#":
            cnt += 1
        if S[i][j + 1] == "#":
            cnt += 1
        if S[i + 1][j + 1] == "#":
            cnt += 1
        ans[i - 1][j - 1] = cnt

for i in ans:
    p = ["#" if x == -1 else x for x in i]
    print(*p, sep="")

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [["."] * (W + 2)]
for _ in range(H):
    S.append([".", *list(input()), "."])
S.append(["."] * (W + 2))

ans = [[-1] * W for _ in range(H)]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == "#":
            continue
        cnt = 0
        if S[i - 1][j - 1] == "#":
            cnt += 1
        if S[i][j - 1] == "#":
            cnt += 1
        if S[i + 1][j - 1] == "#":
            cnt += 1
        if S[i - 1][j] == "#":
            cnt += 1
        if S[i + 1][j] == "#":
            cnt += 1
        if S[i - 1][j + 1] == "#":
            cnt += 1
        if S[i][j + 1] == "#":
            cnt += 1
        if S[i + 1][j + 1] == "#":
            cnt += 1
        ans[i - 1][j - 1] = cnt

for i in ans:
    p = ["#" if x == -1 else x for x in i]
    print(*p, sep="")
Problem p03608 (python)

Fixed submission:
import itertools
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
r = list(map(lambda x: x - 1, r))
d = [[10 ** 10] * N for _ in range(N)]

for _ in range(M):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

for i in range(N):
    d[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

ans = 10 ** 10

for i in itertools.permutations(r):
    dist = 0
    for j in range(R - 1):
        dist += d[i[j]][i[j + 1]]
    ans = min(ans, dist)

print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
import numpy
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
r = list(map(lambda x: x - 1, r))
d = numpy.array([[10 ** 10] * N for _ in range(N)])

for _ in range(M):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c

for i in range(N):
    d[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

ans = 10 ** 10

for i in itertools.permutations(r):
    dist = 0
    for j in range(R - 1):
        dist += d[i[j]][i[j + 1]]
    ans = min(ans, dist)

print(ans)
Problem p03206 (python)

Fixed submission:
print("Christmas"+" Eve"*(25-int(input())))

Buggy submission (verdict: Runtime Error):
print("Christmas"+" Eve"*(25-int(input()))
Problem p03243 (python)

Fixed submission:
n=int(input())
print((((n-1)//111)+1)*111)

Buggy submission (verdict: Runtime Error):
n=int(input()
print((((n-1)//111)+1)*111)

Buggy submission (verdict: Runtime Error):
n=int(input()
print(((n-1)//111)*111+111)

Buggy submission (verdict: Runtime Error):
n=int(input()
print(((n-1)//111)*111+111)

Buggy submission (verdict: Runtime Error):
n=int(input()
print((n-1)//111+111))
Problem p03288 (python)

Fixed submission:
print(["ABC","ARC","AGC"][int(input())//50+8>>5])

Buggy submission (verdict: Runtime Error):
n=int(input())
if(n<1200):
  print("ABC")
elif(n<2800):
  print("ARC"):
else:
  print("AGC")

Buggy submission (verdict: Runtime Error):
n=int(input())
if(n<1200):
  print("ABC")
elif(n<2800):
  print("ARC"):
elif(n>=2800):
  print("AGC")
Problem p03399 (python)

Fixed submission:
a,b,c,d=[int(input()) for _ in range(4)]
print(min(a,b)+min(c,d))

Buggy submission (verdict: Runtime Error):
a,b,c,d = [int(input()) for_ in range(4)]
print(min(a,b)+min(c,d))

Buggy submission (verdict: Runtime Error):
a,b,c,d = [int(input()) for_ range(4)]
print(min(a,b)+min(c,d))
Problem p03407 (python)

Fixed submission:
a,b,c=map(int,input().split())
print("Yes" if a+b>=c else "No")

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input())
print("Yes" if a+b>=c else "No")
Problem p03416 (python)

Fixed submission:
a,b =map(int,input().split())
print(sum(i==i[::-1] for i in map(str,range(a,b+1))))

Buggy submission (verdict: Runtime Error):
a,b =map(int,input().split())
print(sum(i==i[::-1] for i in map(str,range(a,b+1)))
Problem p03563 (python)

Fixed submission:
n=int(input())
m=int(input())
print(2*m-n)

Buggy submission (verdict: Runtime Error):
n=int(input())
m=int(input())
print(2m-n)
Problem p03573 (python)

Fixed submission:
a,b,c=sorted(map(int,input().split()))
print(c if a==b else a)

Buggy submission (verdict: Runtime Error):
a,b,c=sorted(map(int,input().split()))
if("c" a==b else "a")
Problem p03574 (python)

Fixed submission:
h,w=map(int,input().split())
s=[input() for _ in range(h)]
for i in range(h):
    l=""
    for j in range(w):
        if s[i][j]=="#":
            l+="#"
        else:
            l+=str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
    print(l)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[int for _ in range (h)]
for i in range(h):
  l=""
  for j in range(w):
    if s[i][j]=="#":
      l+="#"
    else:
      l+=str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
print(l)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[int for _ in range (h)]
for i in range(h):
  l=""
  for j in range(w):
    if s[i][j]=="#":
      l+="#"
    else:
      l+=str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
print(l)
Problem p03632 (python)

Fixed submission:
a,b,c,d=map(int,input().split())
if(b<=c or d<=a):
  print(0)
else:
  print(abs(min(b,d)-max(a,c)))

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int.input().split())
print(max(0,min(b,d)-max(a,c)))

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int.input().split())
print(min(b,d)-(max(a,c))
Problem p03773 (python)

Fixed submission:
print(sum(map(int,input().split()))%24)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
print(sum(a+b)%24)
Problem p03826 (python)

Fixed submission:
a,b,c,d=map(int,input().split())
print(max(a*b,c*d))

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
print(max(a*b,c*d)
Problem p02887 (python)

Fixed submission:
n=int(input())
s=input()
a=1
for i in range(n-1):
  if s[i]!=s[i+1]:
    a+=1
print(a)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
a=1
for i in range(n):
  if s[i]!=s[i+1]:
    a+=1
print(a)
Problem p02911 (python)

Fixed submission:
N,K,Q = map(int,input().split())
a = [0 for i in range(N)]
for i in range(Q):
  a[int(input())-1]+=1
for i in a:
  if K-Q+i>0:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,K,Q = map(int,input().split())
a = [0 for i in range(N)]
for i in range(Q):
  a[int(input())-1]+=1
for i in range(a):
  if K-Q+i>0:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,K,Q = map(int,input().split())
a=[]
for i in range(Q):
  a.append(int(input()))
for i in range(N):
  if 0<K-Q+a.count(i+1):
	print("Yes")
  else:
	print("No")
Problem p03044 (python)

Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(1000000)

G = defaultdict(list)

N = int(input())
C = [0 for i in range(N)]
last = N


def rec(pos, c):
    """ 2 色で塗ることができることが保証されている """
    global C
    C[pos] = c
    for to in G[pos]:
        if C[to] == 0:
            rec(to, -c)


for i in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1; v -= 1
    # 無向グラフ
    if w%2 == 0:
        G[u].append(last)
        G[last].append(u)
        G[v].append(last)
        G[last].append(v)
        C.append(0)
        last += 1
    else:
        G[u].append(v)
        G[v].append(u)
rec(0, 1)
for i in range(N):
    print(0 if C[i] == -1 else 1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

G = defaultdict(list)

N = int(input())
C = [0 for i in range(N)]
last = N


def rec(pos, c):
    """ 2 色で塗ることができることが保証されている """
    global C
    C[pos] = c
    for to in G[pos]:
        if C[to] == 0:
            rec(to, -c)


for i in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1; v -= 1
    # 無向グラフ
    if w%2 == 0:
        G[u].append(last)
        G[last].append(u)
        G[v].append(last)
        G[last].append(v)
        C.append(0)
        last += 1
    else:
        G[u].append(v)
        G[v].append(u)
rec(0, 1)
for i in range(N):
    print(0 if C[i] == -1 else 1)
Problem p03061 (python)

Fixed submission:
def gcd(x, y):
    xx = 0; yy = 0
    if x >= y:
        xx = x; yy = y
    else:
        xx = y; yy = x
    while yy > 0:
        xx, yy = yy, xx%yy
    return xx


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    # i を入れた場合のgcdと i を入れない場合のgcdを考える
    ans = 1
    # gcd の結合則を利用する(X, Y, Zがありgcd(gcd(x,y),z)==gcd(x,gcd(x, y)))
    l = [0 for i in range(n)]
    r = [0 for i in range(n+1)]
    for i in range(n-1):
        l[i+1] = gcd(l[i], a[i])
    for i in range(n-1, -1, -1):
        r[i] = gcd(r[i+1],a[i])
    for i in range(n):
        ans = max(ans, gcd(l[i], r[i+1]))
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from math import gcd

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 1
    for i in range(n):
        tmp = -1
        for j in range(n):
            if j == i:
                continue
            if tmp == -1:
                tmp = a[j]
            tmp = gcd(tmp, a[j])
        ans = max(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()
Problem p03074 (python)

Fixed submission:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l = 0
    d = min(len(table), 2*k+1)
    r = l + d
    tmp = sum(table[l:r])
    while r <= len(table):
        ans = max(ans, tmp)
        if l < len(table):
            tmp -= table[l]
        l += 1
        if l < len(table):
            tmp -= table[l]
        l += 1
        r += 1
        if r <= len(table):
            tmp += table[r-1]
        r += 1
        if r <= len(table):
            tmp += table[r-1]
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l = 0
    d = min(len(table), 2*k+1)
    r = l + d
    tmp = sum(table[l:r])
    while r <= len(table):
        ans = max(ans, tmp)
        if l < len(table):
            tmp -= table[l]
        l += 1
        if l < len(table):
            tmp -= table[l]
        l += 1
        r += 1
        if r <= len(table):
            tmp += table[r-1]
        r += 1
        if r <= len(table):
            tmp += table[r-1]
    print(ans)


if __name__ == '__main__':
Problem p03355 (python)

Fixed submission:
def main():
    s = input()
    k = int(input())
    substr = []
    for l in range(len(s)):
        for r in range(l+1,len(s)+1):
            if r-l > k:
                break
            substr.append(s[l:r])
    c=sorted(set(substr))

    print(c[k-1])


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import Counter


def main():
    s = input()
    k = int(input())
    substr = []
    for l in range(len(s)):
        for r in range(l+1,len(s)+1):
            substr.append(s[l:r])
    c = Counter(substr)
    c = sorted(c.keys())
    print(c[k-1])


if __name__ == '__main__':
    main()