Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import collections
n = int(input())
vlist = list(map(int,input().split()))
even = vlist[0:n:2]
odd = vlist[1:n:2]
e_counter= collections.Counter(even)
o_counter = collections.Counter(odd)
e_v = list(e_counter.values())
e_k = list(e_counter.keys())
o_v = list(o_counter.values())
o_k = list(o_counter.keys())
ans = 10**9
#ans = len(even)-e + len(odd)-o
#print(e_k,o_k)

for i in range(len(e_v)):
  for j in range(len(o_v)):
    if e_k[i] != o_k[j]:
      ans = min(ans,len(even)-e_v[i]+len(odd)-o_v[j])
    else:
      ans = min(ans,len(even)-e_v[i]+len(odd),len(even)-o_v[i]+len(odd))
print(ans)


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

v_even = v[::2]
v_odd = v[1::2]
#print(v_even,v_odd)

from collections import Counter

c_even = Counter(v_even)
c_odd = Counter(v_odd)
elist = c_even.most_common()
olist = c_odd.most_common()

if elist[0][0] != olist[0][0]:
    print(len(v) - elist[0][1] - olist[0][1])
elif len(elist) >= 2 and len(olist) >= 2:
    print(len(v) - max(elist[0][1] + olist[1][1],elist[1][1] + olist[0][1]))
elif len(elist) >= 2:
    print(len(v) - max(elist[1][1] + olist[0][1], elist[0][1]))
elif len(olist) >= 2:
    print(len(v) - max(olist[1][1] + elist[0][1], olist[0][1]))
else:
    print(n//2)
Buggy python submission (verdict: Time Limit Exceeded):
def func(a):
  global mx
  mx = min(mx, abs(int(a)-x))
  return int(a)

n,x = map(int,input().split())
mx = 10**9
xlist = list(map(func,input().split()))

for i in range(mx,0,-1):
  for j in range(n):
    if abs(x-xlist[j])%i != 0:
      break
  else:
    print(i)
    break
  

Fixed submission:
from fractions import gcd
def func(a):
  return abs(int(a)-x)
n,x = map(int,input().split())
xlist = list(map(func,input().split()))
for i in range(1,n):
  xlist[i] = gcd(xlist[i],xlist[i-1])
print(xlist[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
def func(a):
    global alist
    cnt = 1
    for i in range(len(alist)):
        if abs(alist[i][0]) > abs(int(a)):
            alist[i][1] += 1
        else:cnt += 1
    alist.append([int(a),cnt])

    return int(a)



alist = []
n,k = map(int,input().split())
x = list(map(func,input().split()))#sort済み
anslist = []
for i in range(len(alist)):
    if alist[i][1] <= k:
        anslist.append(alist[i][0])

anslist.sort()
#print(alist)
#print(anslist)
if anslist[0] >= 0 and anslist[-1] >= 0:
    print(anslist[-1])

elif anslist[0] <= 0 and anslist[-1] <= 0:
    print(-anslist[0])

else:
    if anslist[-1] > -anslist[0]:
        print(-2*anslist[0] + anslist[-1])
    else:
        print(2*anslist[-1] - anslist[0])






Fixed submission:
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
for i in range(n-k+1):
    left = i
    right = i+k-1
    ans = min(ans, min(abs(x[left])+abs(x[right]-x[left]),abs(x[right])+abs(x[left]-x[right])))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(200000)
n = int(input())
import math
ans = float('inf')
def dfs(i,j):
    global ans
    if j == 0:
        ans = min(ans, i)
    
    else:
        dfs(i+1, j-9**int(math.log(j,9)))
        dfs(i+1, j-6**int(math.log(j,6)))
dfs(0,n)
print(ans)

Fixed submission:
n = int(input())
#dp[j] = j￥を引き出すために必要な操作の最小回数
dp = [float('inf')]*(10**5+1)
dp[0] = 0
import math

for i in range(1,10**5+1):
    for j in range(int(math.log(i,9)+0.00001)+1):
        dp[i] = min(dp[i], dp[i-9**j]+1)

    for k in range(int(math.log(i,6))+1):
        dp[i] = min(dp[i],dp[i-6**k]+1)
 
print(dp[n])
#print(dp[n-3:n+1])
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
k = int(input())
n = len(s)

ansset= set()
from itertools import combinations_with_replacement

c = list(combinations_with_replacement(range(n),2))
#print(c)

for i in range(len(c)):
    ansset.add(s[c[i][0]:c[i][1]+1])
ans = sorted(list(ansset))

print(ans[k-1])

Fixed submission:
s = input()
k = int(input())
n = len(s)

ansset= set()
for i in range(1,k+1):
    for j in range(n-i+1):
        ansset.add(s[j:j+i])

anslist = sorted(list(ansset))

print(anslist[k-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
k = int(input())
n = len(s)

ansset= set()

for i in range(1,n+1):
    for j in range(n-i+1):
        ansset.add(s[j:j+i])

anslist = sorted(list(ansset))

print(anslist[k-1])


Fixed submission:
s = input()
k = int(input())
n = len(s)

ansset= set()
for i in range(1,k+1):
    for j in range(n-i+1):
        ansset.add(s[j:j+i])

anslist = sorted(list(ansset))

print(anslist[k-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())

x = list(map(int,input().split()))

from statistics import median

for i in range(n):
    print(median(x[:i]+x[i+1:]))

Fixed submission:
n = int(input())

xx = list(map(int,input().split()))
x = sorted(xx)

left = x[n//2-1]
right = x[n//2]
mid = (left+right)/2

for i in range(n):
    if xx[i] >= mid:
        print(left)
    else:print(right)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 0
def count(i):
    res = 0
    for x in a[i:]:
        if x - a[i] <= 1:
            res += 1
    for y in a[:i]:
        if a[i] - y <= 1:
            res += 1
    return res

for i in range(n):
    ans = max(ans,count(i))
print(ans)

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

s = [0]*(a[-1]+1)
for i in range(n):
    s[a[i]] += 1
ans = 0
if a[-1] - 1 > 0:
    for i in range(1,a[-1]):
        ans = max(ans, s[i] + s[i-1] + s[i+1])
    print(ans)
else:
    print(max(s))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
a.sort()
cnts = [0]*(a[-1]+1)
cnt2 = []
cnt4 = []
for i in range(len(a)):
    cnts[a[i]] += 1
    if cnts[a[i]] == 2:
        cnt2.append(a[i])
    if cnts[a[i]] == 4:
        cnt2.remove(a[i])
        cnt4.append(a[i])
ans = 0
if len(cnt2) <= 1 and not(cnt4):
    print(0)
else:
    if len(cnt2) >= 2:
        ans = max(ans, cnt2[-1]*cnt2[-2])
    if cnt4:
        ans = max(ans, cnt4[-1]**2)
    
    print(ans)



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

adic = {}
for i in range(n):
    if a[i] not in adic:
        adic[a[i]] = 0
    adic[a[i]] += 1

alist = sorted(adic.items(),reverse = True)
before = 0
after = 0

for i in range(len(alist)):
    if before == 0:
        if alist[i][1] >= 4:
            before = alist[i][0]
            after = alist[i][0]
            break
        elif alist[i][1] >= 2:
            before = alist[i][0]
    else:
        if alist[i][1] >= 2:
            after = alist[i][0]
            break
print(before * after)
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = [list(map(int,input().split()))for i in range(m)]
from1 = []
to_n = []
for i in range(m):
    if ab[i][0] == 1:
        from1.append(ab[i][1])
    if ab[i][1] == n:
        to_n.append(ab[i][0])

for i in range(len(from1)):
    if from1[i] in to_n:
        print('POSSIBLE')
        break
else:print('IMPOSSIBLE')


Fixed submission:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
ab = [list(map(int,input().split()))for i in range(m)]
from1 = []
to_n = []
for i in range(m):
    if ab[i][0] == 1:
        from1.append(ab[i][1])
    if ab[i][1] == n:
        to_n.append(ab[i][0])
from1.sort()
to_n.sort()
'''
for i in range(len(from1)):
    if from1[i] in to_n:
        print('POSSIBLE')
        break
else:print('IMPOSSIBLE')
二分探索で実装できそう
'''
def bisearch(data,value):#data:ソート済みリスト  value:調べる値
  left = 0
  right = len(data) - 1
  while left <= right:
    mid = (left + right)//2
    if data[mid] == value:
      return mid
    elif data[mid] < value:
      left = mid + 1
    else:
      right = mid - 1
  return -1
for i in range(len(from1)):
    if bisearch(to_n,from1[i]) != -1:
        print('POSSIBLE')
        break
else:print('IMPOSSIBLE')
Buggy python submission (verdict: Runtime Error):
n = int(input())
slist = []
for i in range(n):
    s = sorted(list(input()))
    slist.append(s)

common = [s for s in slist[0]]

for i in range(1,n):
    poplist = []
    for j in range(len(common)):
        if common[j] in slist[i]:
            slist[i].remove(common[j])
        else:
            poplist.append(j)
    for j in range(len(poplist)):
        common.pop(poplist[j])
    #print(common)
common.sort()
print(''.join(common))

Fixed submission:
n = int(input())
slist = [input()for i in range(n)]

alp = {chr(i):float('inf') for i in range(97,97+26)}

for i in range(len(slist)):
    sdic = {chr(i):0 for i in range(97,97+26)}
    for j in range(len(slist[i])):
        sdic[slist[i][j]] += 1
    
    for k in alp:
        alp[k] = min(alp[k],sdic[k])

alplist = sorted(list(alp.items()))
ans = ''
for i in range(26):
    ans += alplist[i][0]*alplist[i][1]
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
slist = []
for i in range(n):
    s = sorted(list(input()))
    slist.append(s)

common = [s for s in slist[0]]

for i in range(1,n):
    poplist = []
    for j in range(len(common)):
        if common[j] in slist[i]:
            slist[i].remove(common[j])
        else:
            poplist.append(j)
    for j in range(len(poplist)):
        common.pop(poplist[j])
    #print(common)

print(''.join(common))







Fixed submission:
n = int(input())
slist = [input()for i in range(n)]

alp = {chr(i):float('inf') for i in range(97,97+26)}

for i in range(len(slist)):
    sdic = {chr(i):0 for i in range(97,97+26)}
    for j in range(len(slist[i])):
        sdic[slist[i][j]] += 1
    
    for k in alp:
        alp[k] = min(alp[k],sdic[k])

alplist = sorted(list(alp.items()))
ans = ''
for i in range(26):
    ans += alplist[i][0]*alplist[i][1]
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
alist = list(map(int,input().split()))
dic = {}
mod = 10**9+7
for i in range(n):
    if alist[i] not in dic:
        dic[alist[i]] = 0
    dic[alist[i]] += 1
k = 0
while n-k-1 >= 2:
    p = n-k-1
    if p == 0:
        if dic[p] != 1:
            print(0)
            exit()
    else:
        if dic[p] != 2:
            print(0)
            exit()
    k += 2
else:
    print(2**(n//2)%mod)



Fixed submission:
n = int(input())
cnts = [0]*n
alist = list(map(int,input().split()))
mod = 10**9+7

for i in range(n):
    cnts[alist[i]] += 1

p = n%2
if p == 1:
    cnts[0] += 1

for i in range(n):
    if p != (i%2):
        if cnts[i] != 2:
            print(0)
            exit()
    else:
        if cnts[i] != 0:
            print(0)
            exit()
print(pow(2,(n//2))%mod)

Buggy python submission (verdict: Runtime Error):
n = int(input())
alist = list(map(int,input().split()))
dic = {}
for i in range(n):
    if alist[i] not in dic:
        dic[alist[i]] = 0
    dic[alist[i]] += 1
k = 0
while n-k-1 >= 2:
    p = n-k-1
    if p == 0:
        if dic[p] != 1:
            print(0)
            exit()
    else:
        if dic[p] != 2:
            print(0)
            exit()
    k += 2
else:
    print(2**(n//2))


Fixed submission:
n = int(input())
cnts = [0]*n
alist = list(map(int,input().split()))
mod = 10**9+7

for i in range(n):
    cnts[alist[i]] += 1

p = n%2
if p == 1:
    cnts[0] += 1

for i in range(n):
    if p != (i%2):
        if cnts[i] != 2:
            print(0)
            exit()
    else:
        if cnts[i] != 0:
            print(0)
            exit()
print(pow(2,(n//2))%mod)

Buggy python submission (verdict: Runtime Error):
n = int(input())
alist = list(map(int,input().split()))
dic = {}
for i in range(n):
    if alist[i] not in dic:
        dic[alist[i]] = 0
    dic[alist[i]] += 1

k = -2
while n-1-k >= 2:
    k += 2
    if n-1-k != 0:
        if dic[n-1-k] != 2:
            print(0)
            exit()
    else:
        if dic[0] != 1:
            print(0)
            exit()

p = n//2
print(2**p)

Fixed submission:
n = int(input())
cnts = [0]*n
alist = list(map(int,input().split()))
mod = 10**9+7

for i in range(n):
    cnts[alist[i]] += 1

p = n%2
if p == 1:
    cnts[0] += 1

for i in range(n):
    if p != (i%2):
        if cnts[i] != 2:
            print(0)
            exit()
    else:
        if cnts[i] != 0:
            print(0)
            exit()
print(pow(2,(n//2))%mod)
Buggy python submission (verdict: Time Limit Exceeded):
X, K, D = [int(x) for x in input().split()]

s=X

for i in range(K):
    if abs(s - D) < abs(s + D):
        s -= D
    else:
        s += D
print(abs(s))

Fixed submission:
X, K, D = [int(x) for x in input().split()]

X=abs(X)
s = min(K, X//D)
K = K - s
X = X - s*D
#print(s, K, X)
if K%2 == 0:
    print(X)
else:
    print(D-X)
Buggy python submission (verdict: Runtime Error):
A=sorted(list(map(int,input().split())))

r = (A[0]+A[1])/2
for i in range(2,len(A)):
    r=(r+A[i])/2
    
print(r)

Fixed submission:
N=int(input()) #not needed for Python, but needed for competition
A=sorted(list(map(int,input().split())))

r = (A[0]+A[1])/2
for i in range(2,len(A)):
    r=(r+A[i])/2
    
print(r)
Buggy python submission (verdict: Runtime Error):
#another way:
def count_digits(n):
    d=0
    while(n>0):
        d += 1
        n = n//10
    return d
c=0
for i in range(1,N+1):
    if (count_digits(i)%2==1):
        c += 1
print(c)

Fixed submission:
N=int(input())
c=0
for i in range(1, N+1):
    if (len(str(i))%2 == 1):
        c += 1
print(c)
Buggy python submission (verdict: Runtime Error):
S=input()
T=sorted(S.split())
print("Yes" if T[0] == T[1] and T[2]=T[3] and T[0] != T[2] else "No")

Fixed submission:

T=sorted(input())
print("Yes" if T[0] == T[1] and T[2] == T[3] and T[0] != T[2] else "No")
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
hs = list(map(int, input().split()))
d = [1]*n

for i in range(m):
    a,b = map(int, input().split())
    if hs[a-1] <= hs[b-1]:
        d[a-1] = 0
    if hs[a-1] >= hs[b-1]:
        d[b-1] = 0
        
    print(hs,d)
    
print(d.count(1))

Fixed submission:
n,m = map(int, input().split())
hs = list(map(int, input().split()))
d = [1]*n

for i in range(m):
    a,b = map(int, input().split())
    if hs[a-1] <= hs[b-1]:
        d[a-1] = 0
    if hs[a-1] >= hs[b-1]:
        d[b-1] = 0
    
print(d.count(1))
Buggy python submission (verdict: Runtime Error):
N = int(input())
L_list = list(map(int,input().split()))


conter = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if a==b or b==c or c==a or a+b<=c or b+c<=a or c+a<=b:
                continue
            counter += 1


print(counter)

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


counter = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            a = L_list[i]
            b = L_list[j]
            c = L_list[k]
            if a==b or b==c or c==a or a+b<=c or b+c<=a or c+a<=b:
                continue
            counter += 1


print(counter)
Buggy python submission (verdict: Runtime Error):
,K,D = map(int,input().split())

y = abs(X)//D

if K<=y:
    print(abs(abs(X)-K*D))

else  :
    a = (X-y*D)
    b = (X-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)

Fixed submission:
X,K,D = map(int,input().split())

y = abs(X)//D

if K<=y:
    print(abs(abs(X)-K*D))

else  :
    a = (abs(X)-y*D)
    b = (abs(X)-(y+1)*D)
    
    C = abs(a)
    D = abs(b)
    
    if (K-y)%2==0:
        print(C)
    else:
        print(D)


#print(y+1)
#print(1000000000000000)
Buggy python submission (verdict: Runtime Error):
N = int(input())
C = list(input())

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Fixed submission:
N = int(input())
C = input()

flag_left = 0
flag_right = N-1
counter = 0

ok = max(C.count("W"),C.count("R"))
if ok ==N:
    print(0)
    exit()

while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Buggy python submission (verdict: Runtime Error):
N = int(input())
C = input()

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>=N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<=0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Fixed submission:
N = int(input())
C = input()

flag_left = 0
flag_right = N-1
counter = 0

ok = max(C.count("W"),C.count("R"))
if ok ==N:
    print(0)
    exit()

while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Buggy python submission (verdict: Runtime Error):
N = int(input())
C = input()

flag_left = 0
flag_right = N-1
counter = 0


while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)

Fixed submission:
N = int(input())
C = input()

flag_left = 0
flag_right = N-1
counter = 0

ok = max(C.count("W"),C.count("R"))
if ok ==N:
    print(0)
    exit()

while flag_left<flag_right:
    while C[flag_left]=="R":
        flag_left += 1
        if flag_left>N-1:
            break
    while C[flag_right]=="W":
        flag_right -= 1
        if flag_left<0:
            break
        
    if flag_left<flag_right:
        counter += 1
    flag_left += 1
    flag_right -= 1
print(counter)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
import numpy as np
li = np.array([0]*N)
ans = 0
for i in range(N):
    li[i:N:i+1]+=1
    ans += li[i]*(i+1)
    

print(ans)

Fixed submission:
N = int(input())

counter = 0
for n in range(1,N+1):
    a = N//n
    counter += a*(a+1)*n//2


print(counter)

Buggy python submission (verdict: Runtime Error):
N = int(input())
import numpy as np
li = np.array([0]*N)
ans = 0
for i in range(N):
    li[i:N:i+1]+=1
    ans += li[i]*(i+1)
    

print(ans)

Fixed submission:
N = int(input())

counter = 0
for n in range(1,N+1):
    a = N//n
    counter += a*(a+1)*n//2


print(counter)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
import numpy as np
li = np.array([0]*N)

for i in range(N):
    li[i:N:i+1]+=1
    li[i]*=(i+1)
    

print(sum(li))


Fixed submission:
N = int(input())

counter = 0
for n in range(1,N+1):
    a = N//n
    counter += a*(a+1)*n//2


print(counter)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
import numpy as np
li = np.array([0]*N)


for i in range(N):
    li[i:N:i+1]+=1

print(sum(i*a for i,a in enumerate(li,1)))

Fixed submission:
N = int(input())

counter = 0
for n in range(1,N+1):
    a = N//n
    counter += a*(a+1)*n//2


print(counter)
Buggy python submission (verdict: Runtime Error):
N = int(input())


A = list(map(int,input().split()))
ok = [0]*pow(10,5)
Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]

f):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0
    
    if max(ok)==N:
        memo1=i
        memo2=ok.index(N)
        break



for i in range(memo1+1,Q):
    if li[i][0]==memo2:
        print(N*li[i][1])
        memo2=li[i][1]
    else:
        print(memo2*N)

Fixed submission:
N = int(input())


A = list(map(int,input().split()))
ok = [0]*pow(10,6)
Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]

for i in range(N):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0

Buggy python submission (verdict: Runtime Error):
N = int(input())

A = list(map(int,input().split()))
ok = [0]*pow(10,5)
Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]

for i in range(N):
    ok[A[i]]+=1
memo=0

S = sum(A)
for i in range(Q):
    print(S + ok[li[i][0]]*(li[i][1]-li[i][0]))
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0

Fixed submission:
N = int(input())


A = list(map(int,input().split()))
ok = [0]*pow(10,6)
Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]

for i in range(N):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0

Buggy python submission (verdict: Time Limit Exceeded):
N= int(input())
from collections import Counter

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

Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]


memo2=0

S = sum(A)
cou = Counter(A)

for i in range(Q):
    global ohno
    S+=cou[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = cou[li[i][0]]
    cou[li[i][1]]+=a
    cou[li[i][0]]=0
    
    if set(cou.values())==1:
        global ohno
        memo1=i
        memo2=cou[max(cou.values())]
        for i in range(memo1+1,Q):
            if li[i][0]==memo2:
                print(N*li[i][1])
                memo2=li[i][1]
            else:
                print(memo2*N)

Fixed submission:
N = int(input())


A = list(map(int,input().split()))
ok = [0]*pow(10,6)
Q = int(input())

li = [list(map(int,input().split())) for i in range(Q)]

for i in range(N):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0
Buggy python submission (verdict: Time Limit Exceeded):
A,B,C,K = map(int,input().split())
li = [1 for a in range(A)]+ [ 0 for b in range(B)]+ [ -1 for c in range(C)]
print(sum(li[:K]))


Fixed submission:
A,B,C,K = map(int,input().split())

if K<=A:
    print(K)
elif A<K<=B:
    print(A)
else:
    print(A - (K-A-B))