Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
# D - Decayed Bridges
from collections import deque

N, M = map(int, input().split())

graph = [[] for _ in range(N)]
AB = deque()
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    AB.append([a, b])
    graph[a].append(b)
    graph[b].append(a)

def isDivided(a, b, graph):
    d = deque()
    d.append(a)
    done = [0] * N
    while len(d)>0:
        now = d.popleft()
        done[now] = 1
        for i in graph[now]:
            if i==b:
                return 0
            elif done[i]==0:
                d.append(i)
    return sum(done)

ans = 0
for i in range(M):
    a, b = AB.popleft()
    graph[a].remove(b)
    graph[b].remove(a)
    tmpa = isDivided(a, b, graph)
    tmpb = isDivided(b, a, graph)
    if tmpa!=0:
        ans += tmpa*tmpb
        print(ans)
    else:
        print(ans)



Fixed submission:
# A - Favorite Sound

A, B, C = map(int, input().split())

print(min(B//A, C))
Buggy python submission (verdict: Runtime Error):
# B - K-th Common Divisor

A, B, K = map(int, input().split())

def gcd(x, y):
    if x==0:
        return y
    else:
        return gcd(y%x, x)

gcdAB = gcd(A, B)

cdlist = [gcdAB]
for i in range(1, (int(gcdAB**0.5))+1)[::-1]:
    if gcdAB%1==0:
        cdlist.append(i)

print(cdlist[K-1])

Fixed submission:
# B - K-th Common Divisor

A, B, K = map(int, input().split())

cnt = 0
for i in range(1, min(A, B)+1)[::-1]:
    if A%i==0 and B%i==0:
        cnt += 1
        if cnt==K:
            print(i)
            break
Buggy python submission (verdict: Time Limit Exceeded):
# D - Decayed Bridges
from collections import deque

N, M = map(int, input().split())

graph = [[] for _ in range(N)]
AB = deque()
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    AB.append([a, b])
    graph[a].append(b)
    graph[b].append(a)

def isDivided(a, b, graph):
    d = deque()
    d.append(a)
    done = [0] * N
    while len(d)>0:
        now = d.popleft()
        done[now] = 1
        for i in graph[now]:
            if i==b:
                return 0
            elif done[i]==0:
                d.append(i)
    return sum(done)

ans = 0
for i in range(M):
    a, b = AB.popleft()
    graph[a].remove(b)
    graph[b].remove(a)
    tmpa = isDivided(a, b, graph)
    tmpb = isDivided(b, a, graph)
    if tmpa!=0:
        ans += tmpa*tmpb
        print(ans)
    else:
        print(ans)



Fixed submission:
# Decayed Bridges
from collections import deque

class UnionFind():
    def __init__(self, x):
        self.parent = [-1] * x

    def Find(self, x):
        if self.parent[x]<0:
            return x
        else:
            self.parent[x] = self.Find(self.parent[x])
            return self.parent[x]
    
    def Union(self, x, y):
        x = self.Find(x)
        y = self.Find(y)
        if x==y:
            return False
        if x>y:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def isSameGroup(self, x, y):
        return self.Find(x)==self.Find(y)

    def Size(self, x):
        return self.parent[self.Find(x)]*-1

N, M = map(int, input().split())
AB = deque()
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    AB.append([a, b])

uf = UnionFind(N)

ans = deque()
tmp = N*(N-1)//2
ans.append(tmp)
for i in range(M-1):
    a, b = AB.pop()
    if uf.isSameGroup(a, b):
        ans.append(tmp)
    else:
        tmp -= uf.Size(a) * uf.Size(b)
        ans.append(tmp)
        uf.Union(a, b)

for i in range(M):
    print(ans.pop())
Buggy python submission (verdict: Time Limit Exceeded):
# D - Match Matching
import sys
sys.setrecursionlimit(10**7)
from collections import deque
from collections import defaultdict

MATCH = [(0, 6), (1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
N, M = map(int, input().split())
A = list(int(x) for x in input().split())
MA = []
for a in A:
    MA.append(MATCH[a])
MA.sort(key=lambda x: x[1])

def dfs(tmp, s):
    global ans
    if s == N:
        ans = max(ans, tmp)    
    for ma in MA:
        if s+ma[1] <= N:
            tmp = tmp*10 + ma[0]
            dfs(tmp, s + ma[1])
            tmp //= 10

tmp = 0
ans = 0
dfs(tmp, 0)
print(ans)

Fixed submission:
# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
    MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)

# dp[i] := i本のマッチを使って作れる最大桁数
dp = [-1] * (N+1)
dp[0] = 0
for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1)

remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break

print(ans)

Buggy python submission (verdict: Runtime Error):
# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [(0, 6), (1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
MA = []
for a in A:
    MA.append(MATCH[a])
MA.sort(key=lambda x: x[1])

def dfs(tmp, s):
    global ans
    if s == N:
        ans = max(ans, tmp)    
    for ma in MA:
        if s+ma[1] <= N:
            tmp = tmp*10 + ma[0]
            dfs(tmp, s + ma[1])
            tmp //= 10

tmp = 0
ans = 0
dfs(tmp, 0)
print(ans)

Fixed submission:
# D - Match Matching

N, M = map(int, input().split())
A = list(int(x) for x in input().split())
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
    MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)

# dp[i] := i本のマッチを使って作れる最大桁数
dp = [-1] * (N+1)
dp[0] = 0
for i in range(N+1):
    for j in range(M):
        if i-MA[j][1] >= 0:
            dp[i] = max(dp[i], dp[i-MA[j][1]] + 1)

remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
    for ma in MA:
        # 大きい数字から一桁使った時に、
        # 残り桁がちょうど一桁減っている場合
        if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
            ans +=  ma[0]
            remain_keta -= 1
            remain_match -= ma[1]
            break

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, K = map(int, input().split())
H = list(int(x) for x in input().split())

dp = np.full(N, np.inf)
dp[0] = 0
H = np.array(H)

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

print(int(dp[N-1]))

Fixed submission:
import numpy as np

N, K = map(int, input().split())
H = list(int(x) for x in input().split())

dp = np.full(N, np.inf)
dp[0] = 0
H = np.array(H)

for i in range(1, N):
    j = max(0,i-K)
    dp[i] = min(dp[j:i] + np.abs(H[i] - H[j:i]))

print(int(dp[N-1]))

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
H = list(int(x) for x in input().split())

dp = np.full(N, np.inf)
dp[0] = 0
H = np.array(H)

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

print(int(dp[N-1]))

Fixed submission:
import numpy as np

N, K = map(int, input().split())
H = list(int(x) for x in input().split())

dp = np.full(N, np.inf)
dp[0] = 0
H = np.array(H)

for i in range(1, N):
    j = max(0,i-K)
    dp[i] = min(dp[j:i] + np.abs(H[i] - H[j:i]))

print(int(dp[N-1]))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(int(x) for x in input().split())

f_inf = float('infinity')
dp = [f_inf] * N
dp[0] = 0

for i in range(1, N):
    for j in range(1, min(i,K)+1):
        dp[i] = min(dp[i], dp[i-j] + abs(H[i] - H[i-j]))

print(dp[N-1])


Fixed submission:
import numpy as np

N, K = map(int, input().split())
H = list(int(x) for x in input().split())

dp = np.full(N, np.inf)
dp[0] = 0
H = np.array(H)

for i in range(1, N):
    j = max(0,i-K)
    dp[i] = min(dp[j:i] + np.abs(H[i] - H[j:i]))

print(int(dp[N-1]))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, W = map(int, input().split())
w = np.zeros(N, dtype=int)
v = np.zeros(N, dtype=int)
dp = np.zeros((N+1, W+1),dtype=int)

for i in range(N):
    w[i], v[i] = map(int, input().split())
    for j in range(W+1):
        if w[i] > j:
            dp[i+1, j] = dp[i, j]
        else:
            dp[i+1, j] = max(dp[i, j], dp[i, j-w[i]] + v[i])

print(dp[N,W])

Fixed submission:
import numpy as np

N, W = map(int, input().split())
dp = np.zeros((W+1),dtype=int)

for _ in range(N):
    w, v = map(int, input().split())
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)

print(dp.max())

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, W = map(int, input().split())
w = np.zeros(N, dtype=int)
v = np.zeros(N, dtype=int)
dp = np.zeros((N+1, W+1),dtype=int)

for i in range(N):
    w[i], v[i] = map(int, input().split())

for i in range(N):
    for j in range(W+1):
        if w[i] > j:
            dp[i+1, j] = dp[i, j]
        else:
            dp[i+1, j] = max(dp[i, j], dp[i, j-w[i]] + v[i])

print(dp[N,W])

Fixed submission:
import numpy as np

N, W = map(int, input().split())
dp = np.zeros((W+1),dtype=int)

for _ in range(N):
    w, v = map(int, input().split())
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)

print(dp.max())
Buggy python submission (verdict: Time Limit Exceeded):
# C - IDC
import bisect
import sys
readline = sys.stdin.readline
 
base = 1000000000000
N, M = map(int, readline().split())
P = [0] * M
Y = [0] * M
T = [[] for _ in range(N+1)]
 
for i in range(M):
    P[i], Y[i] = map(int, readline().split())
    T[P[i]].append(Y[i])

for i in range(M):    
    T[P[i]].sort()
    
for i in range(M):
    num = bisect.bisect_right(T[P[i]], Y[i])
    print(str(base + P[i]*1000000 + num)[1:])

Fixed submission:
# C - IDC

import bisect
import sys
readline = sys.stdin.readline

base = 1000000000000
N, M = map(int, readline().split())
PY = []
T = [[] for _ in range(N+1)]

for i in range(M):
    P, Y = map(int, readline().split())
    PY.append((P, Y))

PY_sorted = sorted(PY, key=lambda x: x[1])
PY_sorted = sorted(PY_sorted, key=lambda x: x[0])

for i in range(M):
    T[PY_sorted[i][0]].append(PY_sorted[i][1])

for i in range(M):
    n = bisect.bisect_right(T[PY[i][0]], PY[i][1])
    print(str(base + PY[i][0]*1000000 + n)[1:])

Buggy python submission (verdict: Time Limit Exceeded):
# C - IDC
import bisect
import sys
readline = sys.stdin.readline

base = 1000000000000
N, M = map(int, readline().split())
P = [0] * M
Y = [0] * M
T = [[] for _ in range(N+1)]

for i in range(M):
    P[i], Y[i] = map(int, readline().split())
    T[P[i]].append(Y[i])
    T[P[i]].sort()

for i in range(M):
    num = bisect.bisect_right(T[P[i]], Y[i])
    print(str(base + P[i]*1000000 + num)[1:])

Fixed submission:
# C - IDC

import bisect
import sys
readline = sys.stdin.readline

base = 1000000000000
N, M = map(int, readline().split())
PY = []
T = [[] for _ in range(N+1)]

for i in range(M):
    P, Y = map(int, readline().split())
    PY.append((P, Y))

PY_sorted = sorted(PY, key=lambda x: x[1])
PY_sorted = sorted(PY_sorted, key=lambda x: x[0])

for i in range(M):
    T[PY_sorted[i][0]].append(PY_sorted[i][1])

for i in range(M):
    n = bisect.bisect_right(T[PY[i][0]], PY[i][1])
    print(str(base + PY[i][0]*1000000 + n)[1:])

Buggy python submission (verdict: Time Limit Exceeded):
# C - IDC
import bisect

base = 1000000000000
N, M = map(int, input().split())
P = [0] * M
Y = [0] * M
T = [[] for _ in range(N+1)]

for i in range(M):
    P[i], Y[i] = map(int, input().split())
    T[P[i]].append(Y[i])
    T[P[i]].sort()

for i in range(M):
    num = bisect.bisect_right(T[P[i]], Y[i])
    print(str(base + P[i]*1000000 + num)[1:])

Fixed submission:
# C - IDC

import bisect
import sys
readline = sys.stdin.readline

base = 1000000000000
N, M = map(int, readline().split())
PY = []
T = [[] for _ in range(N+1)]

for i in range(M):
    P, Y = map(int, readline().split())
    PY.append((P, Y))

PY_sorted = sorted(PY, key=lambda x: x[1])
PY_sorted = sorted(PY_sorted, key=lambda x: x[0])

for i in range(M):
    T[PY_sorted[i][0]].append(PY_sorted[i][1])

for i in range(M):
    n = bisect.bisect_right(T[PY[i][0]], PY[i][1])
    print(str(base + PY[i][0]*1000000 + n)[1:])
Buggy python submission (verdict: Runtime Error):
# C - Pyramid

N = int(input())
XYH = [tuple(int(i) for i in input().split()) for _ in range(N)]

for cx in range(101):
    for cy in range(101):
        pre_tmp_H = 0
        for (x, y, h) in XYH:
            tmp_H = h + abs(x - cx) + abs(y - cy)
            if pre_tmp_H != 0 and tmp_H != pre_tmp_H:
                break
            pre_tmp_H = tmp_H
        else:
            ans = (cx, cy, tmp_H)

print(' '.join(map(str, ans)))

Fixed submission:
# C - Pyramid

N = int(input())
XYH = [tuple(int(i) for i in input().split()) for _ in range(N)]

XYH.sort(key = lambda x: x[2], reverse=True)

for cx in range(101):
    for cy in range(101):
        (x, y, h) = XYH[0]
        tmp_H = h + abs(x - cx) + abs(y - cy)
        for (x, y, h) in XYH:
            if h != max(tmp_H - abs(x - cx) - abs(y - cy), 0):
                break
        else:
            ans = (cx, cy, tmp_H)
            break

print(' '.join(map(str, ans)))

Buggy python submission (verdict: Runtime Error):
# C - Pyramid

N = int(input())
XYH = [tuple(int(x) for x in input().split()) for _ in range(N)]

for cx in range(101):
    for cy in range(101):
        pre_tmp_H = 0
        for (x, y, h) in XYH:
            tmp_H = h + abs(x - cx) + abs(y - cy)
            if pre_tmp_H != 0 and tmp_H != pre_tmp_H:
                break
            pre_tmp_H = tmp_H
        else:
            ans = (cx, cy, tmp_H)

print(' '.join(map(str, ans)))
        

Fixed submission:
# C - Pyramid

N = int(input())
XYH = [tuple(int(i) for i in input().split()) for _ in range(N)]

XYH.sort(key = lambda x: x[2], reverse=True)

for cx in range(101):
    for cy in range(101):
        (x, y, h) = XYH[0]
        tmp_H = h + abs(x - cx) + abs(y - cy)
        for (x, y, h) in XYH:
            if h != max(tmp_H - abs(x - cx) - abs(y - cy), 0):
                break
        else:
            ans = (cx, cy, tmp_H)
            break

print(' '.join(map(str, ans)))
Buggy python submission (verdict: Runtime Error):
# C - Skip

import numpy as np
import math

N, X = map(int, input().split())
x = list(int(x) for x in input().split())
x.append(X)
x.sort()
x = np.array(x)
x = x - x[0]

ans = 10**18
if N == 1:
    print(x[1] - x[0])
else:
    for i in range(N-1):
        ans = min(ans, math.gcd(x[i], x[i+1]))
    print(ans)

Fixed submission:
# C - Skip

def gcd(x, y):
    if x == 0:
        return y
    else:
        return gcd(y%x, x)

N, X = map(int, input().split())
x = list(int(x) for x in input().split())
x.append(X)
x.sort()
stnd = x[0]
for i in range(N+1):
    x[i] -= stnd

ans = gcd(x[0], x[1])
for i in range(1, N):
    ans = gcd(ans, gcd(x[i], x[i+1]))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# D - AtCoder Express 2

N, M, Q = map(int, input().split())
G = [[0] * (N+1) for _ in range(N+1)]

for _ in range(M):
    L, R = map(int, input().split())
    G[L][R] += 1

for i in range(N+1):
    for j in range(N):
        G[i][j+1] += G[i][j]

for _ in range(Q):
    p, q = map(int, input().split())
    ans = 0
    for i in range(p, q+1):
        ans += G[i][q] - G[i][p-1]
    print(ans)

Fixed submission:
# D - AtCoder Express 2

N, M, Q = map(int, input().split())
G = [[0] * (N+1) for _ in range(N+1)]

for _ in range(M):
    L, R = map(int, input().split())
    G[L][R] += 1

for i in range(N+1):
    for j in range(N):
        G[i][j+1] += G[i][j]

for _ in range(Q):
    p, q = map(int, input().split())
    ans = 0
    for i in range(p, q+1):
        ans += G[i][q] - G[i][p-1]
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# D - We Love ABC

S = list(str(input()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a = (a + pat) % MOD
    elif s == 'B':
        ab = (ab + a) % MOD
    elif s == 'C':
        abc = (abc + ab) % MOD
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a = (3*a + pat) % MOD
        ab = (3*ab + tmpa) % MOD
        abc = (3*abc + tmpab) % MOD
        pat *= 3

print(abc)

Fixed submission:
# D - We Love ABC
import sys
readline = sys.stdin.readline

S = list(str(readline().rstrip()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a = (a + pat) % MOD
    elif s == 'B':
        ab = (ab + a) % MOD
    elif s == 'C':
        abc = (abc + ab) % MOD
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a = (3*a + pat) % MOD
        ab = (3*ab + tmpa) % MOD
        abc = (3*abc + tmpab) % MOD
        pat = (pat*3) % MOD

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
# D - We Love ABC

S = list(str(input()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a = (a + pat) % MOD
    elif s == 'B':
        ab = (ab + a) % MOD
    elif s == 'C':
        abc = (abc + ab) % MOD
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a = (3*a + pat) % MOD
        ab = (3*ab + tmpa) % MOD
        abc = (3*abc + tmpab) % MOD
        pat *= 3

print(abc)

Fixed submission:
# D - We Love ABC
import sys
readline = sys.stdin.readline

S = list(str(readline().rstrip()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a = (a + pat) % MOD
    elif s == 'B':
        ab = (ab + a) % MOD
    elif s == 'C':
        abc = (abc + ab) % MOD
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a = (3*a + pat) % MOD
        ab = (3*ab + tmpa) % MOD
        abc = (3*abc + tmpab) % MOD
        pat = (pat*3) % MOD

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
# D - We Love ABC

S = list(str(input()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a += pat
    elif s == 'B':
        ab += a
    elif s == 'C':
        abc += ab
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a += 2*a + pat
        ab += 2*ab + tmpa
        abc += 2*abc + tmpab
        pat *= 3

print(abc%MOD)

Fixed submission:
# D - We Love ABC
import sys
readline = sys.stdin.readline

S = list(str(readline().rstrip()))
MOD = 10**9 + 7
a = 0
ab = 0
abc = 0
pat = 1
for s in S:
    if s == 'A':
        a = (a + pat) % MOD
    elif s == 'B':
        ab = (ab + a) % MOD
    elif s == 'C':
        abc = (abc + ab) % MOD
    else: # s == '?'
        tmpa = a
        tmpab = ab
        a = (3*a + pat) % MOD
        ab = (3*ab + tmpa) % MOD
        abc = (3*abc + tmpab) % MOD
        pat = (pat*3) % MOD

print(abc)
Buggy python submission (verdict: Runtime Error):
# D - Good Grid

from collections import defaultdict
import itertools

N, C = map(int, input().split())
D = [[0] * C for _ in range(C)]
for i in range(C):
    D[i] = list(int(d) for d in input().split())

# MOD3の世界で0,1,2ごとに、最初の値の数をカウントしておく
dict_c = [defaultdict(int) for _ in range(3)]
for i in range(N):
    c = list(int(x) for x in input().split())
    for j in range(N):
        dict_c[(i+j)%3][c[j]-1] += 1

# ここからは全探索。
# 0, 1, 2の各配列に対して、全ての色を試す(C**3)
list_C = list(range(C))
ans = 10**18
for (p, q, r) in itertools.permutations(list_C):
    tmp = 0
    for i, j in enumerate((p, q, r)):
        for m, n in dict_c[i].items():
            tmp += D[m][j] * n
    ans = min(ans, tmp)

print(ans)

Fixed submission:
# D - Good Grid

from collections import defaultdict
import itertools

N, C = map(int, input().split())
D = [[0] * C for _ in range(C)]
for i in range(C):
    D[i] = list(int(d) for d in input().split())

# MOD3の世界で0,1,2ごとに、最初の値の数をカウントしておく
dict_c = [defaultdict(int) for _ in range(3)]
for i in range(N):
    c = list(int(x) for x in input().split())
    for j in range(N):
        dict_c[(i+j)%3][c[j]-1] += 1

# ここからは全探索。
# 0, 1, 2の各配列に対して、全ての色を試す(C**3)
list_C = list(range(C))
ans = 10**18
for (p, q, r) in itertools.permutations(list_C, 3):
    tmp = 0
    for i, j in enumerate((p, q, r)):
        for m, n in dict_c[i].items():
            tmp += D[m][j] * n
    ans = min(ans, tmp)

print(ans)

Buggy python submission (verdict: Runtime Error):
# D - Good Grid

from collections import defaultdict
import itertools

N, C = map(int, input().split())
D = [[0] * C for _ in range(C)]
for i in range(C):
    D[i] = list(int(d) for d in input().split())

# MOD3の世界で0,1,2ごとに、最初の値の数をカウントしておく
dict_c = [defaultdict(int) for _ in range(3)]
for i in range(N):
    c = list(int(x) for x in input().split())
    for j in range(N):
        dict_c[(i+j)%3][c[j]-1] += 1
#print(D)
#print(dict_c)

# ここからは全探索。
# 0, 1, 2の各配列に対して、全ての色を試す(C**3)
list_C = list(range(C))
ans = 10**18
for (p, q, r) in itertools.permutations(list_C):
    tmp = 0
    for i, j in enumerate((p, q, r)):
        #print(p, q, r)
        for i, k in dict_c[i].items():
            #print(i, j, k)
            #print(D[i][j])
            tmp += D[i][j] * k
    #print(tmp)
    ans = min(ans, tmp)

print(ans)

Fixed submission:
# D - Good Grid

from collections import defaultdict
import itertools

N, C = map(int, input().split())
D = [[0] * C for _ in range(C)]
for i in range(C):
    D[i] = list(int(d) for d in input().split())

# MOD3の世界で0,1,2ごとに、最初の値の数をカウントしておく
dict_c = [defaultdict(int) for _ in range(3)]
for i in range(N):
    c = list(int(x) for x in input().split())
    for j in range(N):
        dict_c[(i+j)%3][c[j]-1] += 1

# ここからは全探索。
# 0, 1, 2の各配列に対して、全ての色を試す(C**3)
list_C = list(range(C))
ans = 10**18
for (p, q, r) in itertools.permutations(list_C, 3):
    tmp = 0
    for i, j in enumerate((p, q, r)):
        for m, n in dict_c[i].items():
            tmp += D[m][j] * n
    ans = min(ans, tmp)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
K = int(input())

str_list = set()
for i in range(len(s)+1):
    for j in range(i+1, len(s)+1):
        str_list.add(s[i:j])

str_list = list(str_list)
str_list.sort()
print(str_list[K-1])

Fixed submission:
# C - K-th Substring

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

str_list = set()
for i in range(len(s)+1):
    for j in range(i+1, i+K+1):
        if j > len(s):
            break
        str_list.add(s[i:j])

str_list = list(str_list)
str_list.sort()
print(str_list[K-1])
Buggy python submission (verdict: Time Limit Exceeded):
# C - Grid Repainting 2
from collections import deque

H, W = map(int, input().split())
grid = [[''] * W for _ in range(H)]
for i in range(H):
    grid[i] = list(str(input()))

d = deque()
ans = 'Yes'
for i in range(H):
    for j in range(W):
        d.append((i, j))
        tmp = 0
        while d:
            p, q = d.popleft()
            if grid[p][q] == '#':
                grid[p][q] = '.'
                tmp += 1
            for x, y in ((0, 1), (1, 0), (1, 0), (0, -1)):
                nx = p+x
                ny = q+y
                if nx < 0 or nx >= H or ny < 0 or ny >= W:
                    continue
                if grid[nx][ny] == '.':
                    continue
                d.append((nx, ny))
        if tmp == 1:
            ans = 'No'
            break
    else:
        continue
    break
print(ans)

Fixed submission:
# C - Grid Repainting 2
from collections import deque

H, W = map(int, input().split())
grid = [[''] * W for _ in range(H)]
for i in range(H):
    grid[i] = list(str(input()))

d = deque()
ans = 'Yes'
for i in range(H):
    for j in range(W):
        if grid[i][j] == '.':
            continue
        d.append((i, j))
        grid[i][j] = '.'
        tmp = 1
        while d:
            p, q = d.popleft()
            for x, y in ((0, 1), (1, 0), (-1, 0), (0, -1)):
                nx = p+x
                ny = q+y
                if nx < 0 or nx >= H or ny < 0 or ny >= W:
                    continue
                if grid[nx][ny] == '.':
                    continue
                d.append((nx, ny))
                grid[nx][ny] = '.'
                tmp += 1
        if tmp == 1:
            ans = 'No'
            break
    else:
        continue
    break
print(ans)