Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    h, w = map(int, input().split())
    
    ss = ['.' * w] + ['.' + input() + '.' for _ in range(h)] + ['.' * w]

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if ss[i][j] == '#':
                if ss[i + 1][j] == '.' and ss[i - 1][j] == '.' and ss[i][j + 1] == '.' and ss[i][j - 1] == '.':
                    print("No")
                    break
        else:
            continue
        break
    else:
        print("Yes")



if __name__ == '__main__':
    main()

Fixed submission:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    h, w = map(int, input().split())
    
    ss = ['.' * (w + 2)] + ['.' + input() + '.' for _ in range(h)] + ['.' * (w + 2)]

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if ss[i][j] == '#':
                if ss[i + 1][j] == '.' and ss[i - 1][j] == '.' and ss[i][j + 1] == '.' and ss[i][j - 1] == '.':
                    print("No")
                    break
        else:
            continue
        break
    else:
        print("Yes")



if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    h, w = map(int, input().split())
    
    ss = [['.'] * w] + ['.' + input() + '.' for _ in range(h)] + [['.'] * w]

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if ss[i][j] == '#':
                if ss[i + 1][j] == '.' and ss[i - 1][j] == '.' and ss[i][j + 1] == '.' and ss[i][j - 1] == '.':
                    print("No")
                    break
        else:
            continue
        break
    else:
        print("Yes")



if __name__ == '__main__':
    main()

Fixed submission:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    h, w = map(int, input().split())
    
    ss = ['.' * (w + 2)] + ['.' + input() + '.' for _ in range(h)] + ['.' * (w + 2)]

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if ss[i][j] == '#':
                if ss[i + 1][j] == '.' and ss[i - 1][j] == '.' and ss[i][j + 1] == '.' and ss[i][j - 1] == '.':
                    print("No")
                    break
        else:
            continue
        break
    else:
        print("Yes")



if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())

if a == b:
    print("H")
else:
    print("D")

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

if a == b:
    print("H")
else:
    print("D")
Buggy python submission (verdict: Runtime Error):
import collections
import functools
DIVISOR = 10 ** 9 + 7
n = int(input())

prime_factor = collections.defaultdict(int)

for i in range(1, n + 1):
    tmp = i
    for j in range(2, int(n ** 0.5) + 1):
        while tmp % j == 0:
            prime_factor[j] += 1
            tmp //= j

    if tmp > 1:
        prime_factor[tmp] += 1

# print(prime_factor)
prime_factor_num = [prime_factor[key] + 1 for key in prime_factor]

print((functools.reduce(lambda x, y: (x % DIVISOR) * y, prime_factor_num)) % DIVISOR)

Fixed submission:
import collections
import functools
DIVISOR = 10 ** 9 + 7
n = int(input())

if n == 1:
    print(1)
else:
    prime_factor = collections.defaultdict(int)
    for i in range(1, n + 1):
        tmp = i
        for j in range(2, int(n ** 0.5) + 1):
            while tmp % j == 0:
                prime_factor[j] += 1
                tmp //= j

        if tmp > 1:
            prime_factor[tmp] += 1

    # print(prime_factor)
    prime_factor_num = [prime_factor[key] + 1 for key in prime_factor]

    print((functools.reduce(lambda x, y: (x % DIVISOR) * y, prime_factor_num)) % DIVISOR)
Buggy python submission (verdict: Runtime Error):
def erase_check(i, s):
    if s[i:i + 5] == 'erase':
        i_next = erase_check(i + 5, s)
        if i_next == i + 5 and s[i_next] == 'r':
            return s_check(i_next + 1, s)
        else:
            return s_check(i_next, s)
    else:
        return i


def s_check(i, s):
    if s[i:i + 5] == 'dream':
        i_next = erase_check(i + 5, s)
        if i_next == i + 5 and s[i_next:i_next + 2] == 'er':
            return s_check(i_next + 2, s)
        else:
            return s_check(i_next, s)
    else:
        return erase_check(i, s)


s = input()

if s_check(0, s) == len(s):
    print("YES")
else:
    print("NO")

Fixed submission:
s = input()
i = len(s)
while i > 0:
    if s[i - 5:i] in ('dream', 'erase'):
        i -= 5
    elif s[i - 6:i] == 'eraser':
        i -= 6
    elif s[i - 7:i] == 'dreamer':
        i -= 7
    else:
        print("NO")
        break
else:
    print("YES")
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bisect import bisect_left,bisect_right


## input
N = int(input())
Ln = list(map(int, input().split()))
Ln = sorted(Ln)

ret = 0
for a in range(N-1,1,-1):
  for b in range(a-1,0,-1):
    if Ln[a] >= Ln[b] + Ln[b-1]:
      continue
    else:
      c = b-1
      while Ln[a]<Ln[b]+Ln[c] and c>=0:
        ret += 1
        c -= 1
print(ret)

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-


## Input
N = int(input())
Ln = list(map(int, input().split()))
Ln = sorted(Ln)

## Solve
ret = 0
for a in range(2,N):
  c = 0
  for b in range(a-1,0,-1):
    if c >= b:
      continue
    else:
      while (c<b and Ln[a]>=Ln[b]+Ln[c]):
        c += 1
      ret += max(b-c,0)
print(ret)
Buggy python submission (verdict: Runtime Error):
N = int(input())

ans = 0
for i in range(1,N+1):
	if i%2==1:
      ans += 1
print(ans/N)

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import math



N = int(input())
print(math.ceil(N/2)/N)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from collections import defaultdict


def solve():
  N = int(input())
  S = str(input())

  dct = defaultdict(lambda:[N+1,-1])
  for i in range(N):
    for j in range(i+1,N+1):
      dct[S[i:j]][0] = min(dct[S[i:j]][0],i)
      dct[S[i:j]][1] = max(dct[S[i:j]][1],i)

  ret = 0
  for sn in dct.keys():
    if dct[sn][1]-dct[sn][0]>=len(sn):
      ret = max(ret,len(sn))
  return ret

print(solve())

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
S = str(input())

r,i,j = 0,0,0
while j < N:
  t = S[i:j]
  if t in S[j:]:
    r = max(r,j-i)
    j += 1
  else:
    i += 1
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from collections import defaultdict


def solve():
  N = int(input())
  S = str(input())

  dct = defaultdict(lambda:set([]))
  for i in range(N):
    for j in range(i+1,N+1):
      dct[S[i:j]].add(i)

  for sn in sorted(dct.keys(),key=lambda x:-len(x)):
    if len(dct[sn]) < 2:
      continue
    else:
      if max(dct[sn])-min(dct[sn]) >= len(sn):
        return len(sn)
  return 0

print(solve())

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
S = str(input())

r,i,j = 0,0,0
while j < N:
  t = S[i:j]
  if t in S[j:]:
    r = max(r,j-i)
    j += 1
  else:
    i += 1
print(r)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bisect import bisect_left


def solve():
  N = int(input())
  An = [int(input()) for _ in range(N)]
  if N==1:
    return 1
  else:
    ret = [An[0]]
    for a in An[1:]:
      idx = bisect_left(ret,a)
      if idx == 0:
        ret = [a] + ret
      else:
        ret[idx-1] = a
    return len(ret)


print(solve())

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bisect import bisect_left
from collections import deque


def solve():
  N = int(input())
  An = [int(input()) for _ in range(N)]
  if N==1:
    return 1
  else:
    ret = deque([An[0]])
    for a in An[1:]:
      idx = bisect_left(ret,a)
      if idx == 0:
        ret.appendleft(a)
      else:
        ret[idx-1] = a
    return len(ret)


print(solve())
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import math

MOD = 1000000007



def fact(n):
  ans = {0}
  for i in range(1,math.ceil(n**0.5)+1):
      ans.add(i)
      ans.add(n//i)
  return sorted(list(ans))

def main():
  N,K = map(int, input().split())
  dp = [{} for _ in range(K+1)]
  keys = fact(N)
  dp[1] = {f:f for f in keys}

  for i in range(1,K):
    j_prev = 0
    dp[i+1][0] = 0
    for j in keys[1:]:
      dp[i+1][j] = (dp[i+1][j_prev] + (j-j_prev)*dp[i][N//j]) % MOD
      j_prev = j
  print(dp[K][N])


if __name__ == "__main__":
  main()

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import math

MOD = 1000000007



def fact(n):
  ans = {0}
  for i in range(1,math.ceil(n**0.5)+1):
      ans.add(i)
      ans.add(n//i)
  return sorted(list(ans))

def main():
  N,K = map(int, input().split())
  dp = [{} for _ in range(K+1)]
  keys = fact(N)
  dp[1] = {f:f for f in keys}

  for i in range(1,K):
    j_prev = 0
    dp[i+1][0] = 0
    for j in keys[1:]:
      dp[i+1][j] = (dp[i+1][j_prev] + (j-j_prev)*dp[i][N//j]) % MOD
      j_prev = j
  print(dp[K][N])


if __name__ == "__main__":
  main()
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-



S = list("Z"+str(input()))
idx = 1
l = len(S)

ans = 0
while(idx <= l-3):
  if S[idx]=="A" and S[idx+1]=="B" and S[idx+2] == "C":
    ans += 1
    S[idx],S[idx+1],S[idx+2] = ["B","C","A"]
    idx -= 1
  else:
    idx += 1

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-


S = str(input()).replace("BC","D")

ans,stk = 0,0
for c in S:
  if c == "A":
    stk += 1
  elif c == "D":
    ans += stk
  else:
    stk = 0
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-



S = list("Z"+str(input()))
idx = 1
l = len(S)

ans = 0
while(idx <= l-3):
  if S[idx]+S[idx+1]+S[idx+2] == "ABC":
    ans += 1
    S[idx],S[idx+1],S[idx+2] = ["B","C","A"]
    idx -= 1
  else:
    idx += 1

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-


S = str(input()).replace("BC","D")

ans,stk = 0,0
for c in S:
  if c == "A":
    stk += 1
  elif c == "D":
    ans += stk
  else:
    stk = 0
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-



S = list("Z"+str(input()))
idx = 1
l = len(S)

ans = 0
while(idx <= l-3):
  if S[idx]+S[idx+1]+S[idx+2] == "ABC":
    ans += 1
    S[idx],S[idx+1],S[idx+2] = ["B","C","A"]
    idx -= 1
  else:
    idx += 1

print(ans)

Fixed submission:
# -*- coding: utf-8 -*-


S = str(input()).replace("BC","D")

ans,stk = 0,0
for c in S:
  if c == "A":
    stk += 1
  elif c == "D":
    ans += stk
  else:
    stk = 0
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding: utf-8 -*-


MOD = 10**9+7
Facts = [1]*(2*10**5+1)
for i in range(2,2*10**5+1):
  Facts[i] = (Facts[i-1]*i) % MOD

def mul(a,b):
  return (a*b) % MOD

def power(a,b):
  if   b==0   : return 1
  elif b==1   : return a % MOD
  elif b%2==0 : return power(a,b//2)**2 % MOD
  else        : return power(a,b//2)**2 * a % MOD

def div(a,b):
  return mul(a,power(b,MOD-2))


def combi(n,m,k):
  if k <= 2 or k >= n*m:
    return 1
  else:
    return div(Facts[n*m-2],(Facts[k-2]*Facts[n*m-k]))


def main():
  N,M,K = map(int, input().split())
  ans = 0
  for d in range(1,N):
    ans = (ans + d * (N-d)*M**2 * combi(N,M,K)) % MOD
  for d in range(1,M):
    ans = (ans + d * (M-d)*N**2 * combi(N,M,K)) % MOD
  return ans

if __name__ == "__main__":
  print(main())

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-


MOD = 10**9+7
Facts = [1]*(2*10**5+1)
for i in range(2,2*10**5+1):
  Facts[i] = (Facts[i-1]*i) % MOD

def mul(a,b):
  return (a*b) % MOD

def power(a,b):
  if   b==0   : return 1
  elif b==1   : return a % MOD
  elif b%2==0 : return power(a,b//2)**2 % MOD
  else        : return power(a,b//2)**2 * a % MOD

def div(a,b):
  return mul(a,power(b,MOD-2))


def combi(n,m,k):
  if k <= 2 or k >= n*m:
    return 1
  else:
    return div(Facts[n*m-2],(Facts[k-2]*Facts[n*m-k]))


def main():
  N,M,K = map(int, input().split())
  com = combi(N,M,K)
  ans = 0
  for d in range(1,N):
    ans = (ans + d * (N-d)*M**2 * com) % MOD
  for d in range(1,M):
    ans = (ans + d * (M-d)*N**2 * com) % MOD
  return ans

if __name__ == "__main__":
  print(main())
Buggy python submission (verdict: Runtime Error):
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



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

ans = 0
for i in range(1,N+1):
  if i < K:
    cnt = math.ceil(math.log2(K/i))
  else:
    cnt = 0
  ans += (1/2)**cnt

print(ans/N)

Fixed submission:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



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

ans = 0
for i in range(1,N+1):
  if i < K:
    cnt = math.ceil(math.log2(K/i))
  else:
    cnt = 0
  ans += (1/2)**cnt

print(ans/N)
Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict


def solve():
  ## input
  N = int(input())
  route = defaultdict(set)
  costs = defaultdict(lambda:defaultdict(int))
  for _ in range(N-1):
    u,v,w = map(int, sys.stdin.readline().rstrip().split())
    route[u-1].add(v-1)
    route[v-1].add(u-1)
    costs[u-1][v-1] = w

  ## solve
  visited = set()
  p,q = [0,0]
  colors = [0]*N
  nxt = set([0])
  while len(visited)<N and len(nxt)>0:
    p = q
    q = nxt.pop()
    for r in route[q]:
      if p==r:
        continue
      else:
        colors[r] = (costs[q][r]+costs[r][q]+colors[q]) % 2
        nxt.add(r)
        visited.add(r)
  return colors


if __name__ == "__main__":
  ans = solve()
  for a in ans:
    print(a)

Fixed submission:
# /usr/bin/python
# -*- coding: utf-8 -*-
from collections import deque,defaultdict


N = int(input())

colors = [-1 for _ in range(N)]
tree = [{} for _ in range(N)]
for _ in range(N-1):
  u,v,w = map(int, input().split())
  tree[u-1][v-1] = w % 2
  tree[v-1][u-1] = w % 2

visiting = deque([0])
colors[0] = 0
while len(visiting) > 0:
  p = visiting.popleft()
  for q in tree[p]:
    if colors[q] < 0:
      colors[q] = (tree[p][q]+colors[p]) % 2
      visiting.append(q)

for c in colors:
  print(c)

Buggy python submission (verdict: Runtime Error):
# /usr/bin/python
# -*- coding: utf-8 -*-
import math
from collections import defaultdict


N = int(input())
cn = [-1 for i in range(N)]
cn[0] = 0
route = defaultdict(list)
wn = [[0 for j in range(N+1)] for i in range(N+1)]

for _ in range(N-1):
  u,v,w = map(int, input().split())
  wn[u][v] = w
  wn[v][u] = w
  route[u].append(v)
  route[v].append(u)

nxt = set([1])
vnum = 0
while vnum<N and len(nxt)>0:
  p = nxt.pop()
  for q in route[p]:
    if cn[q-1] > 0:
      continue
    else:
      vnum += 1
      nxt.add(q)
      cn[q-1] = (cn[p]+wn[p][q]) % 2

for c in cn:
  print(c)

Fixed submission:
# /usr/bin/python
# -*- coding: utf-8 -*-
from collections import deque,defaultdict


N = int(input())

colors = [-1 for _ in range(N)]
tree = [{} for _ in range(N)]
for _ in range(N-1):
  u,v,w = map(int, input().split())
  tree[u-1][v-1] = w % 2
  tree[v-1][u-1] = w % 2

visiting = deque([0])
colors[0] = 0
while len(visiting) > 0:
  p = visiting.popleft()
  for q in tree[p]:
    if colors[q] < 0:
      colors[q] = (tree[p][q]+colors[p]) % 2
      visiting.append(q)

for c in colors:
  print(c)
Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import math
import sys


# input
X,Y,Z,K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
B = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))

# solve
AB = [0 for i in range(X*Y)]
i = 0
for a in A:
  for b in B:
    AB[i] = a+b
    i += 1
AB = sorted(AB, key=lambda x:-x)[:K]

ABC = [0 for i in range(len(AB)*Z)]
i = 0
for ab in AB:
  for c in C:
    ABC[i] = ab+c
    i += 1
ABC = sorted(ABC, key=lambda x:-x)[:K]

for ans in ABC:
  print(ans)




Fixed submission:
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import math
import sys
import heapq


# input
X,Y,Z,K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
B = list(map(int, sys.stdin.readline().rstrip().split()))
C = list(map(int, sys.stdin.readline().rstrip().split()))
A = sorted(A, key=lambda x:-x)
B = sorted(B, key=lambda x:-x)
C = sorted(C, key=lambda x:-x)

# solve
heap_q = []
heapq.heappush(heap_q, [-1*(A[0]+B[0]+C[0]), 0, 0, 0])
smt,at,bt,ct = [0, 0, 0, 0]

for _ in range(K):
  sm,a,b,c = heapq.heappop(heap_q)
  print(-sm)

  if len(heap_q) > 0:
    smt,at,bt,ct = heapq.heappop(heap_q)
  while len(heap_q) > 0 and sm==smt and a==at and b==bt and c==ct:
    smt,at,bt,ct = heapq.heappop(heap_q)
  heapq.heappush(heap_q, [smt,at,bt,ct])

  if a+1 < X:
    heapq.heappush(heap_q, [-A[a+1]-B[b]-C[c], a+1, b, c])
  if b+1 < Y:
    heapq.heappush(heap_q, [-A[a]-B[b+1]-C[c], a, b+1, c])
  if c+1 < Z:
    heapq.heappush(heap_q, [-A[a]-B[b]-C[c+1], a, b, c+1])
Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import sys


num = 0
def chk(s):
  if ('AGC' in s) or ('ACG' in s) or ('GAC' in s):
    return 1
  elif ('AGAC' in s) or ('AGGC' in s) or ('AGTC' in s):
    return 1
  elif ('ACGC' in s) or ('AGGC' in s) or ('ATGC' in s):
    return 1
  else:
    return 0

def solve(n, s):
  if n == 0:
    if chk(s) > 0:
      return 1
    else:
      return 0
  else:
    ret = 0
    for c in ['A','C','G','T']:
      ret += solve(n-1,s+c)
    return ret

N = int(input())
num = solve(N,"")
print(4**N-num)


Fixed submission:
# /usr/bin/python
# -*- coding: utf-8 -*-
#
import sys
import itertools as itr

S = []

dp = [] #末尾3つのパターンで記憶

def init():
  dct = {}
  for i in ['A','C','G','T']:
    for j in ['A','C','G','T']:
      for k in ['A','C','G','T']:
        s = i+j+k
        if s not in ['AGC','ACG','GAC']:
          dct[s] = 1
  return dct


def chk(s):
  if ('AGC' in s) or ('ACG' in s) or ('GAC' in s):
    return False
  elif ('AGAC' in s) or ('AGCC' in s) or ('AGGC' in s) or ('AGTC' in s):
    return False
  elif ('AAGC' in s) or ('ACGC' in s) or ('AGGC' in s) or ('ATGC' in s):
    return False
  else:
    return True


n = int(input())-3

dp = init()
for i in range(n):
  dct = {}
  for s in dp:
    for c in ['A','C','G','T']:
      if chk(s+c):
        if s[1:]+c not in dct:
          dct[s[1:]+c] = dp[s] % (10**9+7)
        else:
          dct[s[1:]+c] += dp[s] % (10**9+7)
  dp = dct

print(sum(dp.values())%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
# /user/bin/python
# -*- coding: utf-8 -*-
import sys
 
s = str(input())
s_bef = ""
s_aft = s

while(s_bef != s_aft):
  s_bef = s_aft
  s_aft = s_aft.replace("01", "")
  s_aft = s_aft.replace("10", "")

print(len(s) - len(s_aft))


Fixed submission:
# /user/bin/python
# -*- coding: utf-8 -*-
import sys
 
s = str(input())
ans = 0

for c in s:
  if c == '0':
    ans -= 1
  else:
    ans += 1
print(len(s) - abs(ans))
Buggy python submission (verdict: Runtime Error):
# /user/bin/python
# -*- coding: utf-8 -*-
import sys
 

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

  def root(self, x):
    if self.par[x] < 0:
      return x
    else:
      return self.root(self.par[x])

  def issame(self, x, y):
    if self.root(x) == self.root(y):
      return True
    else:
      return False
  
  def merge(self, x, y):
    if x == y:
      return False
    if self.par[x] > self.par[y]:
      x, y = y, x
    self.par[x] += self.par[y]
    self.par[y] = x
    return True

  def size(self, x):
    return -self.par[self.root(x)]


if __name__ == "__main__":
  n,m = map(int, sys.stdin.readline().rstrip().split())
  ab = [[0 for _ in range(2)] for _ in range(m)]
  for i in range(m):
    ab[m-i-1] = list(map(int, sys.stdin.readline().rstrip().split()))

  uf = UnionFind(n)
  ans = []
  cur = n * (n-1) // 2

  for a,b in ab:
    ans.append(cur)

    if uf.issame(a-1, b-1):
      continue

    sa, sb = [uf.size(a-1), uf.size(b-1)]
    cur -= sa * sb
    uf.merge(a-1,b-1)

  for i in ans[::-1]:
    print(i)






Fixed submission:
# /user/bin/python
# -*- coding: utf-8 -*-
import sys
 

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

  def root(self, x):
    if self.par[x] < 0:
      return x
    else:
      return self.root(self.par[x])

  def issame(self, x, y):
    if self.root(x) == self.root(y):
      return True
    else:
      return False
  
  def merge(self, x, y):
    x, y = [self.root(x), self.root(y)]
    if x == y:
      return False
    if self.par[x] > self.par[y]:
      x, y = y, x
    self.par[x] += self.par[y]
    self.par[y] = x
    return True

  def size(self, x):
    return -self.par[self.root(x)]


if __name__ == "__main__":
  n,m = map(int, sys.stdin.readline().rstrip().split())
  A = [0] * m
  B = [0] * m
  for i in range(m):
    A[i], B[i] = list(map(int, sys.stdin.readline().rstrip().split()))

  uf = UnionFind(n)
  ans = []
  cur = n * (n-1) // 2

  for i in range(m):
    ans.append(cur)
    a, b = [A[m-i-1]-1, B[m-i-1]-1]

    if not uf.issame(a, b):
      sa = uf.size(a)
      sb = uf.size(b)
      cur -= sa * sb
      uf.merge(a, b)

  ans.reverse()
  for i in ans:
    print(i)
Buggy python submission (verdict: Runtime Error):
import math
import sys
import random
from operator import itemgetter
input = sys.stdin.readline

# a = int(input().rstrip())
# a, b = map(int, input().rstrip().split())
# D = list(map(int, input().rstrip().split()))
# H = [int(input().rstrip()) for _ in range(L)]
# DD = [[0 for j in range(100)] for i in range(200)]
# dict = {} ## dictionary

# a = sorted(a, key=lambda x: x[1]))
# if __name__ == '__main__':

input().rstrip()
As = list(map(int, input().rstrip().split()))

ans = As[0]
for i in As[1:]:
	ans = math.gcd(ans, i)
print(ans)

Fixed submission:
import math
import sys
from operator import itemgetter
input = sys.stdin.readline

# a = int(input().rstrip())
# a, b = map(int, input().rstrip().split())
# D = list(map(int, input().rstrip().split()))
# H = [int(input().rstrip()) for _ in range(L)]
# DD = [[0 for j in range(100)] for i in range(200)]
# dict = {} ## dictionary

# a = sorted(a, key=lambda x: x[1]))
# if __name__ == '__main__':

def gcd(a, b):
	if a > b:
		return gcd(b, a)
	elif a == 0:
		return b
	else:
		return gcd(a, b % a)

N = int(input().rstrip())
As = list(map(int, input().rstrip().split()))

ans = As[0]
for i in As[1:]:
	ans = gcd(ans, i)
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
import sys
import random
from operator import itemgetter
input = sys.stdin.readline

# a = int(input().rstrip())
# a, b = map(int, input().rstrip().split())
# D = list(map(int, input().rstrip().split()))
# H = [int(input().rstrip()) for _ in range(L)]
# DD = [[0 for j in range(100)] for i in range(200)]
# dict = {} ## dictionary

# a = sorted(a, key=lambda x: x[1]))
# if __name__ == '__main__':

input().rstrip()
As = list(map(int, input().rstrip().split()))

ans = As[0]
for i in As[1:]:
	ans = math.gcd(ans, i)
print(ans)

Fixed submission:
import math
import sys
from operator import itemgetter
input = sys.stdin.readline

# a = int(input().rstrip())
# a, b = map(int, input().rstrip().split())
# D = list(map(int, input().rstrip().split()))
# H = [int(input().rstrip()) for _ in range(L)]
# DD = [[0 for j in range(100)] for i in range(200)]
# dict = {} ## dictionary

# a = sorted(a, key=lambda x: x[1]))
# if __name__ == '__main__':

def gcd(a, b):
	if a > b:
		return gcd(b, a)
	elif a == 0:
		return b
	else:
		return gcd(a, b % a)

N = int(input().rstrip())
As = list(map(int, input().rstrip().split()))

ans = As[0]
for i in As[1:]:
	ans = gcd(ans, i)
print(ans)