Problem:


Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Runtime Error

Solution code:
import sys
import time
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [a = list(map(int,input().split())) for i in range(q)]

ans = 0
data = [0]*100020
for i in a:
  data[i] += 1
  ans += i

for b,c in bc:
  data[c] += data[b]
  data[b] = 0
  ans += c*data[b] - b*data[b]
  print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
BC = [list(map(int, list(input().split(' ')))) for i in range(Q)]

count = [0] * 10**5 + 5
for i in range(N):
    count[A[i]] += 1

s = sum(A)
for i in range(Q):
    s = s - BC[i][0] * count[BC[i][0]] + BC[i][1] * count[BC[i][0]]
    count[BC[i][1]] += count[BC[i][0]]
    count[BC[i][0]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
BC = [list(map(int, list(input().split(' ')))) for i in range(Q)]

count = [0] * 10**5
for i in range(N):
    count[A[i]] += 1

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
BC = [list(map(int, list(input().split(' ')))) for i in range(Q)]

count = [0] * 10**5
for i in range(N):
    count[A[i]] += 1

s = sum(A)
for i in range(Q):
    s = s - BC[i][0] * count[BC[i][0]] + BC[i][1] * count[BC[i][0]]
    count[BC[i][1]] += count[BC[i][0]]
    count[BC[i][0]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
#Get the number
#Declare a variable to store the sum and set it to 0
#Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
#Divide the number by 10 with help of ‘/’ operator
#Print result

def getResult(n): 
  
    sum = 0
  
    # Single line that calculates sum  
    while(n > 0): 
        result += int(n%10) 
        n = int(n/10) 
  
    return result
  
print(get(n))

Submission status: Runtime Error

Solution code:

n=int(input())

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

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

for i in A:
    alist[i]+=1
asum=sum(A)
for i in range(q):
    b,c=map(int , input().split())
    bnum=alist[b]
    asum+=bnum*(c-b)
    alist[b]=0
    alist[c]+=bnum
    print(asum)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
aa = [0 for i in range(10 ** 5)]
for j in a:
    aa[j] += 1
suma = sum(a)
for f in range(q):
    b, c = map(int, input().split())
    suma += (c-b) * aa[b]
    aa[c] += aa[b]
    aa[b] = 0
    print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()
 
n=ii()
a=lmi()
suma=sum(a)
counter=[0]*(10**5)
for i in a:
  counter[i]+=1
#print(counter)
q=ii()
 
for i in range(q):
  b,c=mi()
  suma+=(c-b)*counter[b]
  
  counter[c]+=counter[b]
  counter[b]=0
  #print(counter)
  print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()

n=ii()
a=lmi()
suma=sum(a)
counter=[0]*(10**5)
for i in a:
  counter[i]+=1
#print(counter)
q=ii()

for i in range(q):
  b,c=mi()
  suma+=(c-b)*counter[b]
  
  counter[c]+=Counter[b]
  counter[b]=0
  #print(Counter)
  print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()

n=ii()
a=lmi()
suma=sum(a)
Counter=[0]*(10**5)
for i in a:
  Counter[i]+=1
#print(Counter)
q=ii()

for i in range(q):
  b,c=mi()
  suma-=b*counter[b]
  suma+=c*counter[b]
  
  Counter[c]+=Counter[b]
  Counter[b]=0
  #print(Counter)
  print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()
 
n=ii()
a=lmi()
suma=sum(a)
counter=[0]*(10**5)
for i in a:
  counter[i]+=1
#print(counter)
q=ii()
 
for i in range(q):
  b,c=mi()
  suma+=(c-b)*counter[b]
  
  counter[c]+=counter[b]
  counter[b]=0
  #print(counter)
  print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()
 
n=ii()
a=lmi()
suma=sum(a)
Counter=[0]*(10**5)
for i in a:
  Counter[i]+=1
#print(Counter)
q=ii()
 
for i in range(q):
  b,c=mi()
  suma+=(c-b)*Counter[b]
  
  Counter[c]+=Counter[b]
  Counter[b]=0
  #print(Counter)
  print(suma)

Submission status: Runtime Error

Solution code:
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()

n=ii()
a=lmi()
suma=sum(a)
Counter=[0]*(10**5)
for i in a:
  Counter[i]+=1
#print(Counter)
q=ii()

for i in range(q):
  b,c=mi()
  suma+=(c-b)*Counter[b]
  
  Counter[c]+=Counter[b]
  Counter[b]=0
  #print(Counter)
  print(suma)

Submission status: Runtime Error

Solution code:
import sys
import time
import math
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))
# ------------------------------

N = int(input())
nl = [0] * 10101
ls = inpl()
ans = 0
for i in ls:
	nl[i] += 1
	ans += i
M = int(input())
for _ in range(M):
	a, b = map(int, input().split())
	ans = ans - (nl[a] * a) + (nl[a] * b)
	nl[b] += nl[a]
	nl[a] = 0
	print(ans)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Runtime Error

Solution code:
import sys
import time
import math
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))
# ------------------------------

N = int(input())
nl = [0] * 10101
ls = inpl()
ans = 0
for i in ls:
	nl[i] += 1
	ans += i
M = int(input())
for _ in range(M):
	a, b = map(int, input().split())
	ans = ans - nl[a] * a + nl[b] * b
	nl[b] += nl[a]
	nl[a] = 0
	print(ans)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))

su = sum(arr)

q = int(input())
for i in range(q):
	b,c = map(int, input().split())
    si = su -b + c
    print(si)

Submission status: Runtime Error

Solution code:
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
c=Counter(A)
commons=c.most_common()

ans=0

dic={}
for common in commons:
    dic[common[0]]=common[1]
    ans+=common[0]*common[1]
# print(dic)
# print(ans)

Q=int(input())
for i in range(Q):
    b,c=map(int,input().split())
    ans+=(c-b)*dic[b]
    dic[c]+=dic[b]
    dic[b]=0
    # print(dic)
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
c=Counter(A)
commons=c.most_common()

ans=0

dic={}
for common in commons:
    dic[common[0]]=common[1]
    ans+=common[0]*common[1]
# print(dic)
# print(ans)

Q=int(input())
for i in range(Q):
    b,c=map(int,input().split())
    ans+=(c-b)*dic[b]
    dic[b]=0
    dic[c]+=dic[b]
    # print(dic)
    print(ans)

Submission status: Runtime Error

Solution code:
# input
N = int(input())
A = Counter(list(map(int, input().split())))

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
for before, after in BC:
    if before in A.keys():
        cnt = A[before]
        del A[before]
        add = Counter({after: cnt})
        A = A + add
    print(sum(list(A.elements())))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v

#print(d)
ans=sum(d2.values())
for i in range(q):
  #print(d2)
  
  if d.get(b[i]):
    removed=d.pop(b[i])
    _=d2.pop(b[i])
    d[c[i]]+=removed
    d2[c[i]]=c[i]*d[c[i]]
  else:
    pass
  #print((np.array(list(d2.values()))))
  ans+=(c[i]-b[i])*removed
  print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
L = [0]*100000
s = sum(A)
for a in A:
    L[a] += 1 #数字のカウント
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    s += L[B]*(C-B) #合計値の変化
    L[C] += L[B] 
    L[B] = 0 #Bの値の数だけCに移す
    print(s)

Submission status: Runtime Error

Solution code:
number = int(input())

numbers = [i for i in range(1, number+1)]
print(' '.join(map(str, numbers)))
count = int(input())

sum_numbers = []
for i in range(count):
    before, after = map(int, input().split())
    numbers = [after if value == before else value for value in numbers]
    sum_numbers.append(sum(numbers))

for i in range(count):
    print(sum_numbers[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B,C = [0 for i in range(Q)],[0 for i in range(Q)]
for i in range(Q):
    B[i],C[i] = map(int,input().split())

n = [0 for i in range(100000)]
S = 0
for i in range(N):
    n[A[i]] = n[A[i]] + 1
    S = S + A[i]

for i in range(Q):
    S = S + (C[i]-B[i])*n[B[i]]
    n[C[i]] = n[C[i]] + n[B[i]]
    n[B[i]] = 0
    print(S)

Submission status: Runtime Error

Solution code:
N=int(input())
array=map(int,input().split())
A=[]
for i in array:
    A.append(i)

dict_num={i:0 for i in range(100000)}

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

#全数値の合計計算
sum=0
#各数字の出現回数の計測
for i in range(len(A)):
    dict_num[A[i]]+=1
    sum+=A[i]

#合計の再計算 & 数字の出現回数の再計算
for i in matrix:
    sum+=(i[1]-i[0])*dict_num[i[0]]
    dict_num[i[1]]+=dict_num[i[0]]
    dict_num[i[0]]=0
    print(sum)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())

base=sum(A)

from collections import Counter

ad=Counter(A)

for _ in range(Q):
    b,c=map(int.input().split())
    if ad[b]!=0:
        base+=(c-b)*ad[b]
        ad[c]+=ad[b]
        ad[b]=0
    print(base)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
L=[0]*100000
for i in range(1,100000):
    a=A.count(i)
    L[i]=a
ans=sum(A)
for _ in range(Q):
    B,C=map(int,input().split())
    ans+=L[B]*(C-B)
    L[C]+=L[B]
    L[B]=0
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B,C = [],[]
for i in range(Q):
  b,c = map(int,input().split())
  B.append(b)
  C.append(c)

S = [0]*10001
for i in A:
  S[i] += 1

for i in range(Q):
  S[C[i]] += S[B[i]]
  S[B[i]] = 0

  ans = 0

  for i in range(len(S)):
    ans += i*S[i]

  print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
l = input()
q = int(input())

d = {}
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    print(sum(int, l.split()))

Submission status: Runtime Error

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = dic[B]
    dic[B] = 0
    if (C not in dic) : dic[C] = 1
    else : dic[C] += cnt
    sums += cnt * (C - B)
    print(sums)

Submission status: Runtime Error

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = dic[B]
    dic[B] = 0
    dic[C] += cnt
    sums += cnt * (C - B)
    print(sums)

Submission status: Runtime Error

Solution code:

n = int(input())
a = list(map(int, input().split()))
q = int(input())

a.sort()
A = [0] * 100000
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

for n in range(q):
  b,c = list(map(int, input().split()))
  ans += (c * A[b]) - (b * A[b])
  A[c] += A[b]
  A[b] = 0
  print(ans)

Submission status: Runtime Error

Solution code:

n = int(input())
a = list(map(int, input().split()))
q = int(input())

a.sort()
A = [0] * 100000
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

for n in range(q):
  b,c = list(map(int, input().split()))
  ans += (c * A[b]) - (b * A[b])
  A[c] += A[b]
  A[b] = 0
  print(ans)

Submission status: Runtime Error

Solution code:
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(3000):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5)
for i in range(1,10**5):
  dp[i]=a.count(i)

for j in range(q):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]=dp[b]+dp[c]
  dp[b]=0
  print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
m=list(map(int,input().split()))
d={}
k=0
for i in range(n):
    k+=m[i]
    if m[i] in d:
        d[m[i]]+=1
    else:
        d[m[i]]=1
for x in range(int(input())):
    
    b,c=list(map(int,input().split()))

    k+=d[b]*(c-b)
    
    if c in d:
        d[c]+=d[b]
    else:
        d[c] = d[b]
    del d[b] 
    print(k)

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
s,*l=[0]*100001
for i in a[:n]: s+=i; l[i]+=1
for b,c in zip(*[iter(a[n+1:])]*2): s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)

Submission status: Runtime Error

Solution code:
# 整数の入力
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
process = []
proc_append = process.append
hash = [0]*100000
for i in range(Q):
    proc_append(tuple(input().split()))
    process[i] = tuple(map(int,process[i]))

goukei = sum(A)

for i in A:
	hash[i] += 1

for b,c in process:
	if hash[b] != 0:
		goukei += (c-b)*hash[b]
		hash[c] += hash[b]
		hash[b] = 0
	print(goukei)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
process = []
proc_append = process.append
for i in range(Q):
	proc_append(tuple(input().split()))
	process[i] = tuple(map(int,process[i]))

for b,c in process:
	A.sort()
	if b not in set(A):
		print('not in')
		continue
	for i in range(N):
		if b==A[i]:
			begin = i
			break
	for i in range(begin,N):
		if b!=A[i]:
			end = i-1
			break
	for i in range(begin,end+1):
		A[i] = c
	print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int,input().split()) for _ in range(Q)]
B, C = [i for i in zip(*BC)]
#print(B,C)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1

ans = sum(A)
for q in range(Q):
  bc = C(q) - B(q)
  ans = ans + bc * dic.get(B(q),0)
  dic[C(q)] = dic.get(C(q),0) + dic.get(B(q),0)
  dic[B(q)] = 0
  print(ans)

Submission status: Runtime Error

Solution code:
def my_input():
    i = list(map(int, input().split()))
    if len(i) == 1:
        return i[0]
    return i


n = my_input()
a = [0]+my_input()
a.sort()
d = {}
for i in range(1, n+1):
    if a[i] != a[i-1]:
        d[a[i]] = 0
    d[a[i]] += 1
s = sum(a)
q = my_input()
for i in range(q):
    b, c = my_input()
    if b in d:
        b_count = d.pop(b)
        s += (c-b)*b_count
        if c in d:
            d[c] += b_count
        else:
            d[c] = b_count
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
	b[i],c[i] = map(int,input().split())

s = 0
L = [0]*(10**5)
for A in a:
	L[A] += 1
	s += A

for i in range(q):
	s += L[b[i]]*(c[i]-b[i])
	L[c[i]] += L[b[i]]
	L[b[i]] = 0
	print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[]
c=[]
for _ in range(q):
    x,y=map(int,input().split())
    b.append(x)
    c.append(y)

d=[0]*10001
for i in a:
    d[i]+=1

for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans=0
    for i in range(len(d)):
        ans+=i*d[i]
    print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[]
c=[]
for _ in range(q):
    x,y=map(int,input().split())
    b.append(x)
    c.append(y)

d=[0]*10000
for i in a:
    d[i]+=1

for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans=0
    for i in range(len(d)):
        ans+=i*d[i]
    print(ans)

Submission status: Runtime Error

Solution code:
"""
input.txt

2
1 2
3
1 100
2 100
100 1000

"""

from sys import stdin
data = stdin.readlines()

n = int(data[0])
a = [int(s) for s in data[1].split()]
q = int(data[2])

i = 1
l = []
while i <= q:
    l.append(int(data[i+2].split()[0]))
    l.append(int(data[i+2].split()[1]))
    i += 1

dic = {}
for s in range(1,10**5):
    tmp = {s: a.count(s)}
    dic.update(tmp)

s = sum(a)
i = 1
while i <= q:
    s = s + (l[2*i-1]-l[2*i-2]) * dic[l[2*i-2]]
    print(s)
    dic[l[2*i-1]] += dic[l[2*i-2]]
    dic[l[2*i-2]] = 0
    i += 1

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
    	ans = ans+((c-b)*v)
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
    ans = ans+((c-b)*v)
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
    ans = ans+((c-b)*v)
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = sum(a)
#print(ans)
q = int(input())
import collections
cc = collections.Counter(a)
#print(cc)
for i in range(q):
    b,c = map(int,input().split())
    tmp = cc[b]
    cc[b] = 0
    s=s-b*tmp+c*tmp
    cc[c]+=tmp
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = sum(a)
#print(ans)
q = int(input())
import collections
cc = collections.Counter(a)
print(cc)
for i in range(q):
    b,c = map(int,input().split())
    tmp = cc[b]
    cc[b] = 0
    s=s-b*tmp+c*tmp
    cc[c]+=tmp
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
bc = [[int(i) for i in input().split()] for j in range(Q)]

counter = [0] * 100000
for i in a:
    counter[i] += 1

ans = sum(a)

for _ in bc:
    ans = ans + counter[_[0]] * _[1] - counter[_[0]] * _[0]
    counter[_[1]] += counter[_[0]]
    counter[_[0]] = 0
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic = {}

for i in range(100000):
  dic[i] = A.count(i)
    
Q = int(input())

base = sum(A)

for i in range(Q):
  B, C = map(int, input().split())
  tmp = dic[B]
  dic[B] = 0
  dic[C] += tmp
  base += (C-B)*tmp
  print(base)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

sumA = sum(A)
cnt_num = []
for i in range(10**5):
    cnt_num.append(A.count(i))

for i in range(Q):
    B, C = map(int, input().split())
    sumA += (C - B) * cnt_num[B]
    print(sumA)
    cnt_num[C] += cnt_num[B]
    cnt_num[B] = 0

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
arr = [0] * 100000
for val in a:
  arr[val] += 1
sums = sum(a)
q = int(input())
for _ in range(q):
  b, c = map(int,input().split())
  diff = (c-b) * (arr[b])
  sums += diff
  print(sums) 
  arr[c] += arr[b]
  arr[b] = 0

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
arr= [0] * 10**5
for val in a:
  arr[val]+=1
sums=sum(a)
q=int(input())
for _ in range(q):
  b,c=map(int,input().split())
  diff=(c-b)*(arr[b])
  sums+=diff
  print(sums) 
  arr[c]+=arr[b]
  arr[b]=0

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
numl=[0 for i in range(10**5+1)]
s=sum(a)
for aa in a:
  numl[aa-1]+=1
 
for qq in range(q):
  x,y=map(int,input().split())
  s+=numl[x-1]*(y-x)
  print(s)
  numl[y-1]+=numl[x-1]
  numl[x-1]=0
  print(numl)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = []
amax=max(A)
for i in range(amax):
    num.append(A.count(i))

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
　  print(total + (C - B) * num[B])
    total += + (C - B) * num[B]
    num[C] += num[B]
    num[B] -= num[B]

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num=[]
for i in range(100000):
    num.append(A.count(i))

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    print(total + (C - B) * num[B])
    total=total + (C - B) * num[B]
    num[C] += num[B]
    num[B] -= num[B]

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num=[]
amax=max(A)
for i in range(amax):
    num.append(A.count(i))

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    print(total + (C - B) * num[B])
    total=total + (C - B) * num[B]
    num[C] += num[B]
    num[B] -= num[B]

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num=[]
for i in range(100000):
    num.append(A.count(i))

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    numb = num[B]
    num[B] -= numb
    num[C] += numb
    print(total + (C - B) * numb)
    total=total + (C - B) * numb

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num=[]
for i in range(10000):
    num.append(A.count(i))

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    numb = num[B]
    num[B] = num[B] - numb
    num[C] = num[C] + numb
    print(total + (C - B) * numb)
    total=total + (C - B) * numb

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))

d=dict()
sum=0
for i in A:
  if not i in d:
    d[i]=0
  d[i]+=1
  sum+=i
#counterと同じ
for i in range(int(input())):
  b,c=map(int,input().split())
  if not c in d:
    d[c]=0
    
  sum=sum-(d[b]*b)
  sum=sum-(d[c]*c)
  d[c]+=d[b]
  d[b]=0
  sum=sum+(d[c]*c)
  #print(d)
  print(sum)

Submission status: Runtime Error

Solution code:
seq = [int(x) for x in input().split()]
Q = int(input())
d = []
somme = []
e = []
a=b=0
for i in range(Q):
    a,b = [int(x) for x in input().split()]
    if a in seq:
        e = [ b if x == a else x for x in seq][:]
    else:
        e = seq[:]
    somme.append(sum(e))
    seq = e[:]
for i in somme:
    print(i)

Submission status: Runtime Error

Solution code:

import collections

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    cnt = collections.defaultdict(int)
    for var in a:
        cnt[var] += 1
    
    sum_a = sum[a]
    
    q = int(input())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        diff = (c-b) * cnt[b]
        sum_a += diff
        print(sum_a)
        cnt[c] += cnt[b]
        cnt[b] = 0
        
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
D = {}
res = 0
for a in A:
    res += a
    if a in D.keys():
        D[a] += 1
    else:
        D[a] = 1

q = int(input())


for cq in range(q):
    b, c = map(int, input().split())
    res += (c - b) * D[b]
    if c in D.keys():
        D[c] += D[b]
    else:
        D[c] = D[b]
    print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
D = {}
res = 0
for a in A:
    res += a
    if a in D.keys():
        D[a] += 1
    else:
        D[a] = 1

q = int(input())


def solve():
    global res, D
    b, c = map(int, input().split())
    res += (c - b) * D[b]
    if c in D.keys():
        D[c] += D[b]
    else:
        D[c] = D[b]
    print(res)

for cq in range(q):
    solve()

Submission status: Runtime Error

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    #max_num = 10**5
    max_num = 10
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    
    query = [map(int, input().split()) for _ in range(q)]
    num = [0 for _ in range(max_num+1)]
    for i in a_list:
        num[i] += 1

    ans = sum(a_list)

    for b,c in query:
        n = num[b]
        num[b] = 0
        num[c] += n
        ans += (c-b)*n
        print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter

def Replacing():

    Num = int(input())

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

    Sum = sum(value_A)
    Counter_value = Counter(value_A)

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        Sum += (value_B[1] - value_B[0])*Counter_value(value_B[0])
        Counter_value[value_B[1]] += Counter_value[value_B[0]]
        Counter_value[value_B[0]] = 0

        print(Sum)

if __name__ == '__main__':
    Replacing()

Submission status: Runtime Error

Solution code:
from collections import Counter

def Replacing():

    Num = int(input())

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

    Sum = sum(value_A)
    Counter_value = Counter(value_A)

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        Sum += (value_B[1] - value_B[0])*Counter_value(value_B[0])
        Counter_value[Ci] += Counter_value[Bi]
        Counter_value[Bi] = 0

        print(Sum)

if __name__ == '__main__':
    Replacing()

Submission status: Runtime Error

Solution code:


def main():
    n, *i = map(int, open(0).read().split())
    a = i[:n]　　＃数列Aのリスト
    _ = i[n]　　　＃Qのこと
    s = sum(a)　　＃数列Aの総和

    m = [0] * (10 ** 5 + 1)　　＃Aは1<A<10^5なのでラベル用リストを作成
    for x in a:　　　　　　　　　＃Aの個数をラベル化。リストmは個数を記録している
        m[x] += 1

    ans = []
    for b, c in zip(*[iter(i[n + 1:])] * 2):
        m[c] += m[b]　　　　　　＃ｃの個数にbの個数を足し算
        s += (c - b) * m[b]　　＃s:数列Aの総和に、(c-b)×(bの個数)を足し算
        m[b] = 0　　　　　　　　　＃bの個数はゼロに
        ans.append(s)　　　　　　＃リストansにSの変化を記録していく

    print("\n".join(map(str, ans)))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
seq = [int(x) for x in input().split()]
Q = int(input())
somme = []
for i in range(Q):
  l = [int(x) for x in input().split()]
  a = l[0]
  b = l[1]
  if a in seq:
   	e = [ b if x == a else x for x in seq]
  else:
    e = seq[:]
  somme.append(sum(e))
  seq.clear()
  seq = e[:]
  e.clear()
for i in somme:
  print(i)
I love this game

Submission status: Runtime Error

Solution code:
import re
n=int(input())
s=str(input())
q=int(input())
 
for i in range(q):
 x,y=map(str, input().split())
 s=re.sub(x,y,s)
 ans=0 
 s_list=list(s.split())
 for j in range(q+1):
  ans+=int(s_list[j])
 print(ans)

Submission status: Runtime Error

Solution code:
import re
n=int(input())
s=str(input())
q=int(input())
 
for i in range(q):
 x,y=map(str, input().split())
 s=re.sub(x,y,s)
 print(s)
 ans=0 
 s_list=list(s.split())
 for j in range(q+1):
  ans+=int(s_list[j])
 print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=str(input())
q=int(input())
 
for i in range(q):
 x,y=map(str, input().split())
 s.replace(x,y)
 ans=0 
 s_list=list(s.split())
 for j in range(q+1):
  ans+=int(s_list[j])
  print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=str(input())
q=int(input())
 
for i in range(q):
 x,y=map(str, input().split())
 s.replace(x,y)
ans=0 
s_list=list(s.split())
for j in range(q+1):
 ans+=int(s_list[j])
 
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=str(input())
q=int(input())
 
for i in range(q):
 x,y=map(str, input().split())
 s.replace(x,y)
ans=0 
s_list=list(s.split())
for j in range(q):
 ans+=int(s_list[j])
 
print(ans)

Submission status: Runtime Error

Solution code:
import re
n=int(input())
s=[]
s=input().split()
q=int(input())

for i in range(q):
 x,y=map(str, input().split())
 s=re.sub(str(x),str(y),str(s))
 
for j in range(q):
 ans+=int(s[j])
 
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=[]
s=input().split()
q=int(input())
for i in range(q):
 x,y=map(int,input().split())
 s=re.sub(x,y,s)
 
for j in range(q):
 ans+=int(s[j])
 
print(ans)