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: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    from collections import Counter
    N = int(input())
    A = list(map(int, input().split()))
    D = Counter(A)
    S = sum(A)
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        b, c = map(int, input().split())
        x = D[b]
        y = D[c]
        temp = -x * b + x * c
        print(S+temp)
        S += temp
        D[b] = 0
        D[c] += x  

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
list = [int(i) for i in input().split()]
for i in list:
    if i not in dic:
        dic[i] = 0
    dic[i] += 1
q = int(input())
ans = sum(list)
for j in range(q):
    b,c = (int(x) for x in input().split())
    if b not in dic:
        print(ans)
        continue
    else:
        if c not in dic:
            dic[c] = dic[b]
        else:
            dic[c] += dic[b]
    ans = ans + (c-b)*dic[b]
    dic[b] = 0
    print(ans)

Submission status: Accepted

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

dp = [0]*(10**5+1)

accum = 0
for value in acollection:
    dp[value] = dp[value] + 1
    accum += value

q = int(input())

for _ in range(q):
    b, c = map(int, input().split())

    changingNum = dp[b]
    diff = c * changingNum - b * changingNum
    accum += diff
    print(accum)

    dp[c] = dp[c] + changingNum
    dp[b] = 0

Submission status: Accepted

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

asum = sum(a)
acount = []
for i in range(10**5):
    acount.append(0)
for i in a:
    acount[i-1] += 1

q = int(input())

ans = []
for i in range(q):
    b,c = map(int, input().split())
    if acount[b-1] > 0:
        asum = asum - b * acount[b-1]
        acount[c-1] += acount[b-1]
        asum = asum + c * acount[b-1]
        acount[b-1] = 0
    ans.append(asum)
for i in ans:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc = [list(map(int,input().split())) for _ in range(q)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
ans = 0
for i,x in enumerate(N):
    if x != 0:
        ans += i*x #初期値

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
cnt = [0]*100005
for i in input().split():
	cnt[int(i)]+=1
	a+=[int(i)]
ans = sum(a)
Q=int(input())
for i in range(Q):
	b,c=map(int,input().split())
	ans += cnt[b]*c - cnt[b]*b
	cnt[c]+=cnt[b]
	cnt[b]=0
	print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
bc = []
for i in range(q):
  bc.append(list(map(int, input().split())))

c = collections.Counter(a)
ans = sum(a)
for i in range(q):
  add = bc[i][1] - bc[i][0]
  cnt = c[bc[i][0]]
  
  ans += cnt * add
  c[bc[i][1]] += c[bc[i][0]]
  c[bc[i][0]] = 0;
  
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

R = lambda: map(int, input().split())
R()
d = Counter(R())
s = sum(d.elements())
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * d[b]
  print(s)
  d[c]+=d[b]
  d[b] = 0

Submission status: Accepted

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

Submission status: Accepted

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

dic = {}
check = [0]*100000
for i in A:
    if check[i-1] == 0:
        dic[i] = 1
        check[i-1] = 1
    else:
        dic[i] += 1
ans = 0
for i in dic.keys():
    ans += i*dic[i]

for i in range(Q):
    B, C = map(int, input().split())
    if check[B-1] == 1:
        if check[C-1] == 1:
            ans = ans + dic[B]*(C-B)
            dic[C] += dic[B]
            check[B-1] = 0
            dic[B] = 0
            print(ans)
        else:
            ans += dic[B]*(C-B)
            check[C-1] = 1
            check[B-1] = 0
            dic[C] = dic[B]
            dic[B] = 0
            print(ans)

    else:
        print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = Counter(map(int, input().split()))
q = int(input())
s = sum(x * y for x, y in a.items())
for _ in range(q):
  b, c = map(int, input().split())
  s = s + (c - b) * a[b]
  a[c] += a[b]
  a[b] = 0
  print(s)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
As =list(map(int,input().split(" ")))
q = int(input())
s = sum(As)
di = defaultdict(lambda :0)
for a in As:
    if a not in di:
        di[a] = 1
    else:
        di[a] += 1
anss = []
for _ in range(q):
    b,c =list(map(int,input().split(" ")))
    
    diff1 = di[b] * b
    temp = di[b]
    di[b] = 0
    diff2 = temp * c
    di[c] += temp
    s -= diff1
    s += diff2
    anss.append(s)
print("\n".join(list(map(str,anss))))

Submission status: Accepted

Solution code:
n=int(input())
x=[int(x) for x in input().split()]
d={}
for i in x:
	if i not in d:
		d[i]=1
	else:d[i]+=1
s=sum(x)
q=int(input())
for i in range(q):
	b,c=map(int,input().split())
	if b in d:
		zz=d[b]
		d[b]=0
		if c not in d:
			d[c]=zz
		else:d[c]+=zz
		s-=b*zz
		s+=c*zz
		print(s)
	else:
		print(s)

Submission status: Accepted

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

for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
s = 0
for j in D:
  s += j * D[j]
for i in range(Q):
  bi, ci = map(int, input().split())
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    s += (ci - bi) * D[bi]
    del D[bi]
  print(s)

Submission status: Accepted

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

l = [0 for i in range(10**5+1)]
for i in range(n):
    l[a[i]] += 1

ans = 0
for i in range(1,10**5+1):
    ans += l[i]*i

for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    before = 0
    after = 0
    if l[b] != 0:
        before = l[b]*b + l[c]*c
        l[c] += l[b]
        l[b] = 0
        after = l[b]*b + l[c]*c
    ans = ans-(before-after)
    print(ans)

Submission status: Accepted

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

s=0
d={}
for i,Ai in enumerate(As):
    s+=Ai
    if Ai in d:
        d[Ai]+=1
    else:
        d[Ai]=1

Q=int(input())
for i in range(Q):
    b,c = map(int,input().split())
    if b in d:
        nb=d[b]
        d[b]=0
        s-=nb*b
        if c in d:
            d[c]+=nb
        else:
            d[c]=nb
        s+=nb*c
    print(s)

Submission status: Accepted

Solution code:
N = int(input())

A_dict = {}
for a in input().split():
    a = int(a)
    if a in A_dict:
        A_dict[a] += 1
    else:
        A_dict[a] = 1

total = 0
for key in A_dict:
    total += key * A_dict[key]

Q = int(input())

for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    if B in A_dict:
        val_B = A_dict.pop(B)
        if C in A_dict:
            A_dict[C] += val_B
        else:
            A_dict[C] = val_B
        total += (C - B) * val_B
    print(total)

Submission status: Accepted

Solution code:
from collections import Counter

R = lambda: map(int, input().split())
R()
d = Counter(R())
s = sum(k * v for k,v in d.items())
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * d[b]
  print(s)
  d[c]+=d[b]
  d[b] = 0

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())

    Co = Counter(A)
    # print(Co)
    SUM = sum(A)
    for _ in range(Q):
        b,c = map(int,input().split())
        SUM += (c-b) * Co[b]
        Co[c] += Co[b]
        Co[b] = 0
        # print(Co)
        print(SUM)



main()

Submission status: Accepted

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

L = [0] * (10 ** 5 + 1)
total = sum(A)

for i in range(N):
    n = A[i]
    L[n] += 1

for i in range(Q):
    b = BC[i][0]
    c = BC[i][1]
    m = L[b]
    total -= b * m
    total += c * m
    L[b] = 0
    L[c] = L[c] + m
    print(total)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
y = [0]*10**5
for i in li:
    y[i-1] += 1
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
x = sum(li)
#print(y)
for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    d = y[b-1]
    e = y[c-1]
    y[b-1] = 0
    y[c-1] = d+e
    x = x + (c-b) * d
    print(x)
    #print(y)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
S = sum(A)
for a in A:
    cnt[a] += 1

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    num_of_b = cnt[b]
    S += num_of_b * c - num_of_b * b
    cnt[c] += num_of_b
    cnt[b] -= num_of_b

    print(S)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
B = [list(map(int, input().split())) for _ in range(q)]

from collections import Counter
ans = sum(a.copy())
a = Counter(a)

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

Submission status: Accepted

Solution code:
def count(l):
    res = {}
    for d in l:
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return res
n = int(input())
data = list(map(int, input().split()))
count = count(data)
ans = sum(data)
q = int(input())
answers = []
for _ in range(q):
    b, c = map(int, input().split())
    if b in count:
        num = count[b]
        ans += c*num - b*num
        if c in count:
            count[c] += num
        else:
            count[c] = num
        count[b] = 0
    answers.append(ans)
for a in answers:
    print(a)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

num_array = [0 for _ in range(10**5 + 1)]
result = 0

for num in a:
  num_array[num] += 1
  result += num
  
for array in bc:
  dif = array[1] - array[0]
  result += dif * num_array[array[0]]
  num_array[array[1]] += num_array[array[0]]
  num_array[array[0]] = 0
  print(result)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
k=10**5+1
C=[0]*k
for i in range(N):
    C[A[i]]+=1
s=0
for i in range(k):
    s+=i*C[i]
#print(s)
Q=int(input())
for i in range(Q):
    b,c=map(int,input().split())
    s+=(c-b)*C[b]
    C[c]+=C[b]
    C[b]=0
    print(s)

Submission status: Accepted

Solution code:
#                           जय श्री राम

import sys; import math; from collections import *
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, input().split())
def get_list(): return list(get_ints())
def printspx(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")

UGLYMOD = int(1e9+7); SEXYMOD = 998244353; MAXN = int(1e5)

# sys.stdin  = open("input.txt","r"); sys.stdout = open("output.txt","w")

# for _testcases_ in range(int(input())):
n =  int(input())
li = get_list()
d = Counter(li)
s = sum(li)
q = int(input())
for _ in range(q):
    b, c = get_ints()
    item = 0
    if b in d:
        item = d[b]
        del d[b]
    s -= item * b
    if c in d:
        d[c] += item
    else:
        d[c] = item
    s += item * c
    print(s)




'''
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
THE LOGIC AND APPROACH IS MINE @luctivud ( UDIT GUPTA )
Link may be copy-pasted here if it's taken from other source.
DO NOT PLAGIARISE.
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
'''

Submission status: Accepted

Solution code:
N = int(input())
lis = [0] * 10**6
A = list(map(int, input().split()))
for i in range(N):
    lis[A[i]] += 1
val = sum(A)
Q = int(input())
for i in range(Q):
    B,C = map(int, input().split())
    val -= lis[B] * B
    val += lis[B] * C
    lis[C] += lis[B]
    lis[B] = 0
    print(val)

Submission status: Accepted

Solution code:
n = int(input().strip())
a = list(map(int, input().split()))
q = int(input().strip())
bcs = [tuple(map(int, input().split())) for _ in range(q)]

dic = {}
for elem in a:
    if dic.get(elem):
        dic[elem] += 1
    else:
        dic[elem] = 1

ans = sum(a)

for b, c in bcs:
    if dic.get(b):
        diff = (c - b) * dic[b]
        if dic.get(c):
            dic[c] += dic[b]
        else:
            dic[c] = dic[b]
        dic[b] = 0
        ans += diff
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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


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


co = dict(Counter(a))

s = sum(a)

for b, c in bc:
    if co.get(b) == None:
        print(s)
    else:
        s = s + co[b] * (c - b)
        print(s)
        if co.get(c) == None:
            co[c] = co[b]
        else:
            co[c] += co[b]
        co[b] = 0

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from collections import Counter

n = int(input())
a = [int(item) for item in input().split()]
total = sum(a)
cnt = Counter(a)

q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if b in cnt:
        total -= cnt[b] * b
        total += cnt[b] * c
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = input().split()
np_a=np.array(a, dtype=int)

sum=np.int(sum(np_a))
count=np.zeros(100001, dtype='int64')
for i in range(0,n):
  count[np_a[i]]+= 1
  
q = int(input())
for i in range(0,q):
  b, c= map(int, input().split())
  sum=sum+(c-b)*count[b]
  count[c]=count[c]+count[b]
  count[b]=0
  
  print (sum)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
C = Counter(A)
ans = sum(A)

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

Submission status: Accepted

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

ans = sum(A)
d = {}
for a in A:
    try:
        d[a] += 1
    except:
        d[a] = 1

for b, c in BC:
    try:
        pre_c = d[b]
        d[b] = 0
    except:
        print(ans)
        continue

    ans += (c - b) * pre_c
    try:
        d[c] += pre_c
    except:
        d[c] = pre_c
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
suma = sum(A)
counter = Counter(A)
for i in range(Q):
  b,c = (int(x) for x in input().split())
  suma = counter[b]*(c-b) + suma
  print(suma)
  if b in counter:
    if c in counter:
      counter[c] += counter[b]
      counter[b] = 0
    else:
      counter[c] = counter[b]
      counter[b] = 0

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())
go=0
jisho={}
for x in a:
    go+=x
    if x in jisho:
        jisho[x]+=1
    else:
        jisho[x]=1
for x in range(q):
    b,c=map(int, input().split())
    if b in jisho:
        go=go+(c-b)*jisho[b]
        if c in jisho:
            jisho[c]+=jisho[b]
        else:
            jisho[c]=jisho[b]
        jisho[b]=0
    print(go)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

'''

'''

n = int(input())
arr = list(map(int, input().split()))
freq = [0] * (10**5 + 1)
cur_sum = 0
for v in arr:
    cur_sum += v
    freq[v] += 1

q = int(input())
for _ in range(q):
    b, c = map(int, input().split())

    if freq[b] > 0:
        cur_sum += (freq[b] * c - freq[b] * b)
        freq[c] += freq[b]
        freq[b] = 0
        print(cur_sum)
    else:
        print(cur_sum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    d = {}
    for i in a:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    s = sum(a)
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        if b in d:
            s = s + (c - b) * d[b]
            if c in d:
                d[c] += d[b]
            else:
                d[c] = d[b]
            d[b] = 0
        print(s)


main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
BC_list = [list(map(int, input().split())) for i in range(Q)]

count_dict = dict(Counter(A_list))
_sum = 0
for i, a in count_dict.items():
    _sum += i * a

for b, c in BC_list:
    if b in count_dict:
        count_b = count_dict[b]
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
        _sum -= b * count_b
        _sum += c * count_b
    print(_sum)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
Count = collections.Counter(A)
ans = sum(A)
for i in range(Q):
    B,C = map(int,input().split())
    n = Count[B]
    ans -= B*n
    ans += C*n
    Count[B] = 0
    if C in Count:
        Count[C] += n
    else:
        Count[C] = n
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(n):
    ai = a[i]
    if ai in dic:
        dic[ai] += 1
    else:
        dic[ai] = 1
s = sum(a)
q = int(input())
ans = []
for i in range(q):
    bi, ci = map(int, input().split())
    if bi in dic:
        num = dic[bi]
        s += (ci-bi)*num
        ans.append(s)
        if ci in dic:
            dic[ci] += num
        else:
            dic[ci] = num
        dic.pop(bi)
    else:
        ans.append(s)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = []
c = []
sa = []
for i in range(q):
  B,C = map(int,input().split())
  b.append(B)
  c.append(C)
  sa.append(C-B)
lst = [0]*100000
su = 0
for i in a:
  lst[i-1] += 1
  su += i

for i in range(q):
  su += sa[i]*lst[b[i]-1]
  print(su)
  if lst[b[i]-1] != 0:
    lst[c[i]-1] += lst[b[i]-1]
    lst[b[i]-1] = 0

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a=list(map(int,input().split()))
q = int(input())
bc=[list(map(int,input().split())) for _ in range(q)]

a_c = Counter(a)
a_sum = sum(a)
for i in range(q):
    if bc[i][0] in a_c:
        a_sum += a_c[bc[i][0]] * (bc[i][1] - bc[i][0])
        if bc[i][1] in a_c:
            a_c[bc[i][1]] += a_c[bc[i][0]]
        else:
            a_c[bc[i][1]] = a_c[bc[i][0]]
        a_c[bc[i][0]] = 0
    else:
        a_sum = a_sum
    print(a_sum)

Submission status: Accepted

Solution code:

# import random

import numpy as np
n = int(input())
a = list(map(int,input().split()))
a = np.array(a)
q = int(input())
# a = [random.randint(0, 10**4) for i in range(10**4)]
# 処理前の時刻
# t1 = time.time() 
import collections
co = collections.Counter(a)
s = 0
for k,v in co.items():
    s+=k*v
for i in range(q):
    b,c = map(int,input().split())
    if b in co.keys():
        s += (c-b)*co[b]
        co[c]+=co[b]
        co.pop(b)
    print(s)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
D = np.zeros((10**5)+1)
S = sum(A)
for i in range(len(A)):
    D[A[i]] += 1
Q = int(input())
for _ in range(Q):
    b, c = list(map(int, input().split()))
    S += D[b]*(c-b)
    D[c] += D[b]
    D[b] = 0
    print(int(S))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Val = [0]*(10**5+1)
for a in A:
    Val[a] += 1
Sum = sum(A)
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    n_B = Val[B]
    Val[B] = 0
    Val[C] += n_B
    Sum += (C-B) * n_B
    print(Sum)

Submission status: Accepted

Solution code:
N = int(input())
SUM = 0
map = [0] * 100001
for i in [int(i) for i in input().split()]:
  SUM += i
  map[i] += 1

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

Submission status: Accepted

Solution code:
from collections import Counter

R = lambda: map(int, input().split())
R()
d = Counter(R())
s = sum(d.elements())
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * d[b]
  print(s)
  d[c]+=d[b]
  del d[b]

Submission status: Accepted

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

ADic = {}

for A in AN:
    if A in ADic:
        ADic[A] += 1
    else:
        ADic[A] = 1

sum = 0
for key, value in ADic.items():
    sum += key * value

for BC in BCList:
    diff = 0
    B, C = map(int, BC.split())
    if B in ADic:
        diff = (C - B) * ADic[B]
        if C in ADic:
            ADic[C] += ADic[B]
            ADic[B] = 0
        else:
            ADic[C] = ADic[B]
            ADic[B] = 0
    sum += diff
    print(sum)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = {i: i for i in range(1, 10**5 + 1)}
total = sum(A)
from collections import Counter
counter = dict(Counter(A))
for i in range(1, 1+10**5):
    if i not in counter:
        counter[i] = 0
for i in range(Q):
    b, c = map(int, input().split())
    dic[b] = c
    total += counter[b] * (c - b)
    counter[c] += counter[b]
    counter[b] = 0
    print(total)

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    A = [0] * (10 ** 5 + 1)
    ans = 0
    for a in input().split():
        a = int(a)
        A[a] += 1
        ans += a

    Q = int(input())
    for _ in range(Q):
        a, b = map(int, input().split())
        l_a = A[a]
        ans -= l_a * a
        A[b] += l_a
        A[a] -= l_a
        ans += l_a * b
        print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    Bi, Ci = map(int, input().split())
    B.append(Bi)
    C.append(Ci)

counter = collections.Counter(A)
s = sum(A)

for i in range(Q):
    if B[i] not in counter:
        print(s)
        continue
    s += counter[B[i]] * (C[i] - B[i])
    counter[C[i]] += counter[B[i]]
    del counter[B[i]]
    print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
L = []
for i in range(Q):
    in1, in2 = list(map(int,input().split()))
    L.append([in1, in2])

dic = {}
m = 10 ** 5 + 10
for i in range(1, m + 1):
    dic[i] = 0

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

ans = sum(A)

for i in range(Q):
    ans = ans + (-L[i][0] + L[i][1]) * dic[L[i][0]]
    dic[L[i][1]] += dic[L[i][0]]
    dic[L[i][0]] = 0
    print(ans)

Submission status: Accepted

Solution code:
# abc171_d.py
import collections

N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)
sums = sum(A)
Q = int(input())
for i in range(Q):
    b ,c = map(int,input().split())
    sums += C[b]*c
    sums -= C[b]*b
    C[c] += C[b]
    C[b] = 0
    print(sums)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')

import numpy as np
from collections import Counter


def main():
    N = I()
    A = LI()
    Q = I()
    act_list = LLIN(Q)
    cnt = Counter(A)
    ans = sum(A)
    # print(cnt)
    for z in act_list:
        b_num = cnt[z[0]]
        cnt[z[0]] = 0
        cnt[z[1]] = cnt[z[1]] + b_num
        # print(cnt)
        # A_list = [z[1] if i == z[0] else i for i in A_list]
        # np.place(np_array, np_array == z[0], z[1])
        ans = ans - (z[0] * b_num) + (z[1] * b_num)
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
ans = 0
for k in d:
    ans += k * d[k]
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    if b in d:
        Bn = d[b]
        if c in d:
            d[c] += d[b]
            d[b] = 0
        else:
            d[c] = d[b]
            d[b] = 0
        ans += (c-b)*Bn
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
l = [0]*(10**5+10)
num = sum(A)
for i in A:
    l[i] = l[i] + 1


for i in range(q):
    b, c = map(int, input().split())
    num += (c-b)*l[b]
    l[c] += l[b]
    l[b] = 0
    print(num)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

import collections
counter = collections.defaultdict(int)
for key, value in collections.Counter(A).items():
    counter[key] = value

ans = sum(A)

for _ in range(Q):
    b, c = map(int, input().split())
    # print("b: {} c: {} counter: {}".format(b, c, counter))
    v = counter[b]
    counter[b] = 0
    counter[c] += v
    dif = v * (c - b)
    ans += dif
    # print("b: {} c: {} counter: {}".format(b, c, counter))
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
q=int(input())
ar=[0 for i in range (100000)]
tot=0
for i in range(n):
    ar[l[i]-1]+=1
    tot+=l[i] 
for j in range(q):
    a,b=map(int,input().split())
    val=0
    tot=tot-(a*ar[a-1])-(ar[b-1]*b)
    ar[b-1]+=ar[a-1]
    tot=tot+(ar[b-1]*b)
    ar[a-1]=0
    print(tot)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0] * Q
C = [0] * Q
dic = {}
for q in range(Q):
  B[q], C[q] = map(int, input().split())
for k in range(N):
  # import pdb;pdb.set_trace()
  if A[k] in dic:
    dic[A[k]] += 1
  else:
    dic[A[k]] = 1
sumA = sum(A)
for q in range(Q):
  if B[q] in dic:
    sumA += (C[q]-B[q]) * dic[B[q]]
  else:
    print(sumA)
    continue
  if C[q] in dic:
    dic[C[q]] += dic[B[q]]
  else:
    dic[C[q]] = dic[B[q]]
  dic[B[q]] = 0
  print(sumA)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n = gete(int)
    a = list(geta(int))

    d = defaultdict(int)
    s = sum(a)
    for i in a:
        d[i] += 1

    for _ in range(gete(int)):
        b, c = geta(int)

        if b in d:
            s += (c-b) * d[b]
            d[c] += d[b]
            d[b] = 0

        print(s)


if __name__ == "__main__":
    main()