Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N, = map(int, input().split())
R = [0] * (N**2+1)
V = [0] * (N**2+1)
X = list(map(int, input().split()))
for ai in range(N**2):
    i, j = divmod(ai, N)
    R[ai+1] = min([i, N-i-1, j, N-j-1])

r = 0
for a in X:
    r += R[a]
    V[a] = 1
    stack=[a]
    while stack:
        v = stack.pop()
        i, j = divmod(v-1, N)
        ccs = []
        if v>N:
            ccs.append(v-N)
        if v<N**2+1-N:
            ccs.append(v+N)
        if v%N:
            ccs.append(v+1)
        if v%N!=1:
            ccs.append(v-1)
        for u in ccs:
            if R[v]>=R[u] or ((1-V[v]) and R[v]+1 >= R[u]):
                continue
            R[u] -= 1
            stack.append(u)
print(r)


Fixed submission:
N, = map(int, input().split())
R = [0] * (N**2+1)
V = [0] * (N**2+1)
X = list(map(int, input().split()))
for ai in range(N**2):
    i, j = divmod(ai, N)
    R[ai+1] = min([i, N-i-1, j, N-j-1])

r = 0
for a in X:
    r += R[a]
    V[a] = 1
    stack=[a]
    while stack:
        v = stack.pop()
        ccs = []
        if v>N:
            ccs.append(v-N)
        if v<N**2+1-N:
            ccs.append(v+N)
        if v%N:
            ccs.append(v+1)
        if v%N!=1:
            ccs.append(v-1)
        for u in ccs:
            if R[v]>=R[u] or ((1-V[v]) and R[v]+1 >= R[u]):
                continue
            R[u] -= 1
            stack.append(u)
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
N, = map(int, input().split())
R = [0] * (N**2+1)
V = [0] * (N**2+1)
X = list(map(int, input().split()))
for ai in range(N**2):
    i, j = divmod(ai, N)
    R[ai+1] = min([i, N-i-1, j, N-j-1])
#print(R)

r = 0
for a in X:
    r += R[a]
    V[a] = 1
    stack=[a]
    vs = set([a])
    while stack:
        v = stack.pop()
        i, j = divmod(v-1, N)
        ccs = []
        if v>N:
            ccs.append(v-N)
        if v<N**2+1-N:
            ccs.append(v+N)
        if v%N:
            ccs.append(v+1)
        if v%N!=1:
            ccs.append(v-1)
        for u in ccs:
            if u in vs:
                continue
            if V[v] and R[v] >= R[u]:
                continue
            if (1-V[v]) and R[v] +1 >= R[u]:
                continue
            R[u] -= 1
            vs.add(u)
            stack.append(u)
print(r)


Fixed submission:
N, = map(int, input().split())
R = [0] * (N**2+1)
V = [0] * (N**2+1)
X = list(map(int, input().split()))
for ai in range(N**2):
    i, j = divmod(ai, N)
    R[ai+1] = min([i, N-i-1, j, N-j-1])

r = 0
for a in X:
    r += R[a]
    V[a] = 1
    stack=[a]
    while stack:
        v = stack.pop()
        ccs = []
        if v>N:
            ccs.append(v-N)
        if v<N**2+1-N:
            ccs.append(v+N)
        if v%N:
            ccs.append(v+1)
        if v%N!=1:
            ccs.append(v-1)
        for u in ccs:
            if R[v]>=R[u] or ((1-V[v]) and R[v]+1 >= R[u]):
                continue
            R[u] -= 1
            stack.append(u)
print(r)
Buggy python submission (verdict: Runtime Error):
from fractions import Fraction
N, = map(int, input().split())
d = dict()
X = []
cc = 0
for _ in range(N):
    a, b = map(int, input().split())
    if a == 0 and b==0:
        cc += 1
        continue
    f = Fraction(a, b)
    if f not in d:
        d[f] = 0
    d[f] += 1
    X.append((a, b))

vs = dict()
cs = []
for a, b in X:
    f1 = Fraction(a, b)
    f2 = Fraction(b, -a)
    if f2 not in d:
        d[f2] = 0
#        cs.append(d[f1])
#        continue
    if f1 in vs or f2 in vs:
        continue
    vs[f1] = (d[f1], d[f2])
mod = 10**9+7

tmp = []
r = 1
#print(pow(2, sum(cs),mod))
r2 = 1
for k in vs:
    a, b = vs[k]
#    print((pow(2, a, mod), pow(2, b, mod)))
    r = r * (pow(2, a, mod) + pow(2, b, mod)-1) % mod
#    r2 = r2 * (pow(2, a-1, mod) + pow(2, b-1, mod)+2) % mod

print(r-1+cc)
#print(len(vs))
#print(r2)
#print(r2*pow(2, sum(cs),mod)+1)


Fixed submission:
from math import gcd
def to_standard_form(a, b):
    if a == 0:
        return (0, 1)
    g = gcd(a, b)
    a //= g
    b //= g
    if a < 0:
        return (-a, -b)
    return (a, b)

N, = map(int, input().split())
d = dict()
X = []
cc = 0
for _ in range(N):
    a, b = map(int, input().split())
    if a == 0 and b==0:
        cc += 1
        continue
    f = to_standard_form(a, b)
    if f not in d:
        d[f] = 0
    d[f] += 1
    X.append(f)

vs = dict()
for a, b in X:
    f1 = (a, b)
    f2 = to_standard_form(-b, a)
    if f2 not in d:
        d[f2] = 0
    if f1 in vs or f2 in vs:
        continue
    vs[f1] = (d[f1], d[f2])

mod = 10**9+7
r = 1
for k in vs:
    a, b = vs[k]
    r = r * (pow(2, a, mod) + pow(2, b, mod)-1) % mod
print((r-1+cc)%mod)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
if N%2:
    for i in range(1, M+1):
        print(1/0)
        print(i, N-i)
else:
    for i in range(1, M+1):
        if i <= -(-(N//2)//2):
            print(i, N-i+1)
        else:
            print(i+1, N-i+1)


Fixed submission:
N, M = map(int, input().split())
if N%2:
    for i in range(1, M+1):
        print(i, N-i)
else:
    if M%2:
        for i in range(1, M+1):
            if i <= -(-(N//2)//2):
                print(i, N-i+1)
            else:
                print(i+1, N-i+1)
    else:
        for i in range(1, M+1):
            if i < -(-(N//2)//2):
                print(i, N-i+1)
            else:
                print(i+1, N-i+1)
Buggy python submission (verdict: Runtime Error):
N, K, C = map(int, input().split())
S = input().strip()
dp1 = [0]*(N+2)
dp2 = [0]*(N+2)
c = 10*18
cc = 0
for i in range(N):
    if S[i] == "o" and c >= C:
        cc += 1
        c = 0
    else:
        c += 1
    dp1[i+1] = cc
c = 10*18
cc = 0
for i in range(N-1, -1, -1):
    if S[i] == "o" and c >= C:
        cc += 1
        c = 0
    else:
        c += 1
    dp2[i+1] = cc
rs = []
for i in range(N):
    if S[i+1] == "o" and dp1[i] + dp2[i+2] == K-1:
        rs.append(i+1)

#print(dp1, dp2)
for r in rs:
    print(r)


Fixed submission:
N, K, C = map(int, input().split())
S = input().strip()
dp1 = [0]*(N+2)
dp2 = [0]*(N+2)
c = 10*18
cc = 0
for i in range(N):
    if S[i] == "o" and c >= C:
        cc += 1
        c = 0
    else:
        c += 1
    dp1[i+1] = cc
c = 10*18
cc = 0
for i in range(N-1, -1, -1):
    if S[i] == "o" and c >= C:
        cc += 1
        c = 0
    else:
        c += 1
    dp2[i+1] = cc
rs = []
for i in range(N):
    if S[i] == "o" and dp1[i] + dp2[i+2] == K-1 and dp1[i+1] + dp2[i+1] - 1 == K:
        rs.append(i+1)

#print(dp1, dp2)
for r in rs:
    print(r)
Buggy python submission (verdict: Runtime Error):

N, T = map(int, input().split())
X = []
for _ in range(N):
    a, b = map(int, input().split())
    X.append((a,b))

X.sort(key=lambda x:x[0]/(x[1]+1))
X=X[::-1]

dp = [[0]+[T+2]*29 for _ in range(N+1)]
bs = []
for i, (a, b) in enumerate(X):
    if a==0:
        bs.append(b)
        continue
    for j in range(1, 30):
#        if j==1:
#            print(a*(dp[i][j-1]+1)+b)
        dp[i+1][j] = min(min(dp[i][j], a*(dp[i][j-1]+1)+b), T+1)
    fi = i+1

#print(dp)

bs.sort()
bbs = [0]
for i, b in enumerate(bs):
    bbs.append(bbs[-1]+bs[i]+1)
bbs.append(10**18)


#print(dp[fi], bbs)
r=0
for i in range(30):
    if dp[fi][i] > T:
        break
    nt = T-dp[fi][i]
#    print(nt)
    for j, b in enumerate(bbs):
#        print(1, b)
        if b > nt:
            break
    r=max(r, i+j-1)
print(r)


Fixed submission:

N, T = map(int, input().split())
X = []
for _ in range(N):
    a, b = map(int, input().split())
    X.append((a,b))

X.sort(key=lambda x:x[0]/(x[1]+1))
X=X[::-1]

dp = [[0]+[T+2]*29 for _ in range(N+1)]
bs = []
fi=0
for i, (a, b) in enumerate(X):
    if a==0:
        bs.append(b)
        continue
    for j in range(1, 30):
#        if j==1:
#            print(a*(dp[i][j-1]+1)+b)
        dp[i+1][j] = min(min(dp[i][j], (a+1)*(dp[i][j-1]+1)+b), T+1)
    fi = i+1

#print(dp)

bs.sort()
bbs = [0]
for i, b in enumerate(bs):
    bbs.append(bbs[-1]+bs[i]+1)
bbs.append(10**18)


#print(dp[fi], bbs)
r=0
for i in range(30):
    if dp[fi][i] > T:
        break
    nt = T-dp[fi][i]
#    print(nt)
    for j, b in enumerate(bbs):
#        print(1, b)
        if b > nt:
            break
    r=max(r, i+j-1)
print(r)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
pY = []
mY = []
z = 0
for x in X:
    if x > 0:
        pY.append(x)
    elif x < 0:
        mY.append(x)
    else:
        z += 1

def bsearch(mn, mx, func):
    #func(i)=False を満たす最大のi
    idx = (mx + mn)//2
    while mx-mn>1:
        if func(idx):
            idx, mx = (idx + mn)//2, idx
            continue
        idx, mn = (idx + mx)//2, idx
    return idx


def count2(k):
    c = 0
    for y in pY:
        c += bsearch(-1, N, lambda i:X[i]*y>k) + 1
    for y in mY:
        c += N-(bsearch(-1, N, lambda i:X[i]*y<=k)+1)
    if k >= 0:
        c += N*z
    for x in X:
        if x*x <=k:
            c -= 1
    c = c//2
    return c


def count(k):
    # x*y <= k を満たすx,yの個数
    c = 0
    for i in range(N):
        for j in range(i+1, N):
            if X[i]*X[j] <= k:
                c += 1
    return c

x = bsearch(-10**18, 10**18, lambda x : count2(x)>=K)
print(x+1)


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
mA, nA, zc = [], [], 0
dd = []
for a in A:
    if a<0:
        mA.append(a)
    elif a>0:
        nA.append(a)
    else:
        zc += 1
    dd.append(a*a)
#rA = A[::-1]
#print(mA, nA)
lnA = len(nA)
lmA = len(mA)

def F(idx):
    j = lnA-1
    r = 0
    for a in nA:
        while j >= 0:
            if nA[j]*a <= idx:
                break
            j -= 1
        r += j+1
    if idx >= 0:
#        print(2*(zc*N) - zc*zc)
        r += 2*(zc*N) - zc*zc
#    print(r, zc)
    j = 0
    for a in mA[::-1]:
        while j < lmA:
            if mA[j]*a <= idx:
                break
            j += 1
        r += lmA-j
    j = 0
    for a in mA:
        while j < lnA:
            if nA[j]*a <= idx:
                break
            j += 1
        r += 2*(lnA-j)

    for a in dd:
        if a <= idx:
            r-=1
    return r//2
#print(F(448283280358331064))

mx, mn = 2*10**18, -2*10**18
idx = (mx + mn)//2
target = K
while mx - mn > 1:
    if F(idx) < target:
        idx, mn = (idx + mx)//2, idx
        continue
    idx, mx = (idx + mn)//2, idx
print(idx+1)
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
s="#"*W+"".join("#"+input() for _ in range(H))
f=(W+1)*(H+1)
d=[[0]*f for _ in range(f)]
for i in range(W,f-1):
 d[i][i-1]=s[i]==s[i-1]=="."
 d[i][i-W-1]=s[i]==s[i-W-1]=="."
from scipy.sparse.csgraph import csgraph_from_dense as C,floyd_warshall as F
Q=F(C(d),directed=False)
r=-1
for q in Q:
 for x in q:
  if f>x:
   r=max(r,int(x))
print(r)

Fixed submission:
H, W = map(int, input().split())
s = "#"*(W+2)
for _ in range(H):
    s+="#"+input().strip()+"#"
s += "#"*(W+2)

#for i, c in enumerate(s):
#    print(i, c)
ff = (W+2)*(H+2)
from scipy.sparse.csgraph import csgraph_from_dense,floyd_warshall
dp = [[-1]*ff for _ in range(ff)]
for i in range(1, H+1):
    for j in range(1, W+1):
        u = j+(W+2)*i
        v = j+(W+2)*(i-1)
        if s[u] == "." and s[u-1] == ".":
#            print(u,u-1)
            dp[u][u-1] = 1
        if s[u] == "." and s[v] == ".":
#            print(u,v)
            dp[u][v] = 1
#print(dp)
G = csgraph_from_dense(dp,  null_value=-1)
Q = floyd_warshall(G, directed=False)
r = -1
for q in Q:
    for x in q:
        if x < 10**9:
            r = max(r, int(x))
print(r)
Buggy python submission (verdict: Time Limit Exceeded):
N,*X=map(int,open(0).read().split());M,g,r,h,i=10**9+7,1,0,[1],N;exec("i-=1;h+=[h[-1]*i%M];"*N+"r,g=(r+g*h[N-2-i]*(X[-1]-X[i]))%M,g*-~i%M;i+=1;"*N);print(r)

Fixed submission:
N = int(input())
Xs = list(map(int, input().split()))
mod = 10**9 + 7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
NNN = (10**6)
g1 = [1, 1]
inverse = [0, 1]
for i in range( 2, NNN + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )

mxx = Xs[-1]
r = 0
r2 = 0
for i in range(N-1):
    r = (r+mul(mul(g1[N-1],inverse[i+1]),Xs[N-1]-Xs[i]))%mod
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
Xs = [0] + list(map(int, input().split()))
mod = 10**9 + 7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

NNN = (10**6)
g1 = [1, 1]
inverse = [0, 1]

for i in range( 2, NNN + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )

mxx = Xs[-1]
r = 0
for i in range(N):
    i += 1
    for j in range(1, N-i):
        ue = mul(g1[N-1], Xs[i+j] - Xs[i])
        ff = mul(mul(ue, inverse[j+1]), inverse[j])
        r = (r + ff) % mod
    ue = mul(g1[N-1], Xs[N]-Xs[i])
    ff = mul(ue,inverse[N-i])
    r = (r+ff)%mod

print(r)


Fixed submission:
N = int(input())
Xs = list(map(int, input().split()))
mod = 10**9 + 7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
NNN = (10**6)
g1 = [1, 1]
inverse = [0, 1]
for i in range( 2, NNN + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )

mxx = Xs[-1]
r = 0
r2 = 0
for i in range(N-1):
    r = (r+mul(mul(g1[N-1],inverse[i+1]),Xs[N-1]-Xs[i]))%mod
print(r)
Buggy python submission (verdict: Time Limit Exceeded):
#def gcd(a, b):
#    while b:
#        a, b = b, a % b
#    return a
#
#def lcm(a, b):
#    return a * b // gcd (a, b)

import fractions
from functools import reduce
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


N,M = map(int, input().split())
xs = list(map(int, input().split()))
b = None
for x in xs:
    for i in range(30):
        if x%(2**i):
            break
    if b!=None:
        if b!=i:
            b = -1
            break
    b = i
if b==-1:
    print(0)
else:
    r = lcm_list(xs)
    if r//2 > M:
        print(0)
    else:
        print((M-r//2)//r+1)


Fixed submission:
#def gcd(a, b):
#    while b:
#        a, b = b, a % b
#    return a
#
#def lcm(a, b):
#    return a * b // gcd (a, b)

import fractions
from functools import reduce
def lcm_base(x, y):
    r = (x * y) // fractions.gcd(x, y)
    if r > 2*M:
        return 0
    return r

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


N,M = map(int, input().split())
xs = list(map(int, input().split()))
b = None
for x in xs:
    for i in range(30):
        if x%(2**i):
            break
    if b!=None:
        if b!=i:
            b = -1
            break
    b = i
if b==-1:
    print(0)
else:
    r = lcm_list(xs)
    if r//2 > M:
        print(0)
    elif r == 0:
        print(0)
    else:
        print((M-r//2)//r+1)
Buggy python submission (verdict: Time Limit Exceeded):
import copy
import sys
sys.setrecursionlimit(100000)

N,U,V = map(int, input().split())
d = [set() for _ in range(N+1)]
dps = [0 for _ in range(N+1)]
dpss = [0 for _ in range(N+1)]
vs = set()
r = {}

def it(v, p, depth, ps):
    for u in d[v]:
        if u == p:
            continue
        dps[u] = depth
        if u == V:
            r["p"] = copy.deepcopy(ps)
            continue
        ps.add(u)
        it(u, v, depth+1,ps)
        ps.remove(u)
        vs.add(u)

def it2(v, p, depth):
    for u in d[v]:
        if u == p:
            continue
        if u not in vs:
            continue
        if dps[v] >= depth-1 and u not in r["p"]:
            continue
        dpss[u] = depth
        it2(u, v, depth+1)



for _ in range(N-1):
    a, b = [int(x) for x in input().split()]
    d[a].add(b)
    d[b].add(a)

vs.add(U)
it(U, -1, 1, set([U]))
it2(V, -1, 1)
print(max(dpss)-1)


Fixed submission:
import copy
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline

N,U,V = map(int, input().split())
d = [set() for _ in range(N+1)]
dps = [0 for _ in range(N+1)]
dpss = [0 for _ in range(N+1)]
vs = set()
r = {}

def it(v, p, depth, ps):
    for u in d[v]:
        if u == p:
            continue
        dps[u] = depth
        if u == V:
            r["p"] = copy.deepcopy(ps)
            continue
        ps.add(u)
        it(u, v, depth+1,ps)
        ps.remove(u)
        vs.add(u)

def it2(v, p, depth):
    for u in d[v]:
        if u == p:
            continue
        if u not in vs:
            continue
        if dps[v] >= depth-1 and u not in r["p"]:
            continue
        dpss[u] = depth
        it2(u, v, depth+1)



for _ in range(N-1):
    a, b = [int(x) for x in input().split()]
    d[a].add(b)
    d[b].add(a)

vs.add(U)
it(U, -1, 1, set([U]))
it2(V, -1, 1)
print(max(dpss)-1)
Buggy python submission (verdict: Runtime Error):
t1,t2 = map(int, input().split()))
a1,a2 = map(int, input().split()))
b1,b2 = map(int, input().split()))
x,y=(a1-b1)*t1 + (a2-b2)*t2, (a1-b1)*t1
if x == 0:
    print("infinity")
elif x*y > 0:
    print(0)
else:
    s,t = divmod(abs(y), abs(x))
    print(s*2+1 if t else 0)

Fixed submission:
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
elif x*(a1-b1) > 0:
    print(0)
else:
    s,t = divmod(abs((a1-b1)*t1), abs(x))
    if t:
        print(s*2+1)
    else:
        print(s*2)

Buggy python submission (verdict: Runtime Error):
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
if x*(a1-b1) > 0:
    print(0)
else:
    s,t = divmod(abs((a1-b1)*t1), abs(x))
    if t:
        print(s*2+1)
    else:
        print(s*2)


Fixed submission:
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
elif x*(a1-b1) > 0:
    print(0)
else:
    s,t = divmod(abs((a1-b1)*t1), abs(x))
    if t:
        print(s*2+1)
    else:
        print(s*2)
Buggy python submission (verdict: Runtime Error):
n=int(input())
s=input().strip()
a='ABCDEFGHIJKLMNLPQRSTUVWXYZ'
aa = dict()
for i, c in enumerate(a):
    aa[c] = i
def f(c):
    return a[(aa[c] + n)%26]

r = ''
for c in s:
    r += f(c)
print(r)


Fixed submission:
n=int(input())
s=input().strip()
a='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
aa = dict()
for i, c in enumerate(a):
    aa[c] = i
def f(c):
    return a[(aa[c] + n)%26]

r = ''
for c in s:
    r += f(c)
print(r)
Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
ees = dict()
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
    ees[v+1] = tuple([min(a,b), max(a,b)])
cd = dict()
cd[1] = 0
ed = dict()

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        ed[tuple([min(c,v), max(c,v)])] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N-1):
    xs.append(ed[ees[i+1]])
print(max(xs))
print("\n".join(map(str,xs)))


Fixed submission:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**5+1)
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
ees = dict()
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
    ees[v+1] = tuple([min(a,b), max(a,b)])
cd = dict()
cd[1] = 0
ed = dict()

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        ed[tuple([min(c,v), max(c,v)])] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N-1):
    xs.append(ed[ees[i+1]])
print(max(xs))
print("\n".join(map(str,xs)))

Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
cd = dict()
cd[1] = 1

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N):
    xs.append(cd[i+1])
print(max(xs))
print("\n".join(map(str,xs)))


Fixed submission:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**5+1)
readline = sys.stdin.readline
N, = map(int, readline().split())
G = [[] for _ in range(N+1)]
ees = dict()
for v in range(N-1):
    a, b = map(int, readline().split())
    G[a].append(b)
    G[b].append(a)
    ees[v+1] = tuple([min(a,b), max(a,b)])
cd = dict()
cd[1] = 0
ed = dict()

def itree(v, p):
    cdd = 1
    for c in G[v]:
        if c == p:
            continue
        if cdd == cd[v]:
            cdd += 1
        cd[c] = cdd
        ed[tuple([min(c,v), max(c,v)])] = cdd
        cdd += 1
        itree(c, v)


def process():
    itree(1,0)
process()
xs = []
for i in range(N-1):
    xs.append(ed[ees[i+1]])
print(max(xs))
print("\n".join(map(str,xs)))
Buggy python submission (verdict: Runtime Error):
# ABC140E
class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

n,m=map(int, input().split())
xs = []
for _ in range(m):
    L, R, C = map(int, input().split())
    xs.append((L-1, R-1, C))
xs.sort()

mx = 10**15
ss = SegTree([0] + [mx] * n, n+1, mx, min)
for l, r, c in xs:
    mm = ss.query(l, n+1)
    nx = min(mm + c, ss.query(r, r+1))
    ss.update(r, nx)
r = ss.query(n-1,n)
print(r if r != mx else -1)


Fixed submission:
import sys
MM = 998244353
n, = map(int, input().split())
xs = list(map(int, input().split()))
mm = max(xs)
cc = [0] * n
if xs[0] != 0:
    print(0)
    sys.exit()
for x in xs:
    cc[x] += 1
for i in range(mm+1):
    if cc[i] == 0:
        print(0)
        sys.exit()
if cc[0] != 1:
    print(0)
    sys.exit()
r = 1
for i in range(mm):
    r = (r * pow(cc[i], cc[i+1])) % MM
print(r)