Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ans=0
b=sorted(a)
for i in range(n):
  if b[i]==b[i+1]:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
b=sorted(a)
for i in range(n-1):
  if b[i]==b[i+1]:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ans=0
a=sorted(a)
for i in range(n):
  if a[i]==a[i+1]:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
b=sorted(a)
for i in range(n-1):
  if b[i]==b[i+1]:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
  if a.count(a[i])>=2:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=0
b=sorted(a)
for i in range(n-1):
  if b[i]==b[i+1]:
    ans=1
    break
if ans==1:
  print('NO')
else:
  print('YES')
Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
p=list(map(int,input().split()))
e=[]
for i in range(n):
  ep=(1+p[i])*p[i]/(2*p[i])
  e.append(ep)
s=[e[0]]
for i in range(1,n):
  s.append(s[i-1]+e[i])
ans=s[k-1]
for i in range(k,n+1):
  ans=max(ans,s[i]-s[i-k])
print(ans)

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
e=[]
for i in range(n):
  ep=(1+p[i])*p[i]/(2*p[i])
  e.append(ep)
s=[e[0]]
for i in range(1,n):
  s.append(s[i-1]+e[i])
ans=s[k-1]
for i in range(k,n):
  ans=max(ans,s[i]-s[i-k])
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i+1]
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(0,N,2):
    a = a + A[i]
    if i+1<=N:
        b = b + A[i+1]
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(0,len(A),2):
    a = a + A[i]
    if i+1<=len(A):
        b = b + A[i+1]
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
while True:
    if len(A) >= 2:
        a = a + max(A)
        A.remove(max(A))
        b = b + max(A)
        A.remove(max(A))
    elif len(A) == 1:
        a = a + A[0]
    else:
        break
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split())
a = 0
b = 0
while True:
    if len(A) >= 2:
        a = a + max(A)
        A.remove(max(A))
        b = b + max(A)
        A.remove(max(A))
    elif len(A) == 1:
        a = a + A[0]
    else:
        break
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split())
a = 0
b = 0
while len(A)>= 1:
    a = a + max(A)
    A.remove(max(A))
    b = b + max(A)
    A.remove(max(A))
print(a-b)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
a = 0
b = 0
A.sort(reverse=True)
for i in range(N):
    if i%2 == 0:
        a = a + A[i]
    if i%2 == 1:
        b = b + A[i]
print(a-b)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a1=[int(input()) for i in range(n)]
a2=[int(input()) for i in range(n)]
a=[a1,a2]
s=a[0][0]
t=sum(a1)
u=sum(a2)
for i in range(1,n):
  if t-a[0][0]>=u:
    s=s+a[0][i]
    t=t-a[0][i]
    u=u-a[1][i-1]
  else:
    s=s+sum(a2[(i-1):n])
print(s)

Fixed submission:
n=int(input())
a=list(list(map(int,input().split())) for i in range(2))
s=0
for i in range(n):
  s=max(s,sum(a[0][0:i+1])+sum(a[1][i:n]))
print(s)
Buggy python submission (verdict: Runtime Error):
if (a * b) % 2 == 0:
    return 'Even'
else:
    return 'Odd'

Fixed submission:
a, b = map(int, input().split())
if a*b%2==0:
    print('Even')
else:
    print('Odd')

Buggy python submission (verdict: Runtime Error):
def A_product(a,b):
	if (a % 2 == 0) or (b % 2 == 0):
    	return Even
    else:
        return Odd

Fixed submission:
a, b = map(int, input().split())
if a*b%2==0:
    print('Even')
else:
    print('Odd')
Buggy python submission (verdict: Runtime Error):
N=int(input())
Ki=[list(map(int,input().split())) for i in range(N)]
f=False
for i in range(Ki):
  t,x,y=i
  s=x+y
  if (t%2==0 and s%2!=0) or (t%2!=0 and s%2==0) or t<s:
    f=True
    break
if f:
  print('No')
else:
  print('Yes')
  


Fixed submission:
N=int(input())
Ki=[list(map(int,input().split())) for i in range(N)]
f=False
for i in Ki:
  t,x,y=i
  s=x+y
  if (t%2==0 and s%2!=0) or (t%2!=0 and s%2==0) or t<s:
    f=True
    break
if f:
  print('No')
else:
  print('Yes')

Buggy python submission (verdict: Runtime Error):
N=int(input())
Ki=[list(map(int,input().split())) for i in range(N)]
f=False
for i in range(Ki):
  t,x,y=i
  s=x+y
  if (t%2==0 and s%2==1) or (t%2==1 and s%2==0) or t<s:
    f=True
    break
if f:
  print('No')
else:
  print('Yes')
  


Fixed submission:
N=int(input())
Ki=[list(map(int,input().split())) for i in range(N)]
f=False
for i in Ki:
  t,x,y=i
  s=x+y
  if (t%2==0 and s%2!=0) or (t%2!=0 and s%2==0) or t<s:
    f=True
    break
if f:
  print('No')
else:
  print('Yes')
Buggy python submission (verdict: Runtime Error):
N=int(input())
d=list(map(int,input().split()))
a=[]
for i in range(N):
  if a.count(d[i])==0:
    a.append(d[i])
print(len(a))


Fixed submission:
N=int(input())
d=[int(input()) for i in range(N)]
a=[]
for i in range(N):
  if a.count(d[i])==0:
    a.append(d[i])
print(len(a))
Buggy python submission (verdict: Runtime Error):
s = input()
c = 0
for i in range(3):
    if a[i] == 1:
        c = c + 1
return c

Fixed submission:
a = input()
c=0
for i in range(len(a)):
    if a[i] == '1':
        c = c + 1
print(c)

Buggy python submission (verdict: Runtime Error):
s = input()
count = 0
for i in range(3):
    if s == 1:
        count = count + 1
    return count

Fixed submission:
a = input()
c=0
for i in range(len(a)):
    if a[i] == '1':
        c = c + 1
print(c)

Buggy python submission (verdict: Runtime Error):
s = input()
count = 0
for i in range(3):
    if s == 1:
        count = count + 1
return count

Fixed submission:
a = input()
c=0
for i in range(len(a)):
    if a[i] == '1':
        c = c + 1
print(c)
Buggy python submission (verdict: Runtime Error):
s = map(int,input().split())
c = 0
for i in range(len(s)):
    while s[0] % 2 == 0:
        s[i] = s[i]/2
        c = c + 1
return c

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
i = 0
odd = 0
while odd == 0:
    for n in range(len(a)):
        if a[n]%2 == 0:
            a[n] /= 2
        else:
            odd = 1
            break
    if odd == 0:
        i += 1
print(i)

Buggy python submission (verdict: Runtime Error):
s = map(int,input().split())
c = 0
while a[len(s)] % 2 == 0:
    for i in range(len(s)):
        if a[i] % 2 == 0:
            a[i] = a[i] / 2
        c = c + 1
        else:
            break
return c

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
i = 0
odd = 0
while odd == 0:
    for n in range(len(a)):
        if a[n]%2 == 0:
            a[n] /= 2
        else:
            odd = 1
            break
    if odd == 0:
        i += 1
print(i)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
p=1
for i in range(1,n+1):
  p=p*i
print(p%(10**9+7))

Fixed submission:
n=int(input())
p=1
for i in range(1,n+1):
  p=p*i
  if p%10==0:
    p=p%(10**9+7)
print(p%(10**9+7))
Buggy python submission (verdict: Runtime Error):
s=input()
s=s.reverse()
voc=['dream','dreamer','erase','eraser']
for i in range(len(voc)):
  voc[i]=voc[i][::-1]
while True:
  flag=False
  for v in voc:
    if s[:len(v)]==v:
      s=s[len(v):]
      flag=True
  if s=='':
    print('Yes')
    break
  elif not flag:
    print('No')
    break

Fixed submission:
S=input()
 
S=S.replace('eraser','')
S=S.replace('erase','')
S=S.replace('dreamer','')
S=S.replace('dream','')
 
if S=='':
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
a,b,x=(map(int,input().split())
print((b//x)-((a-1)//x))

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

Buggy python submission (verdict: Time Limit Exceeded):
a,b,x=map(int,input().split())
c=0
for i in range(a,b+1):
  
  if i%x==0:
    c+=1
print(c)

Fixed submission:
a,b,x=map(int,input().split())
if a<=b<x:
  if a==0:
    print(1)
  else:
    print(0)
else:
  s=(a-1)//x
  t=b//x
  print(t-s)
Buggy python submission (verdict: Runtime Error):
n,k=(map(int,input().split())
print(k*((k-1)**(n-1)))

Fixed submission:
n,k=(map(int,input().split()))
print(k*((k-1)**(n-1)))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
import bisect #二部探索
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7
mod = 10**9 + 7
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def main():
    n = int(input())
    # 1～n-1 までcを回す
    c = 1
    ans = 0
    while(c <= n-1):
        ab = n - c
        if ab == 1:
            ans += 1
            break
        arr_ab = factorization(ab)
        ans_temp = 1
        for i in range(len(arr_ab)):
            ans_temp *= arr_ab[i][1] + 1
        #print(ans_temp, arr_ab)
        ans += ans_temp
        c += 1
    print(ans)
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
import bisect #二部探索
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7
mod = 10**9 + 7
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr
def get_sieve_of_eratosthenes(n):
    if not isinstance(n, int):
        raise TypeError('n is int-type.')
    if n < 2:
        raise ValueError('n is more than 2')
    data = [i for i in range(2, n + 1)]
    for d in data:
        data = [x for x in data if (x == d or x % d != 0)]
    return data

def main():
    n = int(input())
    # 1～n-1 までcを回す
    x = math.floor(math.sqrt(n-1))
    ans = 0
    a = 1
    while(a <= x):
        ans += (n-1) // a
        a += 1
    ans -= (x - 1) * x / 2
    ans *= 2
    ans -= x
    print(int(ans))
    return 0

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7

def main():
    n = int(input())
    N = list(map(int,input().split()))
    ans = 0
    for i in range(n - 1):
        #print(N[i] , sum(N[i + 1:]))
        ans += (N[i] * sum(N[i + 1:])) % INF
        ans %= INF
    print(ans)
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7

def main():
    n = int(input())
    N = list(map(int,input().split()))
    ans = 0
    """
    for i in range(n - 1):
        #print(N[i] , sum(N[i + 1:]))
        ans += (N[i] * sum(N[i + 1:])) % INF
        ans %= INF
    """
    # ans = sum(N)  /2
    N2 = [pow(i, 2, INF) for i in N]
    N3 = pow(sum(N), 2, INF)
    #print(N2, N3)
    N4 = (N3 - sum(N2))
    if N4 % 2 == 1:
        N4 += INF
    ans = (N4 / 2) % INF
    print(int(ans))
    return 0

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7

def main():
    n = int(input())
    N = list(map(int,input().split()))
    ans = 0
    for i in range(n - 1):
        #print(N[i] , sum(N[i + 1:]))
        ans_temp = (N[i] * sum(N[i + 1:])) % INF
        ans += ans_temp
        ans = ans % INF
    print(ans)

    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
INF = 10**9 + 7

def main():
    n = int(input())
    N = list(map(int,input().split()))
    ans = 0
    """
    for i in range(n - 1):
        #print(N[i] , sum(N[i + 1:]))
        ans += (N[i] * sum(N[i + 1:])) % INF
        ans %= INF
    """
    # ans = sum(N)  /2
    N2 = [pow(i, 2, INF) for i in N]
    N3 = pow(sum(N), 2, INF)
    #print(N2, N3)
    N4 = (N3 - sum(N2))
    if N4 % 2 == 1:
        N4 += INF
    ans = (N4 / 2) % INF
    print(int(ans))
    return 0

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    a = list(map(int,input().split()))
    ans = 0
    for i in range(0, n + 1, 2):
        if a[i] % 2 == 1:
            ans += 1
    print(ans)
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    a = list(map(int,input().split()))
    #ans = 0
    ans_list = [a[i] for i in range(n) if i % 2 == 0]
    ans = [ans_list[i] for i in range(len(ans_list)) if ans_list[i] % 2 == 1]
    #print(ans_list)
    #print(ans)
    print(len(ans))
    return 0

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    a = list(map(int,input().split()))
    ans = 0
    for i in range(0, n + 1, 2):
        if a[i] % 2 == 1:
            ans += 1
    print(ans)
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(input())
    a = list(map(int,input().split()))
    #ans = 0
    ans_list = [a[i] for i in range(n) if i % 2 == 0]
    ans = [ans_list[i] for i in range(len(ans_list)) if ans_list[i] % 2 == 1]
    #print(ans_list)
    #print(ans)
    print(len(ans))
    return 0

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import sys
import math
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
def choice_contest(c, data, last, held, day, contest):
    contest -= 1
    #print(last)
    # すでに開催したコンテスト(held)にあればlastに＋1
    last = [i + 1 for i in last]
    # 今回開催するコンテストのlastを0にする
    last[contest] = 0
    #print(last)
    # 開催したコンテストがheldに無ければ追加
    if not contest in held:
        held.append(contest)
    # 不満度の計算
    d_satis = [last[i] * c[i] for i in range(26)]
    # i日目にcontestを選んだ場合の
    return [int(data[day][contest]) - sum(d_satis), last, held]

def main():
    d = int(input())
    c = list(map(int,input().split()))
    data = [input().split() for _ in range(d)]
    last = [0] * 26
    held = [] #すでに行ったコンテスト
    ans = 0
    for i in range(d):
        s, last, held = choice_contest(c, data, last, held, i, contest = int(input()))
        ans += s
        print(ans)
    return 0

if __name__ == "__main__":
    main()import sys
import math
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
def choice_contest(c, data, last, held, day, contest):
    contest -= 1
    #print(last)
    # すでに開催したコンテスト(held)にあればlastに＋1
    last = [i + 1 for i in last]
    # 今回開催するコンテストのlastを0にする
    last[contest] = 0
    #print(last)
    # 開催したコンテストがheldに無ければ追加
    if not contest in held:
        held.append(contest)
    # 不満度の計算
    d_satis = [last[i] * c[i] for i in range(26)]
    # i日目にcontestを選んだ場合の
    return [int(data[day][contest]) - sum(d_satis), last, held]

def main():
    d = int(input())
    c = list(map(int,input().split()))
    data = [input().split() for _ in range(d)]
    last = [0] * 26
    held = [] #すでに行ったコンテスト
    ans = 0
    for i in range(d):
        s, last, held = choice_contest(c, data, last, held, i, contest = int(input()))
        ans += s
        print(ans)
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import math
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化
def choice_contest(c, data_day, last, day, contest):
    contest -= 1
    #print(last)
    last = [i + 1 for i in last]
    # 今回開催するコンテストのlastを0にする
    last[contest] = 0
    #print(len(last))
    #print(last)
    # 不満度の計算
    d_satis = sum([last[i] * c[i] for i in range(26)])
    # i日目にcontestを選んだ場合の
    return [int(data_day[contest]) - d_satis, last]

def main():
    d = int(input())
    c = list(map(int,input().split()))
    data = [input().split() for _ in range(d)]
    last = [0] * 26
    ans = 0
    for i in range(d):
        s, last = choice_contest(c, data[i], last, i, contest = int(input()))
        ans += s
        print(ans)
    return 0

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math  # gcdもあるよ
import collections
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    q = int(sys.stdin.readline())
    queue = [list(map(int,sys.stdin.readline().split())) for _ in range(q)]
    count = collections.Counter(a)
    #print(count)
    #print(list(count))
    for b, c in queue:
        if  b in count:
            if c in count:
                count[c] += count[b]
                del count[b]
            else:
                count[c] = count[b]
                del count[b]
        ans = 0
        for k in count:
            ans += k * count[k]
        print(ans) 
    
    
    return 0

if __name__ == "__main__":
    main()

Fixed submission:
import sys
import numpy as np
import math  # gcdもあるよ
import collections
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    q = int(sys.stdin.readline())
    queue = [list(map(int,sys.stdin.readline().split())) for _ in range(q)]
    count = collections.Counter(a)
    #print(count)
    #print(list(count))
    ans = 0
    for k in count:
        ans += k * count[k]
    for b, c in queue:
        if  b in count:
            if c in count:
                count[c] += count[b]
                ans += (c - b) * count[b]
                del count[b]
            else:
                count[c] = count[b]
                ans += (c - b) * count[b]

                del count[b]
        print(ans)
    
    
    return 0

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
s = 0
for i in range(n):
    for j in range(i+1,n):
        if abs(a[i]+a[j])==abs(i-j):
            s += 1
print(s)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
X = dict()
ans = 0
for i in range(n):
    R = i + a[i]
    if not R in X:
        X[R]=1
    else: X[R]+=1

for i in range(n):
    L = i - a[i]
    if L in X:
        ans+=X[L]
print(ans)