Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
A_ = list(map(int, input().split()))
ma = -float("inf")
ama = -1
for i, a in enumerate(A_):
    if a>0:
        break
    if ma < a:
        ma = a
        ama = i
else:
    print(ma)
    print(N-1)
    Ans = []
    for i in range(N, ama+1, -1):
        Ans.append(i)
    for i in range(ama):
        Ans.append(1)
    print("\n".join(map(str, Ans)))
    exit()

A = A_[:]

R1 = []
an1 = 0
for i in range(0, N, 2):
    a = A[i]
    if a>=0:
        an1 += a
    else:
        R1.append(i)
idx_r1 = i
R2 = []
an2 = 0
for i in range(1, N, 2):
    a = A[i]
    if a>=0:
        an2 += a
    else:
        R2.append(i)
idx_r2 = i
ans, R, idx_r = (an1, R1, idx_r1) if an1 > an2 else (an2, R2, idx_r2)
idx_r += 1
print(ans)
Ans = []

for idx in R[::-1]:
    del A[idx]
    if 1<=idx<len(A):
        A[idx-1] += A.pop(idx)
    Ans.append(idx+1)
for idx in range(idx_r, -1, -2):
    if 0<=idx<len(A):
        del A[idx]
        if 1<=idx<len(A):
            A[idx-1] += A.pop(idx)
        Ans.append(idx+1)

for idx in Ans:
    del A_[idx-1]
    if 1<idx<=len(A):
        A[idx-2] += A.pop(idx-1)
assert len(A)==1 and A[0]==ans

print(len(Ans))
print("\n".join(map(str, Ans)))


Fixed submission:
N = int(input())
A_ = list(map(int, input().split()))
ma = -float("inf")
ama = -1
for i, a in enumerate(A_):
    if a>0:
        break
    if ma < a:
        ma = a
        ama = i
else:
    print(ma)
    print(N-1)
    Ans = []
    for i in range(N, ama+1, -1):
        Ans.append(i)
    for i in range(ama):
        Ans.append(1)
    print("\n".join(map(str, Ans)))
    exit()

A = A_[:]

R1 = []
an1 = 0
for i in range(0, N, 2):
    a = A[i]
    if a>=0:
        an1 += a
    else:
        R1.append(i)
idx_r1 = i
R2 = []
an2 = 0
for i in range(1, N, 2):
    a = A[i]
    if a>=0:
        an2 += a
    else:
        R2.append(i)
idx_r2 = i
ans, R, idx_r = (an1, R1, idx_r1) if an1 > an2 else (an2, R2, idx_r2)
idx_r += 1
print(ans)
Ans = []

for idx in R[::-1]:
    del A[idx]
    if 1<=idx<len(A):
        A[idx-1] += A.pop(idx)
    elif idx==0:
        idx_r -= 1
    Ans.append(idx+1)
for idx in range(idx_r, -1, -2):
    if 0<=idx<len(A):
        del A[idx]
        if 1<=idx<len(A):
            A[idx-1] += A.pop(idx)
        Ans.append(idx+1)

for idx in Ans:
    del A_[idx-1]
    if 1<idx<=len(A):
        A[idx-2] += A.pop(idx-1)
# print(Ans)
assert len(A)==1 and A[0]==ans

print(len(Ans))
print("\n".join(map(str, Ans)))
Buggy python submission (verdict: Runtime Error):
def distribute(n, person, min, max, mode="even"):
    # n 個を person 人に分配する
    # 返り値は [[a (個), a 個もらう人数], ...]
    # 分配できないときは None を返す
    if person==0 and n==0:
        return []
    elif not min*person <= n <= max*person:
        return None
    elif mode=="even":
        q, m = divmod(n, person)
        if m==0:
            return [[q, person]]
        else:
            return [[q, person-m], [q+1, m]]
    elif mode=="greedy":
        n -= min * person
        q, m = divmod(n, max-min)
        if m==0:
            return [[min, person-q], [max, q]]
        else:
            return [[min, person-1-q], [min+m, 1], [max, q]]
    else:
        raise ValueError("'mode' must be 'even' or 'greedy'.")

import numpy as np
N, A, B = map(int, input().split())
Ans = np.arange(N, 0, -1, dtype=np.int64)
Ans[:A] = np.flipud(Ans[:A])
c = N-A
D = distribute(c, B-1, min=1, max=A, mode="greedy")
if D is None:
    print(-1)
    exit()
idx = A
for p, n in D:
    for n_ in range(n):
        Ans[idx:idx+p] = np.flipud(Ans[idx:idx+p])
        idx += p
print(" ".join(str(int(a)) for a in Ans))


Fixed submission:
def distribute(n, person, min, max, mode="even"):
    # n 個を person 人に分配する
    # 返り値は [[a (個), a 個もらう人数], ...]
    if person==0 and n==0:
        return []
    elif not min*person <= n <= max*person:
        return None
    elif mode=="even":
        q, m = divmod(n, person)
        if m==0:
            return [[q, person]]
        else:
            return [[q, person-m], [q+1, m]]
    elif mode=="greedy":
        n -= min * person
        q, m = divmod(n, max-min)
        if m==0:
            return [[min, person-q], [max, q]]
        else:
            return [[min, person-1-q], [min+m, 1], [max, q]]
    else:
        raise ValueError("'mode' must be 'even' or 'greedy'.")

import numpy as np
N, A, B = map(int, input().split())
Ans = np.arange(N, 0, -1, dtype=np.int64)
Ans[:A] = np.flipud(Ans[:A])
c = N-A
D = distribute(c, B-1, min=1, max=A, mode="even")
if D is None:
    print(-1)
    exit()
idx = A
for p, n in D:
    for _ in range(n):
        Ans[idx:idx+p] = np.flipud(Ans[idx:idx+p])
        idx += p
print(" ".join(str(int(a)) for a in Ans))
Buggy python submission (verdict: Runtime Error):
S = input()
n = len(S)
ans = n//2
c = S[ans]

while ans <= n:
    if c == S[ans] == S[-ans-1]:
        ans += 1
    else:
        break
print(ans)


Fixed submission:
S = input()
n = len(S)
ans = n//2
c = S[ans]

while ans < n:
    if c == S[ans] == S[-ans-1]:
        ans += 1
    else:
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
N, Z, W = map(int, input().split())
A = list(map(int, input().split()))
L = []
D = []
def rec(x, y, n, turn):
    xp, yp = x, y
    C = []
    for i in range(n+1, N+1):
        x, y = xp, yp
        if i == N:
            C.append(abs(A[-1]-y if turn else x-A[-1]))
            continue
        if turn:
            x = A[i-1]
        else:
            y = A[i-1]
        if [x, y, i, not turn] in L:
            C.append(D[L.index([x, y, i, not turn])])
        else:
            C.append(rec(x, y, i, not turn))
    ret = max(C) if turn else min(C)
    L.append([xp, yp, n, turn])
    D.append(ret)
    return ret


print(rec(Z, W, 0, True))


Fixed submission:
import numpy as np

def solve(N, Z, W, A):
    # dp_x[n] := y さんが n 枚目を引いて x さんに手番が回ったときの最大値
    dp_x = np.full(N+1, 1<<61, dtype=np.int64)
    dp_y = np.zeros(N+1, dtype=np.int64)
    for i in range(N-1, 0, -1):
        dp_x[i] = max(dp_y[i+1:].max(), abs(A[-1]-A[i-1]))
        dp_y[i] = min(dp_x[i+1:].min(), abs(A[-1]-A[i-1]))
    ans = max(abs(A[-1]-W), dp_y[1:].max())
    return ans

N, Z, W = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
ans = solve(N, Z, W, A)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
N, K = map(int, input().split())
P = [list(map(int, input().split())) for _ in range(N)]
ans = 1<<62
for t in combinations(P, min(4, K)):
    xs, ys = zip(*t)
    xmin = min(xs)
    xmax = max(xs)
    ymin = min(ys)
    ymax = max(ys)
    area = (xmax-xmin) * (ymax-ymin)
    if ans < area:
        continue
    k = 0
    for x, y in P:
        if xmin <= x <= xmax and ymin <= y <= ymax:
            k += 1
    if k >= K:
        ans = area
print(ans)


Fixed submission:
from itertools import combinations
def main():
    N, K = map(int, input().split())
    P = [list(map(int, input().split())) for _ in range(N)]
    ans = 1<<62
    for t in combinations(P, min(4, K)):
        xs, ys = zip(*t)
        xmin = min(xs)
        xmax = max(xs)
        ymin = min(ys)
        ymax = max(ys)
        area = (xmax-xmin) * (ymax-ymin)
        if ans < area:
            continue
        k = 0
        for x, y in P:
            if xmin <= x <= xmax and ymin <= y <= ymax:
                k += 1
        if k >= K:
            ans = area
    print(ans)

main()
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
#E = [sorted(list(map(int, input().split()))) for i in range(M)]
V = [[] for i in range(N+1)]
for i in range(M):
    A, B = map(int, input().split())
    V[A].append(B)
    V[B].append(A)

L = [-1]*(N+1)
L[1] = 0
al = False

from collections import deque
Open = deque()
Open.append(1)
Close = set()
while len(Open)>0:
    v = Open.pop()
    Close.add(v)
    for v2 in V[v]:
        if v2 not in Close:
            Open.append(v2)
        if L[v2] == -1:
            L[v2] = 1-L[v]
        elif L[v] + L[v2] != 1:
            al = True
            break
    if al:
        break

#print(V, L, al)
if al:
    print(N*(N-1)//2-M)
else:
    c = L.count(1)
    print(c*(N-c)-M)



Fixed submission:
N, M = map(int, input().split())
V = [[] for i in range(N+1)]
for i in range(M):
    A, B = map(int, input().split())
    V[A].append(B)
    V[B].append(A)

L = [-1]*(N+1)
L[1] = 0
al = False

from collections import deque
Open = deque()
Open.append(1)
Close = set()
while len(Open)>0:
    v = Open.pop()
    Close.add(v)
    for v2 in V[v]:
        if v2 not in Close:
            Open.append(v2)
        if L[v2] == -1:
            L[v2] = 1-L[v]
        elif L[v] + L[v2] != 1:
            al = True
            break
    if al:
        break

if al:
    print(N*(N-1)//2-M)
else:
    c = L.count(1)
    print(c*(N-c)-M)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
E = [sorted(list(map(int, input().split()))) for i in range(M)]
E.sort()
print(E)
L = [-1]*(N+1)
L[1] = 0
al = False
for e in E:
    a, b = e
    if L[b] == -1:
        L[b] = 1-L[a]
    elif L[a] + L[b] != 1:
        al = True
        break
if al:
    print(N*(N-1)//2-M)
else:
    c = L.count(1)
    print(c*(N-c)-M)
    raise ValueError()


Fixed submission:
N, M = map(int, input().split())
V = [[] for i in range(N+1)]
for i in range(M):
    A, B = map(int, input().split())
    V[A].append(B)
    V[B].append(A)

L = [-1]*(N+1)
L[1] = 0
al = False

from collections import deque
Open = deque()
Open.append(1)
Close = set()
while len(Open)>0:
    v = Open.pop()
    Close.add(v)
    for v2 in V[v]:
        if v2 not in Close:
            Open.append(v2)
        if L[v2] == -1:
            L[v2] = 1-L[v]
        elif L[v] + L[v2] != 1:
            al = True
            break
    if al:
        break

if al:
    print(N*(N-1)//2-M)
else:
    c = L.count(1)
    print(c*(N-c)-M)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        direct = A[i][j]
        for k in range(N):
            if k == i or k == j:
                continue
            ind = A[i][k] + A[k][j]
            if direct > ind:
                print(-1)
                exit()
            elif direct == ind:
                break
        else:
            ans += direct
print(ans)

Fixed submission:
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        direct = A[i][j]
        for k in range(N):
            if k == i or k == j:
                continue
            ind = A[i][k] + A[k][j]
            if direct > ind:
                print(-1)
                exit()
            elif direct == ind:
                break
        else:
            ans += direct
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, M, Rs = map(int, input().split())
R = list(map(int, input().split()))
D = [[float("inf")] * (N+1) for i in range(N+1)]
for i in range(M):
    a, b, c = map(int, input().split())
    D[a][b] = D[b][a] = c

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


def full(L):
    if len(L) == 1:
        return [L]
    else:
        L2 = []
        for i in range(len(L)):
            L2.extend([[L[i]] + Lc for Lc in full(L[:i] + L[i+1:])])
        return L2


L = full(R)
ans = float("inf")
for l in L:
    a = 0
    for i in range(Rs-1):
        a += D[l[i]][l[i+1]]
    ans = min(ans, a)

print(ans)

Fixed submission:
N, M, Rs = map(int, input().split())
R = list(map(int, input().split()))
D = [[float("inf")] * (N+1) for i in range(N+1)]
for i in range(M):
    a, b, c = map(int, input().split())
    D[a][b] = D[b][a] = c

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


def full(L):
    if len(L) == 1:
        return [L]
    else:
        L2 = []
        for i in range(len(L)):
            L2.extend([[L[i]] + Lc for Lc in full(L[:i] + L[i+1:])])
        return L2


L = full(R)
ans = float("inf")
for l in L:
    a = 0
    for i in range(Rs-1):
        a += D[l[i]][l[i+1]]
    ans = min(ans, a)

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

N = int(input())
ABC = [list(map(int, input().split())) for i in range(N-1)]
Q, K = map(int, input().split())
D = [0] * (N+1)
L = [[] for i in range(N+1)]
for a, b, c in ABC:
    L[a].append([b, c])
    L[b].append([a, c])


def dfs(n, np):
    for nc in L[n]:
        if nc[0] == np:
            continue
        else:
            D[nc[0]] = D[n] + nc[1]
            dfs(nc[0], n)


dfs(K, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(D[x] + D[y])


Fixed submission:
import sys
sys.setrecursionlimit(500000)

N = int(input())
ABC = [list(map(int, input().split())) for i in range(N-1)]
Q, K = map(int, input().split())
D = [0] * (N+1)
L = [[] for i in range(N+1)]
for a, b, c in ABC:
    L[a].append([b, c])
    L[b].append([a, c])


def dfs(n, np):
    for nc in L[n]:
        if nc[0] == np:
            continue
        else:
            D[nc[0]] = D[n] + nc[1]
            dfs(nc[0], n)


dfs(K, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(D[x] + D[y])

Buggy python submission (verdict: Runtime Error):
N = int(input())
ABC = [list(map(int, input().split())) for i in range(N-1)]
Q, K = map(int, input().split())
D = [0] * (N+1)
L = [[] for i in range(N+1)]
for a, b, c in ABC:
    L[a].append([b, c])
    L[b].append([a, c])


def dfs(n, np):
    for nc in L[n]:
        if nc[0] == np:
            continue
        else:
            D[nc[0]] = D[n] + nc[1]
            dfs(nc[0], n)


dfs(K, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(D[x] + D[y])


Fixed submission:
import sys
sys.setrecursionlimit(500000)

N = int(input())
ABC = [list(map(int, input().split())) for i in range(N-1)]
Q, K = map(int, input().split())
D = [0] * (N+1)
L = [[] for i in range(N+1)]
for a, b, c in ABC:
    L[a].append([b, c])
    L[b].append([a, c])


def dfs(n, np):
    for nc in L[n]:
        if nc[0] == np:
            continue
        else:
            D[nc[0]] = D[n] + nc[1]
            dfs(nc[0], n)


dfs(K, 0)
for i in range(Q):
    x, y = map(int, input().split())
    print(D[x] + D[y])
Buggy python submission (verdict: Runtime Error):
N = int(input())
Node = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    Node[a].append(b)
    Node[b].append(a)


Route = [1]
def dfs(n, np):
    for nc in Node[n]:
        if nc == np:
            continue
        elif nc == N:
            Route.append(N)
            return True
        else:
            Route.append(nc)
            if dfs(nc, n):
                return True
            else:
                Route.pop()
    return False

dfs(1, 0)
b = (len(Route)+1)//2
Node[Route[b-1]].remove(Route[b])
Node[Route[b]].remove(Route[b-1])

cnt = 0
def dfs2(n, np):
    global cnt
    for nc in Node[n]:
        if nc == np:
            continue
        else:
            cnt += 1
            dfs2(nc, n)

dfs2(1, 0)
cnt = -cnt
dfs2(N, 0)
if cnt >= 0:
    print("Snuke")
else:
    print("Fennec")


Fixed submission:
import sys
sys.setrecursionlimit(500000)
N = int(input())
Node = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    Node[a].append(b)
    Node[b].append(a)
Route = [1]
def dfs(n, np):
    for nc in Node[n]:
        if nc == np:
            continue
        elif nc == N:
            Route.append(N)
            return True
        else:
            Route.append(nc)
            if dfs(nc, n):
                return True
            else:
                Route.pop()
    return False

dfs(1, 0)
b = (len(Route)+1)//2
Node[Route[b-1]].remove(Route[b])
Node[Route[b]].remove(Route[b-1])

cnt = 0
def dfs2(n, np):
    global cnt
    for nc in Node[n]:
        if nc == np:
            continue
        else:
            cnt += 1
            dfs2(nc, n)

dfs2(1, 0)
cnt = -cnt
dfs2(N, 0)
if cnt >= 0:
    print("Snuke")
else:
    print("Fennec")
Buggy python submission (verdict: Runtime Error):
# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

      
n = int(input())
A = list(map(int, input().split()))
C = [-1] * (n+1)
lr = 0
for i, a in enumerate(A):
    if C[a] == -1:
        C[a] = i
    else:
        lr = C[a] + n - i
        break


def combination(n, mod):
    lst = [1]
    for i in range(1, n+1):
        lst.append(lst[-1] * (n+1-i) % mod * modinv(i, mod) % mod)
    return lst


mod = 10**9+7
NC = combination(n+1, mod)
LRC = combination(lr, mod)
for i in range(1, n+2):
    print((NC[i] - (LRC[i-1] if i-1 <= lr else 0)) % mod)


Fixed submission:
A = list(map(int, input().split()))
print(sum(A)-max(A))
Buggy python submission (verdict: Runtime Error):
    # 拡張ユークリッド互除法
    # ax + by = gcd(a,b)の最小整数解を返す
    def egcd(a, b):
        if a == 0:
            return b, 0, 1
        else:
            g, y, x = egcd(b % a, a)
            return g, x - (b // a) * y, y
     
     
    # mを法とするaの乗法的逆元
    def modinv(a, m):
        g, x, y = egcd(a, m)
        if g != 1:
            raise Exception('modular inverse does not exist')
        else:
            return x % m
     
          
    n = int(input())
    A = list(map(int, input().split()))
    C = [-1] * (n+1)
    lr = 0
    for i, a in enumerate(A):
        if C[a] == -1:
            C[a] = i
        else:
            lr = C[a] + n - i
            break
     
     
    def combination(n):
        lst = [1]
        mod = 10**9+7
        for i in range(1, n+1):
            lst.append(lst[-1] * (n+1-i) % mod * modinv(i, mod) % mod)
        return lst
     
     
    NC = combination(n+1)
    LRC = combination(lr)
    for i in range(1, n+2):
        print(NC[i] - (LRC[i-1] if i-1 <= lr else 0))

Fixed submission:
# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


n = int(input())
A = list(map(int, input().split()))
C = [-1] * (n+1)
lr = 0
for i, a in enumerate(A):
    if C[a] == -1:
        C[a] = i
    else:
        lr = C[a] + n - i
        break


def combination(n, mod):
    lst = [1]
    for i in range(1, n+1):
        lst.append(lst[-1] * (n+1-i) % mod * modinv(i, mod) % mod)
    return lst


mod = 10**9+7
NC = combination(n+1, mod)
LRC = combination(lr, mod)
for i in range(1, n+2):
    print((NC[i] - (LRC[i-1] if i-1 <= lr else 0)) % mod)
Buggy python submission (verdict: Runtime Error):
N = int(input())
T = [[i] + list(map(int, input().split())) for i in range(N)]
E = [[] for i in range(N)]
E = []
T.sort(key=lambda x: x[1])
for i in range(N-1):
    s, l = T[i:i+2]
    E.append([s[0], l[0], l[1]-s[1]])
T.sort(key=lambda x: x[2])
for i in range(N-1):
    s, l = T[i:i+2]
    E.append([s[0], l[0], l[2]-s[2]])
E.sort(key=lambda x: x[2])

Par = list(range(N))
def root(x):
    if Par[x] == x:
        return x
    else:
        Par[x] = root(Par[x])
        return Par[x]

def same(x, y):
    return root(x) == root(y)

def unite(x, y):
    x = root(x)
    y = root(y)
    if x != y:
        Par[x] = y

ans = 0
for e in E:
    if not same(e[0], e[1]):
        ans += e[2]
        unite(e[0], e[1])

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(500000)

N = int(input())
T = [[i] + list(map(int, input().split())) for i in range(N)]
E = [[] for i in range(N)]
E = []
T.sort(key=lambda x: x[1])
for i in range(N-1):
    s, l = T[i:i+2]
    E.append([s[0], l[0], l[1]-s[1]])
T.sort(key=lambda x: x[2])
for i in range(N-1):
    s, l = T[i:i+2]
    E.append([s[0], l[0], l[2]-s[2]])
E.sort(key=lambda x: x[2])

Par = list(range(N))
def root(x):
    if Par[x] == x:
        return x
    else:
        Par[x] = root(Par[x])
        return Par[x]

def same(x, y):
    return root(x) == root(y)

def unite(x, y):
    x = root(x)
    y = root(y)
    if x != y:
        Par[x] = y

ans = 0
for e in E:
    if not same(e[0], e[1]):
        ans += e[2]
        unite(e[0], e[1])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
Dy = [1,0,-1,0]
Dx = [0,-1,0,1]

H, W, K = map(int, input().split())
A = [input() for _ in range(H)]
Closed = [[False]*W for _ in range(H)]
for y, a in enumerate(A):
    for x, aa in enumerate(a):
        if aa=="S":
            ys, xs = y, x
q = [(ys, xs)]
Closed[ys][xs] = True
reachable = [(ys, xs)]
for _ in range(K):
    q_new = []
    for y_old, x_old in q:
        for dy, dx in zip(Dy, Dx):
            y, x = y_old + dy, x_old + dx
            if not (0<=y<H and 0<=x<W):
                continue
            if not Closed[y][x] and A[y][x]==".":
                Closed[y][x] = True
                reachable.append((y, x))
                q_new.append((y, x))
    q = q_new
mi = float("inf")
for y, x in reachable:
    mi = min(mi, x, y, W-1-x, H-1-y)
print((mi-1)//K+2)


Fixed submission:
def main():
    Dy = [1,0,-1,0]
    Dx = [0,-1,0,1]
    
    H, W, K = map(int, input().split())
    A = [input() for _ in range(H)]
    Closed = [[False]*W for _ in range(H)]
    for y, a in enumerate(A):
        for x, aa in enumerate(a):
            if aa=="S":
                ys, xs = y, x
    q = [(ys, xs)]
    Closed[ys][xs] = True
    reachable = [(ys, xs)]
    for _ in range(K):
        q_new = []
        for y_old, x_old in q:
            for dy, dx in zip(Dy, Dx):
                y, x = y_old + dy, x_old + dx
                if not (0<=y<H and 0<=x<W):
                    continue
                if not Closed[y][x] and A[y][x]==".":
                    Closed[y][x] = True
                    reachable.append((y, x))
                    q_new.append((y, x))
        q = q_new
    mi = float("inf")
    for y, x in reachable:
        mi = min(mi, x, y, W-1-x, H-1-y)
    print((mi-1)//K+2)

main()
Buggy python submission (verdict: Runtime Error):
N = int(input())

if N%2==1:
    print("First")
    exit()
elif N==2:
    print("Second")
    exit()

E = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

E_dim = [len(e) for e in E]
E_set = [set(e) for e in E]
q = []
for v, (dim, e) in enumerate(zip(E_dim, E)):
    if dim==1:
        for u in e:
            if E_dim[u] == 2:
                q.append(v)
                
while q:
    v = q.pop()
    u = E[v][0]
    if E_dim[u] == 1:
        print("Second")
        exit()
    E_set[u].remove(v)
    w = E_set[u].pop()
    E_set[w].remove(u)
    E_dim[w] -= 1
    if E_dim[w] == 1:
        x = E_set[w].copy().pop()
        if E_dim[x] == 2:
            q.append(w)
    elif E_dim[w] == 2:
        for x in E_set[w]:
            if E_dim[x] == 1:
                q.append(x)
print("First")



Fixed submission:
N = int(input())

if N%2==1:
    print("First")
    exit()
elif N==2:
    print("Second")
    exit()

E = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    E[a].append(b)
    E[b].append(a)

E_dim = [len(e) for e in E]
E_set = [set(e) for e in E]
q = []
for v, (dim, e) in enumerate(zip(E_dim, E)):
    if dim==1:
        for u in e:
            if E_dim[u] == 2:
                q.append(v)
                
while q:
    v = q.pop()
    u = E_set[v].pop()
    if E_dim[u] == 1:
        print("Second")
        exit()
    E_set[u].remove(v)
    w = E_set[u].pop()
    E_set[w].remove(u)
    E_dim[w] -= 1
    if E_dim[w] == 1:
        x = E_set[w].copy().pop()
        if E_dim[x] == 2:
            q.append(w)
    elif E_dim[w] == 2:
        for x in E_set[w]:
            if E_dim[x] == 1:
                q.append(x)
print("First")