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

Fixed submission:
N = int(input())
n = N%10
if n==3:
    print('bon')
elif n in (0,1,6,8):
    print('pon')
else:
    print('hon')

Buggy submission (verdict: Runtime Error):
print('pphbhhphph'.index(int(input())%10)+'on')
Problem p02764 (python)

Fixed submission:
N,K = map(int,input().split())
XYC = [tuple(map(int,input().split())) for i in range(N)]

from math import hypot,acos,atan2,cos,sin
def circles_cross_points(x1,y1,r1,x2,y2,r2):
    d = hypot(x1-x2, y1-y2)
    if r1 + r2 < d or abs(r1 - r2) >= d: return []
    x = (r1**2 + d**2 - r2**2) / (2*r1*d)
    if not -1 <= x <= 1: return []
    a = acos(x)
    t = atan2(y2-y1, x2-x1)
    return [
        (x1+cos(t+a)*r1, y1+sin(t+a)*r1),
        (x1+cos(t-a)*r1, y1+sin(t-a)*r1)
    ]

eps = 10e-10
def is_ok(t):
    cands = []
    for x,y,_ in XYC:
        cands.append((x,y))
    for i,(x1,y1,c1) in enumerate(XYC[:-1]):
        for x2,y2,c2 in XYC[i+1:]:
            ps = circles_cross_points(x1,y1,t/c1,x2,y2,t/c2)
            cands += ps
    for cx,cy in cands:
        k = 0
        for x,y,c in XYC:
            d = hypot(cx-x,cy-y)
            if c*d <= t + eps:
                k += 1
                if k >= K:
                    return True
    return False

ok = 10**10
ng = 0
for _ in range(100):
    m = (ok+ng)/2
    if is_ok(m):
        ok = m
    else:
        ng = m
    if ok-ng < 10e-7: break
print(ok)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
XYC = [tuple(map(int,input().split())) for i in range(N)]

from math import hypot,acos,atan2,cos,sin
def circles_cross_points(x1,y1,r1,x2,y2,r2):
    d = hypot(x1-x2, y1-y2)
    if r1 + r2 < d or abs(r1 - r2) >= d: return []
    a = acos((r1**2 + d**2 - r2**2) / (2*r1*d))
    t = atan2(y2-y1, x2-x1)
    return [
        (x1+cos(t+a)*r1, y1+sin(t+a)*r1),
        (x1+cos(t-a)*r1, y1+sin(t-a)*r1)
    ]

eps = 10e-10
def is_ok(t):
    cands = []
    for x,y,_ in XYC:
        cands.append((x,y))
    for i,(x1,y1,c1) in enumerate(XYC[:-1]):
        for x2,y2,c2 in XYC[i+1:]:
            ps = circles_cross_points(x1,y1,t/c1,x2,y2,t/c2)
            cands += ps
    for cx,cy in cands:
        k = 0
        for x,y,c in XYC:
            d = hypot(cx-x,cy-y)
            if c*d <= t + eps:
                k += 1
                if k >= K:
                    return True
    return False

ok = 10**10
ng = 0
for _ in range(100):
    m = (ok+ng)/2
    if is_ok(m):
        ok = m
    else:
        ng = m
print(ok)
Problem p02811 (python)

Fixed submission:
k,x = map(int,input().split())
print("Yes" if k*500 >= x else "No")

Buggy submission (verdict: Runtime Error):
k,x = map(int,input().split())
print("Yes" if k*500 >= x "No")
Problem p02814 (python)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
s = set()
B = []
for a in A:
    t = a & -a
    s.add(t)
    if len(s) > 1:
        print(0)
        exit()
    B.append(a // t)

from fractions import gcd
l = 1
for b in B:
    l = b*l // gcd(b,l)

x = l*t // 2
ans = M//x - M//(2*x)
print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
s = set()
B = []
for a in A:
    t = a & -a
    s.add(t)
    if len(s) > 1:
        print(0)
        exit()
    B.append(a // t)

from math import gcd
l = 1
for b in B:
    l = b*l // gcd(b,l)

x = l*t // 2
ans = M//x - M//(2*x)
print(ans)
Problem p02822 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)

def solve():
    N = int(input())
    AB = [tuple(map(int,input().split())) for i in range(N-1)]
    MOD = 10**9+7
    es = [[] for _ in range(N)]
    for a,b in AB:
        a,b = a-1,b-1
        es[a].append(b)
        es[b].append(a)

    cs = [1] * N
    def dfs(v,p=-1):
        ret = 1
        for to in es[v]:
            if to==p: continue
            ret += dfs(to,v)
        cs[v] = ret
        return ret
    dfs(0)

    pow2 = [1]
    for i in range(N):
        pow2.append((pow2[-1]*2) % MOD)

    ans = 0
    for i in range(N):
        if len(es[i]) == 1: continue
        ans += pow2[N-1] - 1
        c = 0
        for to in es[i]:
            if cs[to] > cs[i]: continue
            c += cs[to]
            ans -= pow2[cs[to]] - 1
        ans -= pow2[N-c-1] - 1

    ans *= pow(pow2[N], MOD-2, MOD)
    print(ans%MOD)
solve()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)

N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
MOD = 10**9+7
es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

cs = [1] * N
def dfs(v,p=-1):
    ret = 1
    for to in es[v]:
        if to==p: continue
        ret += dfs(to,v)
    cs[v] = ret
    return ret
dfs(0)
ans = 0
for i in range(N):
    if len(es[i]) == 1: continue
    ans += pow(2,N-1,MOD) - 1
    c = 0
    for to in es[i]:
        if cs[to] > cs[i]: continue
        c += cs[to]
        ans -= pow(2,cs[to],MOD) - 1
    ans -= pow(2,N-c-1,MOD) - 1

ans *= pow(2,-N,MOD)
print(ans%MOD)
Problem p02837 (python)

Fixed submission:
N = int(input())
xs = [[] for i in range(N)]
for i in range(N):
    a = int(input())
    for j in range(a):
        x,y = map(int,input().split())
        xs[i].append((x-1,y))
ans = 0
for b in range(1<<N):
    mujun = False
    for k,z in enumerate(xs):
        for x,y in z:
            if b&(1<<k):
                if b&(1<<x):
                    if y==0:
                        mujun = True
                else:
                    if y==1:
                        mujun = True
            if mujun:
                break
        if mujun:
            break
    if not mujun:
        tmp = bin(b).count('1')
        ans = max(ans, tmp)
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
xys = [[] for _ in range(N)]
for i in range(N):
    A = int(input())
    for j in range(A):
        x,y = map(int,input().split())
        xys[i].append((x-1,y))

ans = 0
for b in range(1<<N):
    cnt = bin(b).count('1')
    if cnt <= ans: continue
    for i in range(N):
        if b&(1<<i):
        for x,y in xys[i]:
                if y==1 and b&(1<<x): continue
                if y==0 and b&(1<<x)==0: continue
                break
            else:
                continue
            break
    else:
        ans = cnt
print(ans)
Problem p02889 (python)

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist = [[INF]*N for i in range(N)]
st = set()
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist[a][b] = dist[b][a] = c
    st.add((a,b))
    st.add((b,a))
for i in range(N):
    dist[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][k] == INF or dist[k][j] == INF: continue
            a,x = divmod(dist[i][k],L)
            b,y = divmod(dist[k][j],L)
            if x+y > L:
                dist[i][j] = min(dist[i][j], (a+b+1)*L + min(x,y))
            else:
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
            print(i,j,dist[i][j])

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if (s,t) in st:
        ans.append(0)
    elif dist[s][t] == INF:
        ans.append(-1)
    else:
        ans.append(dist[s][t])//L))
print(*ans, sep='\n')
Problem p02936 (python)

Fixed submission:
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

xs = [0] * N
for p,x in PX:
    xs[p-1] += x

ans = [0] * N
ans[0] = xs[0]
visited = [0] * N
stack = [0]
while stack:
    v = stack.pop()
    visited[v] = 1
    for to in es[v]:
        if visited[to]: continue
        stack.append(to)
        ans[to] = ans[v] + xs[to]

print(*ans)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]
es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)
ps = [0] * N
for p,x in PX:
    ps[p-1] += x

ans = [0] * N
def dfs(v,p=-1, c=0):
    ans[v] = c + ps[v]
    for to in es[v]:
        if to==p: continue
        dfs(to, v, ans[v])
dfs(0)
print(*ans)
Problem p02941 (python)

Fixed submission:
import heapq
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
import heapq
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

cnt = 0
for a,b in zip(A,B):
    if a != b:
        cnt += 1
if cnt == 0:
    print(0)
    exit()

hq = [(-a,i) for i,a in enumerate(B)]
heapq.heapify(hq)

ans = 0
while hq or cnt:
    _,i = heapq.heappop(hq)
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] == B[i]:
        cnt -= 1
        continue
    heapq.heappush(hq, (-B[i],i))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)
Problem p02985 (python)

Fixed submission:
N,K = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

MOD = 10**9+7

fac = [1,1] + [0]*K
finv = [1,1] + [0]*K
inv = [0,1] + [0]*K
for i in range(2,K+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def npr(n,r):
    if n<r: return 0
    return fac[n] * finv[n-r]

ans = K * npr(K-1,len(es[0]))
ans %= MOD

visited = [0]*N
visited[0] = 1
stack = []
for v in es[0]:
    stack.append(v)

while stack:
    v = stack.pop()
    visited[v] = 1
    ans *= npr(K-2, len(es[v])-1)
    ans %= MOD
    for to in es[v]:
        if visited[to]: continue
        stack.append(to)
print(ans%MOD)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
MOD = 10**9+7

es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

MAXN = N+5
fac = [1,1] + [0]*MAXN
finv = [1,1] + [0]*MAXN
inv = [0,1] + [0]*MAXN
for i in range(2,MAXN+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def npr(n,r):
    if n<r: return 0
    return fac[n] * finv[n-r]


ans = K
stack = [(0,0)]
visited = [0]*N
while stack:
    v,d = stack.pop()
    visited[v] = 1
    c = 0
    for to in es[v]:
        if visited[to]: continue
        c += 1
        stack.append((to,d+1))
    if c==0: continue
    m = npr(K-1,c) if d==0 else npr(K-2,c)
    ans *= m
    ans %= MOD
print(ans)
Problem p03128 (python)

Fixed submission:
from collections import defaultdict
from copy import copy

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

ms = [2,5,5,4,5,6,3,7,6]
dic = defaultdict(lambda: 0)
for a in A:
    dic[ms[a-1]] = max(dic[ms[a-1]], a)

sd = sorted(dic.items())

dp = [None] * (N+1)
dp[0] = defaultdict(lambda: 0)

for i in range(sd[0][0], N+1):
    cands = []
    least_len = -1
    for l,a in sd:
        if i-l < 0: break
        if dp[i-l] is None: continue
        if cands and sum(dp[i-l].values())+1 < least_len:
            continue
        cands.append(copy(dp[i-l]))
        cands[-1][a] += 1
        least_len = sum(cands[-1].values())
    if not cands: continue
    max_v = 0
    max_j = -1
    for j,cand_dic in enumerate(cands):
        tmp_s = ''
        for a,n in sorted(cand_dic.items(), reverse=True):
            tmp_s += str(a) * n
        tmp_v = int(tmp_s)
        if tmp_v > max_v:
            max_v = tmp_v
            max_j = j
    dp[i] = cands[max_j]

ans = ''
for a,n in sorted(dp[-1].items(), reverse=True):
    ans += str(a) * n
print(ans)

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

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

ms = [2,5,5,4,5,6,3,7,6]
dic = defaultdict(lambda: 0)
for a in A:
    dic[ms[a-1]] = max(dic[ms[a-1]], a)

sd = sorted(dic.items())

dp = [None] * (N+1)
dp[0] = defaultdict(lambda: 0)

for i in range(sd[0][0], N+1):
    cands = []
    for l,a in sd:
        if i-l < 0: break
        if dp[i-l] is None: continue
        cands.append(copy(dp[i-l]))
        cands[-1][a] += 1
    if not cands: continue
    max_v = 0
    max_i = -1
    for i,cand_dic in enumerate(cands):
        tmp_s = ''
        for a,n in sorted(cand_dic.items(), reverse=True):
            tmp_s += str(a) * n
        tmp_v = int(tmp_s)
        if tmp_v > max_v:
            max_v = tmp_v
            max_i = i
    dp[i] = cands[i]

ans = ''
for a,n in sorted(dp[-1].items(), reverse=True):
    ans += str(a) * n
print(ans)
Problem p03138 (python)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))

bs = [0] * 40
for a in A:
    for k in range(40):
        if a&(1<<k):
            bs[k] += 1

x = 0
for k in range(39,-1,-1):
    if x + (1<<k) > K: continue
    if bs[k] < (N+1)//2:
        x += (1<<k)

ans = 0
for a in A:
    ans += x^a
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))

bs = [0] * 40
for a in A:
    for k in range(40):
        if a&(1<<k):
            bs[k] += 1

x = 0
for k in range(39,-1,-1):
    if x + (1<<k) > K: continue
    if bs[k] < (N+1)s//2:
        x += (1<<k)

ans = 0
for a in A:
    ans += x^a
print(ans)
Problem p03148 (python)

Fixed submission:
from collections import Counter
N,K = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:-x[1])

selected = Counter()
ans = 0
stack = []

for t,d in src[:K]:
    ans += d
    selected[t] += 1
    stack.append((t,d))
ans += len(selected)**2

tmp = ans
for t,d in src[K:]:
    if t in selected: continue
    while stack:
        t0,d0 = stack.pop()
        if selected[t0] > 1:
            tmp += d - d0 + 2*len(selected)+1
            ans = max(ans, tmp)
            selected[t0] -= 1
            selected[t] += 1
            break
    else:
        break
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
TD = [tuple(map(int,input().split())) for i in range(N)]
TD.sort(key=lambda x:x[1])

import heapq
hq = []
heapq.heapify(hq)
selected = set()
ans = 0
for _ in range(K):
    t,d = TD.pop()
    if t in selected:
        heapq.heappush(hq, d)
    else:
        selected.add(t)
    ans += d
ans += len(selected)**2

tmp = ans
while TD:
    t,d = TD.pop()
    if t in selected: continue
    selected.add(t)
    tmp -= heapq.heappop(hq)
    tmp += d
    tmp += len(selected)*2 - 1
    ans = max(ans ,tmp)
print(ans)
Problem p03165 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)

S = input()
T = input()
N = len(S)
M = len(T)

dp = [[0]*(M+1) for i in range(N+1)]

for i in range(N):
    for j in range(M):
        if S[i] == T[j]:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

ans = ''
def rec(i,j):
    if i==0 or j==0: return
    if S[i-1] == T[j-1]:
        global ans
        ans += S[i-1]
        rec(i-1,j-1)
    elif dp[i-1][j] >= dp[i][j-1]:
        rec(i-1,j)
    else:
        rec(i,j-1)

rec(N,M)
print(ans[::-1])

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
N = len(S)
M = len(T)

dp = [[0]*(M+1) for i in range(N+1)]

for i in range(N):
    for j in range(M):
        if S[i] == T[j]:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

ans = ''
def rec(i,j):
    print(i,j)
    if i==0 or j==0: return
    if S[i-1] == T[j-1]:
        global ans
        ans += S[i-1]
        rec(i-1,j-1)
    elif dp[i-1][j] >= dp[i][j-1]:
        rec(i-1,j)
    else:
        rec(i,j-1)

rec(N,M)
print(ans[::-1])
Problem p03178 (python)

Fixed submission:
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for r in range(D):
            for d in range(lim+1):
                dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
                dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)

Buggy submission (verdict: Runtime Error):
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for d in range(lim+1):
            dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
            dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)
Problem p03249 (python)

Fixed submission:
N = int(input())
src = [int(input()) for i in range(N)]
idx = {a:i for i,a in enumerate(src)}

size = [1] * N
ss = list(sorted(src))

es = []
while len(ss) > 1:
    a = ss.pop()
    k = size[idx[a]]
    b = a + 2*k - N
    if b == a or b not in idx:
        print(-1)
        exit()
    size[idx[b]] += k
    es.append((idx[a],idx[b]))

gr = [[] for i in range(N)]
for a,b in es:
    gr[a].append(b)
    gr[b].append(a)

from collections import deque
dist = [N] * N
dist[idx[ss[0]]] = 0
q = deque([idx[ss[0]]])
while q:
    v = q.popleft()
    for to in gr[v]:
        if dist[to] < N: continue
        q.append(to)
        dist[to] = dist[v] + 1

if all(d<N for d in dist) and sum(dist) == ss[0]:
    for a,b in es:
        print(a+1, b+1)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N = int(input())
src = [int(input()) for i in range(N)]
idx = {a:i for i,a in enumerate(src)}

size = [1] * N
ss = list(sorted(src))

es = []
gr = [[] for i in range(N)]
while len(ss) > 1:
    a = ss.pop()
    k = size[idx[a]]
    b = a + 2*k - N
    if b == a or b not in idx:
        print(-1)
        exit()
    size[idx[b]] += k
    ai,bi = idx[a],idx[b]
    es.append((ai,bi))
    gr[a].append(b)
    gr[b].append(a)

from collections import deque
dist = [N] * N
dist[idx[ss[0]]] = 0
q = deque([idx[ss[0]]])
while q:
    v = q.popleft()
    for to in gr[v]:
        if dist[to] < N: continue
        q.append(to)
        dist[to] = dist[v] + 1

if all(d<N for d in dist) and sum(dist) == ss[0]:
    for a,b in es:
        print(a+1, b+1)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N = int(input())
src = [int(input()) for i in range(N)]
idx = {a:i for i,a in enumerate(src)}

size = [1] * N
ss = list(sorted(src))

es = []
while len(ss) > 1:
    a = ss.pop()
    k = size[idx[a]]
    b = a + 2*k - N
    if b == a or b not in idx:
        print(-1)
        exit()
    size[idx[b]] = k + 1
    es.append((idx[a],idx[b]))

assert False

gr = [[] for i in range(N)]
for a,b in es:
    gr[a].append(b)
    gr[b].append(a)

from collections import deque
dist = [N] * N
dist[idx[ss[0]]] = 0
q = deque([idx[ss[0]]])
while q:
    v = q.popleft()
    for to in gr[v]:
        if dist[to] < N: continue
        q.append(to)
        dist[to] = dist[v] + 1

if all(d<N for d in dist) and sum(dist) == ss[0]:
    for a,b in es:
        print(a+1, b+1)
else:
    print(-1)
Problem p03274 (python)

Fixed submission:
N,K = map(int,input().split())
src = list(map(int,input().split()))

ans = float('inf')
for l in range(N-K+1):
    r = l + K - 1
    if src[l] > 0:
        ans = min(ans, src[r])
    elif src[r] < 0:
        ans = min(ans, -src[l])
    else:
        ans = min(ans, src[r]*2 - src[l], -src[l]*2 + src[r])
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    if l >= len(ls): break
    r = K-l
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)
Problem p03280 (python)

Fixed submission:
A,B = map(int,input().split())
print(A*B - A - B + 1)

Buggy submission (verdict: Runtime Error):
A = int(input())
B = int(input())
print(A*B - A - B + 1)
Problem p03282 (python)

Fixed submission:
S = input()
K = int(input())

ones = 0
for c in S:
    if c == '1':
        ones += 1
    else:
        n = c
        break

print(1 if ones >= K else n)

Buggy submission (verdict: Runtime Error):
S = input()
K = int(input())

i = 0
while S[i]=='1':
    i += 1

if i >= K:
    print(1)
else:
    print(S[i])
Problem p03288 (python)

Fixed submission:
R = int(input())
if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
else:
    print('AGC')

Buggy submission (verdict: Runtime Error):
R = int(input())
if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
else:
    prnit('AGC')
Problem p03361 (python)

Fixed submission:
H,W = map(int,input().split())
src = [input() for i in range(H)]

dxy = [(-1,0),(0,-1),(1,0),(0,1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '.': continue
        for dx,dy in dxy:
            nx,ny = x+dx,y+dy
            if not 0 <= nx < W: continue
            if not 0 <= ny < H: continue
            if src[ny][nx] == '#':
                break
        else:
            print('No')
            exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
src = [input() for i in range(H)]

dxy = [(0,1),(1,0),(0,-1),(-1,0)]
def is_isolated(x,y):
    if src[y][x] == '.': return False
    for dx,dy in dxy:
        if not 0 <= x+dx < W: continue
        if not 0 <= y+dy < H: continue
        if src[y+dy][x+dx] == '#': return False
    else:
        return True

for y in range(H):
    for x in range(W):
        if is_isolated(y,x):
            print('No')
            exit()
print('Yes')