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

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

n, m = map(int, input().split())
g = [[] for _  in range(n)]
for _ in range(m):
  x, y = map(int, input().split())
  x, y = x -1, y-1
  g[x].append(y)  

# dp[v]: ノードvを起点にしたときの、Gの有効パスの長さの最大値
dp = [-1]*n
def rec(v):
  if dp[v] != -1:
    return dp[v]
  else:
    res = 0
    for u in g[v]:
      res = max(res, rec(u)+1)
    dp[v] = res
    return dp[v]
  
for i in range(n):
  rec(i)
print(max(dp))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
g = [[] for _  in range(n)]
for _ in range(m):
  x, y = map(int, input().split())
  x, y = x -1, y-1
  g[x].append(y)  

# dp[v]: ノードvを起点にしたときの、Gの有効パスの長さの最大値
dp = [-1]*n
def rec(v):
  if dp[v] != -1:
    return dp[v]
  else:
    res = 0
    for u in g[v]:
      res = max(res, rec(u)+1)
    dp[v] = res
    return dp[v]
  
for i in range(n):
  rec(i)
print(max(dp))
Problem p03230 (python)

Fixed submission:
n = int(input())

k = 0
for i in range(1, 2*n+1):
    if i*(i-1) == 2*n:
        k = i

#print(k)
if not k:
    print('No')
    exit()

S = [[] for _ in range(k)]
import itertools
C = list(itertools.combinations(range(k),2))
for i in range(n):
    j = i+1
    c1, c2 = C[i]
    S[c1].append(j)
    S[c2].append(j)

print('Yes')
print(k)
for i in range(k):
    print(len(S[i]), *S[i])

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

if k == 3:
    print('Yes')
    print(k)
    print(2, 1, 2)
    print(2, 3, 1)
    print(2, 2, 3)

else:
    print('No')
Problem p03238 (python)

Fixed submission:
N = int(input())
if N == 2:
  A = int(input())
  B = int(input())
  print(A + B)
elif N == 1:
  print('Hello World')

Buggy submission (verdict: Runtime Error):
N = input()
if N == 2:
	A = int(input())
  	B = int(input())
    print(A+B)
else:
  	print('Hello World')

Buggy submission (verdict: Runtime Error):
N = int(input())
if N == 2:
	A = int(input())
  	B = int(input())
    print(A+B)
else:
  	print('Hello World')
Problem p03239 (python)

Fixed submission:
N, T = map(int, input().split())
costs = []
for i in range(N):
    c, t = map(int, input().split())
    if t <= T:
        costs.append(c)
if len(costs) == 0:
    print('TLE')
else:
    print(min(costs))

Buggy submission (verdict: Runtime Error):
N, T = map(int, input().split())
dic = {}
for i in range(N):
    c, t = map(int, input().split())
    dic[t].append(c)
if max(dic) <= T:
    print(dic[max(dic)])
else:
    print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = map(int, input().split())
dic = {}
for i in range(N):
    c, t = [int(i) for i in input().split()]
    dic[t].append(c)
if max(dic) <= T:
    print(dic[max(dic)])
else:
    print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = input().split()
dic = {}
for i in range(N):
    c, t = [int(i) for i in input().split()]
    dic[t].append(c)
if max(dic) <= T:
    print(dic[max(dic)])
else:
    print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = input().split()
dic = {}
for i in range(int(input()):
    c, t = [int(i) for i in input().split()]
    dic[t].append(c)
if max(dic) <= T:
    print(dic[max(dic)])
else:
    print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = input().split()
dic = {}
for i in range(int(input()):
    c, t = [int(i) in input().split()]
    dic[t].append(c)
if max(dic) <= T:
    print(dic[max(dic)])
else:
    print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = input().split()
dic = {}
for i in range(int(input()):
	c, t = [int(i) in input().split()]
    dic[t].append(c)
if max(dic) <= T:
	print(dic[max(dic)])
else:
	print('TLE')

Buggy submission (verdict: Runtime Error):
N, T = input().split()
dic = {}
for i in range(int(input()):
	c, t = [int(i) in input().split()]
    dic[t].append(c)
if max(dic) <= T:
	print(dic[max(dic)])
else:
	print('TLE')
Problem p03240 (python)

Fixed submission:
n = int(input())
l = [list(map(int, input().split())) for _ in range(n)]

l.sort(key=lambda x: x[2], reverse=True)
#print(l)

for cx in range(0, 101):
  for cy in range(0, 101):
    x = l[0][0]
    y = l[0][1]
    h = l[0][2]
    H = h+abs(x-cx)+abs(y-cy)
    #print(cx, cy, H)
    for i in range(1, n):
      x = l[i][0]
      y = l[i][1]
      h = l[i][2]
      if h != max(H-abs(x-cx)-abs(y-cy), 0):
        break
    else:
      print(cx, cy, H)
      exit()

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

L = [[0, 0, 0] for _ in range(n)]
for i in range(n):
    x, y, h = map(int, input().split())
    L[i][0] = x
    L[i][1] = y
    L[i][2] = h

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

for i in range(0, 101):
    for j in range(0, 101):
        for k in range(n):
            x = L[k][0]
            y = L[k][1]
            h = L[k][2]
            if k == 0:
                H = abs(i-x)+abs(j-y)+h
            else:
                if H != abs(i-x)+abs(j-y)+h:
                    break
        else:
            ans = [i, j, H]
print(*ans)
Problem p03263 (python)

Fixed submission:
h, w = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]

if h == 1 and w == 1:
    print(0)
    exit()

cnt = 0
ans = []
for i in range(h):
    for j in range(w):
        a = A[i][j]
        if a%2 == 1:
            for dy, dx in (-1, 0), (0, -1), (0, 1), (1, 0):
                ni = i+dy
                nj = j+dx
                if 0 <= ni < h and 0 <= nj < w:
                    ni_ = ni
                    nj_ = nj
                    if A[ni][nj]%2 == 1:
                        A[ni][nj] += 1
                        A[i][j] -= 1
                        cnt += 1
                        ans.append([i+1, j+1, ni+1, nj+1])
                        break
            else:
                A[ni_][nj_] += 1
                A[i][j] -= 1
                cnt += 1
                ans.append([i+1, j+1, ni_+1, nj_+1])

print(cnt)
for i in range(cnt):
    print(*ans[i])

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]

cnt = 0
ans = []
for i in range(h):
    for j in range(w):
        a = A[i][j]
        if a%2 == 1:
            for dy, dx in (-1, 0), (0, -1), (0, 1), (1, 0):
                ni = i+dy
                nj = j+dx
                if 0 <= ni < h and 0 <= nj < w:
                    ni_ = ni
                    nj_ = nj
                    if A[ni][nj]%2 == 1:
                        A[ni][nj] += 1
                        A[i][j] -= 1
                        cnt += 1
                        ans.append([i+1, j+1, ni+1, nj+1])
                        break
            else:
                A[ni_][nj_] += 1
                A[i][j] -= 1
                cnt += 1
                ans.append([i+1, j+1, ni_+1, nj_+1])

print(cnt)
for i in range(cnt):
    print(*ans[i])
Problem p03274 (python)

Fixed submission:
n, k = map(int, input().split())
xs= list(map(int, input().split()))

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
#print(ts)
print(min(ts))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
xs= list(map(int, input().split()))

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()

if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  print(i)
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
print(ts)
print(min(ts))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
xs= list(map(int, input().split()))

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)

if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
#print(ts)
print(min(ts))
Problem p03337 (python)

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

Buggy submission (verdict: Runtime Error):
A, B = map(int, input().split())
print(max([A+B, A-B, A*B])
Problem p03359 (python)

Fixed submission:
a, b = map(int, input().split())
if b < a:
  print(a-1)
else:
  print(a)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())
if b < a:
  print(a-1)
else:
  pringt(a)
Problem p03387 (python)

Fixed submission:
l = list(map(int, input().split()))
l.sort()

i = 0
while l[1] != l[2]:
  l[0], l[1] = l[0]+1, l[1]+1
  i += 1

if l[0] == l[1]:
  print(i)
  exit()
elif l[0]%2 == l[1]%2:
  i += (l[1] - l[0])//2
  print(i)
  exit()
else:
  i += (l[1]+1-l[0])//2+1
  print(i)
  exit()

Buggy submission (verdict: Runtime Error):
a, b = map(str, input().split())
c = int(a+b)
ans = 0
for i in range(c+1):
    if i ** 2 == c:
        ans +=1
        break
if ans == 1:
    print('Yes')
else:
    print('No')
Problem p03410 (python)

Fixed submission:
import numpy as np

n = int(input())
A = np.array(input().split(), np.int64)
B = np.array(input().split(), np.int64)

A = np.sort(A)
B = np.sort(B)

C = [0]*30
for k in range(30):
    A_ = A%(2**(k+1))
    B_ = B%(2**(k+1))
    A_ = np.sort(A_)
    B_ = np.sort(B_)
    A0 = A_[A_<=2**k]
    A1 = A_[A_>2**k]
    C[k] += (np.searchsorted(B_, 2**(k+1)-A0, side='left')-np.searchsorted(B_, 2**k-A0, side='left')).sum()
    C[k] += (np.searchsorted(B_, 2**(k+1)-A1, side='left')-np.searchsorted(B_, 0, side='left')).sum()
    C[k] += (np.searchsorted(B_, 2**(k+1), side='left')-np.searchsorted(B_, 2**(k+1)+2**k-A1, side='left')).sum()

ans = 0
for i in range(len(C)):
    if C[i]%2 == 1:
        ans += 2**i
#print(C)
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n = int(input())
A = np.array(input().split(), np.int64)
B = np.array(input().split(), np.int64)

A = np.sort(A)
B = np.sort(B)

C = [0]*30
for k in range(30):
    A_ = A%(2**(k+1))
    B_ = B%(2**(k+1))
    A_ = np.sort(A_)
    B_ = np.sort(B_)
    A0 = A_[A_<=2**k]
    A1 = A_[A_>2**k]
    C[k] += (np.searchsorted(B_, 2**(k+1)-A0, side='left')-np.searchsorted(B_, 2**k-A0, side='left')).sum()
    C[k] += (np.searchsorted(B_, 2**(k+1)-A1, side='left')-np.searchsorted(B_, 0, side='left')).sum()
    C[k] += (np.searchsorted(B_, 2**(k+1), side='left')-np.searchsorted(B_, 2**(k+1)+2**k-A1, side='left')).sum()

ans = 0
for i in range(len(C)):
    if C[i]%2 == 1:
        ans += 2**i
#print(C)
print(ans)
Problem p03470 (python)

Fixed submission:
N = int(input())
d = []
for i in range(N):
    d.append(int(input()))
print(len(set(d)))

Buggy submission (verdict: Runtime Error):
N = int(input())
d = []
for i in N:
    d.append(int(input()))
print(len(set(d)))
Problem p03480 (python)

Fixed submission:
s = str(input())
n = len(s)

if n == 1:
    print(1)
    exit()

maxs = []
for i in range(n-1):
    if s[i] != s[i+1]:
        maxs.append(max(i+1, n-i-1))

if maxs != []:
    print(min(maxs))
else:
    print(n)

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

if n == 1:
    print(1)
    exit()

maxs = []
for i in range(n-1):
    if s[i] != s[i+1]:
        maxs.append(max(i+1, n-i-1))
print(min(maxs))
Problem p03547 (python)

Fixed submission:
x, y = map(str, input().split())
d = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
 
if d[x] < d[y]:
    print('<')
elif d[x] > d[y]:
    print('>')
else:
    print('=')

Buggy submission (verdict: Runtime Error):
x, y = map(str, input().split())
d = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}

if d[x] < d[y]:
  print('<')
elif d[x] > d[y]:
  print('>')
else:
  print(`=')
Problem p03550 (python)

Fixed submission:
n, z, w = map(int, input().split())
l = list(map(int, input().split()))

if n == 1:
  print(abs(l[0]-w))
else:
  print(max(abs(l[-1]-w), abs(l[-2]-l[-1])))

Buggy submission (verdict: Runtime Error):
n, z, w = map(int, input().split())
l = list(map(int, input().split()))

def trial(n, z, w, t): 
  # n: 残りの枚数
  # z: Xの手持ち
  # w: Yの手持ち
  # t: これまでに引いた枚数
  min_score = [0]*n
  min_j = [0]*n
  for i in range(t,t+n):
    if i == t+n-1:
      min_score[n-1] = abs(l[t+n-1]-w)
      break
    temp = l[i]
    temp_min = 10**9+1
    for j in range(i+1, t+n):
      if abs(temp-l[j]) <= temp_min:
        temp_min = abs(temp-l[j])
        min_score[i-t] = temp_min
        min_j[i-t] = j
    #print(max(min_score))
  #print(min_score)
  #n -= (min_score.index(max(min_score))+1)
  if min_score.index(max(min_score)) == n-1:
    n = 0
    z = l[-1]
    w = w
    t = len(l)
  else:
    n = len(l) - (t+min_j[min_score.index(max(min_score))]+1)
    z = l[t+min_score.index(max(min_score))]
    w = l[t+min_j[min_score.index(max(min_score))]]
    t += (min_score.index(max(min_score))+1)
  return n, z, w, t
  
t = 0
while n > 0:
  n, z, w, t = trial(n, z, w, t)
  #print(n, z, w, t)
print(abs(z-w))

Buggy submission (verdict: Runtime Error):
n, z, w = map(int, input().split())
l = list(map(int, input().split()))

def trial(n, z, w, t): 
  # n: 残りの枚数
  # z: Xの手持ち
  # w: Yの手持ち
  # t: これまでに引いた枚数
  min_score = [0]*n
  min_j = [0]*n
  for i in range(t,t+n):
    if i == t+n-1:
      min_score[n-1] = abs(l[t+n-1]-w)
      break
    temp = l[i]
    temp_min = 10**9+1
    for j in range(i+1, t+n):
      if abs(temp-l[j]) <= temp_min:
        temp_min = abs(temp-l[j])
        min_score[i-t] = temp_min
        min_j[i-t] = j
    #print(max(min_score))
  #print(min_score)
  #n -= (min_score.index(max(min_score))+1)
  if min_score.index(max(min_score)) == n-1:
    n = 0
    z = l[t+min_score.index(max(min_score))]
    w = w
  else:
    n = len(l) - (t+min_j[min_score.index(max(min_score))]+1)
    z = l[t+min_score.index(max(min_score))]
    w = l[t+min_j[min_score.index(max(min_score))]]
    t += (min_score.index(max(min_score))+1)
  return n, z, w, t
  
t = 0
while n > 0:
  n, z, w, t = trial(n, z, w, t)
  #print(n, z, w, t)
print(abs(z-w))
Problem p03611 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

d = {}

for i in range(max(a)+1):
  d[i] = 0

for a_ in a:
  d[a_] += 1

d[-1] = 0
d[max(a)+1] = 0

ans = 0

for x in range(max(a)+1):
  if x-1 in d:
    ans = max(ans, d[x-1] + d[x] + d[x+1])

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
A.sort()
#print(A)

d = {}
for i in range(n):
    a = A[i]
    for k in (-1, 0, 1):
        if a+k not in d:
            d[a+k] = 1
        else:
            d[a] += 1
max_ = 0
for v in d.values():
    if v >= max_:
        max_ = v
print(max_)
Problem p03633 (python)

Fixed submission:
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 = int(input())
ts = [0]*n
for i in range(n):
  ts[i] = int(input())

print(lcm_list(ts))

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

def lcm_base(x, y):
  return (x*y)//math.gcd(x, y)

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

n = int(input())
ts = [0]*n
for i in range(n):
  ts[i] = int(input())

print(lcm_list(ts))
Problem p03635 (python)

Fixed submission:
n, m = map(int, input().split())
print((n-1)*(m-1))

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))

ans = []
for i in range(n):
  ans += [i+1]*l[i]

for i in range(h):
  temp = ans[i*w:(i+1)*w]
  if i%2 == 0:
    print(*temp)
  else:
    temp = reversed(temp)
    print(*temp)
Problem p03638 (python)

Fixed submission:
h, w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))

ans = []
for i in range(n):
  ans += [i+1]*l[i]

for i in range(h):
  temp = ans[i*w:(i+1)*w]
  if i%2 == 0:
    print(*temp)
  else:
    temp = reversed(temp)
    print(*temp)

Buggy submission (verdict: Runtime Error):
w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))
 
ans = []
for i in range(n):
  ans += [i+1]*l[i]
 
for i in range(h):
  temp = ans[i*w:(i+1)*w]
  if i%2 == 0:
    print(*temp)
  else:
    temp = reversed(temp)
    print(*temp)
Problem p03646 (python)

Fixed submission:
k = int(input())
n = 50

q, r = divmod(k, n)
A = [n-1]*n
for i in range(n):
    A[i] += q

for i in range(r):
    A[i] += n+1
    for j in range(n):
        A[j] -= 1

print(n)
print(*A)

Buggy submission (verdict: Runtime Error):
k = int(input())
n = k+1

A = [0]*n
A[0] = (n-1)+n*k
print(n)
print(*A)
Problem p03658 (python)

Fixed submission:
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
ans = 0
for i in range(k):
  ans +=l[i]
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
print(l[0]+l[1]+l[2])
Problem p03671 (python)

Fixed submission:
a, b, c = map(int, input().split())
print(min([a+b, b+c, c+a]))

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
print(min([a+b, b+c, c+a])
Problem p03696 (python)

Fixed submission:
n = int(input())
s = str(input())
A = [0]
cum = 0
ans = ''
for i in range(n):
    if s[i] == '(':
        cum += 1
    else:
        cum -= 1
    if cum == -1:
        ans = '('+ans
        ans += s[i]
        cum = 0
    else:
        ans += s[i]
else:
    if cum > 0:
        ans += ')'*cum
print(ans)

Buggy submission (verdict: Runtime Error):
def power(a, n, mod):
  bi=str(format(n,"b")) #2進数
  res=1
  for i in range(len(bi)):
    res=(res*res) %mod
    if bi[i]=="1":
      res=(res*a) %mod
  return res

n, m, p = map(int, input().split())
print(power(n, p, m))
Problem p03759 (python)

Fixed submission:
a, b, c = map(int, input().split())
if (b-a) == (c-b):
  print('YES')
else:
  print('NO')

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input())
if (b-a) == (c-b):
  print('YES')
else:
  print('NO')
Problem p03760 (python)

Fixed submission:
o = str(input())
e = str(input())

s = ''

for i in range(len(e)):
  s += o[i]
  s += e[i]

if len(o) == len(e) +1:
  s += o[-1]

print(s)

Buggy submission (verdict: Runtime Error):
o = str(input())
e = str(input())

s = ''

for i in range(len(o)):
  s += o[i]
  s += e[i]

if len(o) == len(e) +1:
  s += o[-1]

print(s)
Problem p03840 (python)

Fixed submission:
I, O, T, J, L, S, Z = map(int, input().split())
k0 = 0
k0 += (I//2)*2
k0 += (J//2)*2
k0 += (L//2)*2

k1 = 0
if I >= 1 and J >= 1 and L >= 1:
    k1 = 0
    k1 += 3
    I -= 1
    J -= 1
    L -= 1
    k1 += (I//2)*2
    k1 += (J//2)*2
    k1 += (L//2)*2

k = max(k0, k1)
k += O
print(k)

Buggy submission (verdict: Runtime Error):
I, O, T, J, L, S, Z = map(int, input().split())
k0 = 0
k0 += (I//2)*2
k0 += (J//2)*2
k0 += (L//2)*2

k1 = 0
if I >= 1 and J >= 1 and L >= 1:
    k1 = 0
    k += 3
    I -= 1
    J -= 1
    L -= 1
    k1 += (I//2)*2
    k1 += (J//2)*2
    k1 += (L//2)*2

k = max(k0, k1)
k += O
print(k)
Problem p03844 (python)

Fixed submission:
a, op, b = input().split()
a = int(a)
b = int(b)
op = str(op)

if op == '+':
  print(a+b)
else:
  print(a-b)

Buggy submission (verdict: Runtime Error):
a, op, b = map(int, input().split())
if op == '+':
  print(a+b)
else:
  print(a-b)
Problem p03855 (python)

Fixed submission:
def Find(x, par):
  if par[x] < 0:
    return x
  else:
    # 経路圧縮
    par[x] = Find(par[x], par)
    return par[x]

def Unite(x, y, par, rank):
  x = Find(x, par)
  y = Find(y, par)
  
  if x != y:
    # rankの低い方を高い方につなげる
    if rank[x] < rank[y]:
      par[y] += par[x]
      par[x] = y
    else:
      par[x] += par[y]
      par[y] = x
      if rank[x] == rank[y]:
        rank[x] += 1
    
def Same(x, y, par):
  return Find(x, par) == Find(y, par)
 
def Size(x, par):
  return -par[Find(x)]

n, k, l = map(int, input().split())
rpar = [-1]* n
tpar = [-1]* n
rrank = [0]*n
trank = [0]*n

for i in range(k):
  p, q = map(int, input().split())
  p, q =p-1, q-1
  Unite(p,q, rpar, rrank) 

for i_ in range(l):
  p, q = map(int, input().split())
  p, q =p-1, q-1
  Unite(p,q, tpar, trank)

d = {}
IDs = [0]*n
for j in range(n):
  ID = Find(j, rpar)+(Find(j, tpar)+1)*10**8
  IDs[j] = ID
  if ID in d:
    d[ID] += 1
  else:
    d[ID] = 1

ans = ''
for j_ in range(n):
  if j_ != 0:
    ans += ' '
  ans += str(d[IDs[j_]])
print(ans)

Buggy submission (verdict: Runtime Error):
mport sys
input = sys.stdin.readline
 
def Find(x, par):
  if par[x] < 0:
    return x
  else:
    # 経路圧縮
    par[x] = Find(par[x], par)
    return par[x]

def Unite(x, y, par, rank):
  x = Find(x, par)
  y = Find(y, par)
  
  if x != y:
    # rankの低い方を高い方につなげる
    if rank[x] < rank[y]:
      par[y] += par[x]
      par[x] = y
    else:
      par[x] += par[y]
      par[y] = x
      if rank[x] == rank[y]:
        rank[x] += 1
    
def Same(x, y, par):
  return Find(x, par) == Find(y, par)
 
def Size(x, par):
  return -par[Find(x)]
 
n, k, l = map(int, input().split())
rpar = [-1]* n
tpar = [-1]* n
rrank = [0]*n
trank = [0]*n
 
for i in range(k):
  p, q = map(int, input().split())
  p, q =p-1, q-1
  Unite(p,q, rpar, rrank) 

for i_ in range(l):
  p, q = map(int, input().split())
  p, q =p-1, q-1
  Unite(p,q, tpar, trank)

d = {}
IDs = [0]*n
for j in range(n):
  ID = Find(j, rpar)+(Find(j, tpar)+1)*10**8
  IDs[j] = ID
  if ID in d:
    d[ID] += 1
  else:
    d[ID] = 1

ans = ''
for j_ in range(n):
  if j_ != 0:
    ans += ' '
  ans += str(d[IDs[j_]])
print(ans)
Problem p04006 (python)

Fixed submission:
import sys

n, x = map(int,sys.stdin.readline().split())
A = list(map(int,sys.stdin.readline().split()))

ans = 10**18
mins = [a for a in A]
for k in range(n):
    for i in range(n):
        if k <= i:
            mins[i] = min(A[i-k], mins[i])
        else:
            mins[i] = min(A[n-(k-i)], mins[i])
    ans = min(sum(mins)+k*x, ans)
print(ans)

Buggy submission (verdict: Runtime Error):
def segfunc(x, y):
    return min(x, y)

class segment_():
    def __init__(self, init_val, n, segfunc):
        self.ide_ele = 10**9+1
        # num: n以上の最小の2のべき乗
        self.num = 2**(n-1).bit_length()
        self.seg = [self.ide_ele]*2*self.num
        self.segfunc = segfunc
        # set_val
        for i in range(n):
            self.seg[i+self.num-1] = init_val[i]
        # built
        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:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[2*k+1], self.seg[2*k+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

def main():
    n, x = list(map(int,sys.stdin.readline().split()))
    A = list(map(int,sys.stdin.readline().split()))

    seg_ = segment_(A, n, lambda a, b: min(a, b))
    ans = 10**18
    for k in range(n):
        t = k*x
        for i in range(n):
            if k <= i:
                t += seg_.query(i-k, i+1)
            else:
                t += min(seg_.query(0, i+1), seg_.query(n-(k-i), n))
        ans = min(t, ans)
    print(ans)

if __name__ == '__main__':
    main()