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:
n = int(input())
num = list(map(int,input().strip().split()))
s = sum(num)
num_dict = {}
for i in num:
  if i in num_dict:
    num_dict[i] += 1
  else:
    num_dict[i] = 1
q = int(input())
for i in range(q):
  b,c = map(int,input().strip().split())
  if c in num_dict:
    if b in num_dict:
      k = num_dict[b]
      num_dict[c] += num_dict[b]
      del num_dict[b]
      if b > c:
        s = s - (b-c)*k
        print(s)
      else:
        s = s + (c-b)*k
        print(s)
    else:
      print(s)
  else:
    if b in num_dict:
      k = num_dict[b]
      num_dict[c] = num_dict[b]
      del num_dict[b]
      if b > c:
        s = s - (b-c)*k
        print(s)
      else:
        s = s + (c-b)*k
        print(s)
    else:
      print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ar = list(map(int, input().split()))

t = int(input())
c = Counter(ar)
s = sum(ar)
for i in range(t):
    a, b = list(map(int, input().split()))
    if b in c:
        c[b] += c[a]
    else:
        c[b] = c[a]
    s += ((b-a)*c[a])
    print(s)
    c[a] = 0

Submission status: Accepted

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

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []

A_collections = collections.Counter(A)
sum_A = sum(A)

for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    b, c = B[i], C[i]
    if b in A_collections.keys():
        A_collections[c] += A_collections[b]
        sum_A += (c - b) * A_collections[b]
        A_collections[b] = 0
    
    print(sum_A)

Submission status: Accepted

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

nums = [0 for i in range(100000)]
for a in A :
  nums[a-1] += 1

answer = sum(A)
  
for q in range(Q) :
  B, C = list(map(int, input().split()))
  nums[C-1] += nums[B-1]
  answer = answer + C * nums[B-1] - B * nums[B-1]
  nums[B-1] = 0
  print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
d = Counter(A)
Q = int(input())
ans = sum(A)
for i in range(Q):
    b, c = map(int, input().split())
    ans += (c-b) * d[b]
    d[c] += d[b]
    d[b] = 0
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

tot = sum(A)
cnt = Counter(A)

for _ in range(Q):
    b,a = map(int,input().split())
    tot += (a - b) * cnt[b]
    cnt[a] += cnt[b]
    cnt[b] = 0
    
    print(tot)

Submission status: Accepted

Solution code:

from collections import Counter

def submit():
    n = int(input())
    a = list(map(int, input().split()))
    count = Counter(a)
    s = sum(a)

    # クエリ処理
    q = int(input())
    ans_q = []
    for _ in range(q):
        b, c = map(int, input().split())
        diff = c - b
        s += diff * count[b]
        ans_q.append(s)
        count[c] += count[b]
        count[b] = 0

    for ans in ans_q:
        print(ans)


submit()

Submission status: Accepted

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

Sum = 0
cnt = Max*[0]
for i in range(N):
	Sum += A[i]
	cnt[A[i]] += 1

ans = []
tmp = Sum
Q = int(input())
for i in range(Q):
	B, C = map(int, input().split())
	tmp += (C-B)*cnt[B]
	ans.append(tmp)
	cnt[C] += cnt[B]
	cnt[B] = 0

for i in range(Q):
	print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

N = int(readline())
A = list(map(int,readline().split()))
from collections import Counter
Ac = Counter(A)
s = sum(A)
Q = int(readline())
for i in range(Q):
    b,c = map(int,readline().split())
    s += (c-b)*Ac[b]
    Ac[c] += Ac[b]
    Ac[b] = 0
    print(s)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
 
ans = sum(A)
d = Counter(A) #Counterで要素をまとめる
 
for i in range(Q):
    b, c = map(int,input().split())
    tmp = d[b] #bの総数を記憶
    d[b] = 0 #bの総数を0にする
    d[c] += tmp #cの総数にbの総数を足す
    ans += (c-b)*tmp #合計の補正
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
 
N = int(input())
a = Counter(map(int, input().split()))
Q = int(input())
 
sum_a = sum(a.elements())
 
for i in range(Q):
    b, c = map(int, input().split())
    a[c] += a[b]
    sum_a += (c-b) * a[b]
    a[b] = 0
    print(sum_a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
numlista=list(map(int,input().split()))
import collections
numkosuu=collections.Counter(numlista)
suma=0
for i in numlista:
  suma+=i
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
#変化したところのみ計算する
  suma+=(c-b)*numkosuu[b]
#個数を更新する
  numkosuu[c]+=numkosuu[b]
  numkosuu[b]=0
  print(suma)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())

ans = sum(A)
dic = Counter(A)
for i in range(q):
    b, c = map(int, input().split())
    cnt = dic[b]
    dic[b] = 0
    dic[c] += cnt
    ans -= cnt*b
    ans += cnt*c
    print(ans)

Submission status: Accepted

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

n, s, q = int(input()), list(map(int, input().split())), int(input())
sm, f = sum(s), defaultdict(int)
for i in s: f[i] += 1
for _ in range(q):
    b, c = map(int, input().split())
    sm += (c - b) * f[b]
    f[c] += f.pop(b)
    print(sm)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())

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

ans = sum(A)
d = Counter(A)

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())

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

ans = sum(A)
for a in A:
    d[a] += 1

for i in range(Q):
    b, c = map(int,input().split())
    tmp = d[b] #変動する個数を数える
    d[b] = 0
    d[c] += tmp
    ans += (c-b)*tmp 
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())

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

ans = sum(A)
for a in A:
    d[a] += 1

for i in range(Q):
    b, c = map(int,input().split())
    tmp = d[b] #変動する個数を数える
    d[b] = 0
    d[c] += tmp
    if b >= c:
        ans -= (b-c)*tmp
    else:
        ans += (c-b)*tmp
    print(ans)

Submission status: Accepted

Solution code:
import collections as co
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
c = co.Counter(a)
ans = sum(a)
for i in range(q):
    ans += (c[bc[i][0]] * (bc[i][1]-bc[i][0]))
    print(ans)
    c[bc[i][1]] += c[bc[i][0]]
    c[bc[i][0]] = 0

Submission status: Accepted

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 = [list(map(int,input().split())) for i in range(q)]

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

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

Submission status: Accepted

Solution code:
# collect input N integers
# replace every element whose value is Bi with Ci
# get input x and y and compute them to display output 
from collections import Counter
n=input()
a=list(map(int,input().split()))
b=Counter(a)
n=int(input())
ans=sum(a)
for i in range(n):
    x,y=map(int,input().split())
    ans+=b[x]*(y-x)
    print(ans)
    b[y]+=b[x]
    b[x]=0

Submission status: Accepted

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

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))


N = int(input())
A = LI()
Q = int(input())


move_dic = {}
revers_move_dic = {}
for i in range(1,10**5):
    revers_move_dic[i] = -1

BC = [() for i in range(Q)]
for q in range(Q):
    BC[q] = LI()


anss = []
A_collections = collections.Counter(A)

# print(A_collections)
ans = sum(A)
for q in range(Q):
    b,c = BC[q]

    if b in A_collections.keys():
        A_collections[c] += A_collections[b]
        ans += (c - b) * A_collections[b]
        A_collections[b] = 0
    print(ans)

Submission status: Accepted

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

from collections import Counter
arr = Counter(A)
ans = sum([i*arr[i] for i in arr])
 
for i in range(q):
    b,c = list(map(int,input().split()))
    ans += (c-b)*arr[b]
    arr[c],arr[b] = arr[c]+arr[b],0
    print(ans)

Submission status: Accepted

Solution code:

n=int(input())

alist=[0]*(10**5+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: Accepted

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+1)
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: Accepted

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

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

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

Alist = list(map(int, input().split()))
counter = Counter(Alist)

Q = int(input())
Clist = []
cnt_list = []

for _ in range(Q):
    b, c = map(int, input().split())
    Clist.append(c-b)
    num = counter[b]
    cnt_list.append(num)
    counter[b] = 0
    counter[c] += num

base = sum(Alist)

for (diff, cnt) in zip(Clist, cnt_list):
    ans = base + diff * cnt
    print(ans)
    base = ans

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
q = int(input())
from collections import Counter
arr = Counter(arr)
ans = sum([i*arr[i] for i in arr])

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

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
s = sum(lst)
q = int(input())
dic = {}
for i in range(n):
  dic.setdefault(lst[i], 0)
  dic[lst[i]] += 1
for i in range(q):
  b, c = map(int, input().split())
  dic.setdefault(b, 0)
  dic.setdefault(c, 0)
  dic[c] += dic[b]
  s += ((c - b) * dic[b])
  print(s)
  dic[b] = 0

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
for _ in range(q):
    before, after = map(int, input().split())
    sum_res -= before * counter[before]#合計を減らす
    sum_res += after * counter[before]#合計を増やす
    counter[after] += counter[before]
    counter[before] = 0
    print(sum_res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
for _ in range(q):
    before, after = map(int, input().split())
    sum_res -= before * counter[before]
    sum_res += after * counter[before]
    counter[after] += counter[before]
    counter[before] = 0
    print(sum_res)

Submission status: Accepted

Solution code:
import bisect
import sys
input=sys.stdin.readline
n=int(input())
lst=sorted(map(int,input().split()))
s=sum(lst)
sum_lst=[0]
for i in range(1,10**5+1):
    sum_lst.append(bisect.bisect_right(lst,i)-bisect.bisect_left(lst,i))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    s+=((c-b)*sum_lst[b])
    print(s)
    sum_lst[c]+=sum_lst[b]
    sum_lst[b]=0

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int,sys.stdin.readline().split()))
q = int(input())
s = sum(a)
cnt = [0]*(10**5+5)
for c in a:
  cnt[c] +=1
for i in range(q):
  b,c = map(int,sys.stdin.readline().split())
  bdif = c-b
  s += cnt[b]*bdif
  cnt[c] += cnt[b]
  cnt[b] = 0
  print(s)

Submission status: Accepted

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] * 100100
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: Accepted

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())
    
num = [0]*100000
for i in range(len(A)):
    num[A[i]-1] += 1
    
S =[sum(A)]
for i in range(Q):
    S.append(S[i]+(C[i]-B[i])*num[B[i]-1])
    num[C[i]-1] += num[B[i]-1]
    num[B[i]-1] = 0
 
S.pop(0)    
for i in range(Q):
    print(S[i])

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * (10**5 + 1)
for a in map(int, input().split()):
    A[a] += 1
Q = int(input())
acc = sum(i * a for i, a in enumerate(A))
for _ in range(Q):
    b, c = map(int, input().split())
    k = A[b]
    A[b] = 0
    A[c] += k
    acc += c * k - b * k
    print(acc)

Submission status: Accepted

Solution code:
import collections

n = int(input())
l_a = list(map(int, input().split()))
col = collections.Counter(l_a)
bef = sum(l_a)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*col[b]
    ans.append(bef)
    col[c] += col[b]
    col[b] = 0

for value in ans :
    print(value)

Submission status: Accepted

Solution code:
n = int(input())
l_a = list(map(int, input().split()))
many = [0]*101000
for i in range(0, len(l_a)) :
    many[l_a[i]-1]+=1
bef = sum(l_a)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*many[b - 1]
    ans.append(bef)
    many[c - 1] += many[b - 1]
    many[b - 1] = 0
 
for value in ans :
    print(value)

Submission status: Accepted

Solution code:
import collections

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

cnt_list = collections.Counter(A)
total = sum(A)
for i in range(Q):
    cnt_list[BC[i][1]] += cnt_list[BC[i][0]]
    total += (BC[i][1] - BC[i][0]) * cnt_list[BC[i][0]]
    cnt_list[BC[i][0]] = 0
    print(total)

Submission status: Accepted

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

d = {}
s = 0
for i in a:
  if d.get(i) is None:
    d[i] = 0
  d[i] += 1
  s += i

query = int(input())

for q in range(query):
  b, c = map(int, input().split())
  if d.get(b, 0) > 0:
    t = d[b]
    d[b] = 0
    if d.get(c) is not None:
      d[c] += t
    else:
      d[c] = t
    s += (c-b) * t
  print(s)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

sum = sum(arr)

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


    if dd[b]>0:
        sum += dd[b]*(c-b)
        dd[c]+=dd[b]
        dd[b]=0

    print(sum)

Submission status: Accepted

Solution code:
def ad(a,bank):
	if a not in bank:
		bank[a] = 1
	else:
		bank[a] += 1

def rp(a,b,bank):
	ans = 0
	if a in bank:
		t = bank[a]
		ans = t
		bank[a] = 0
		if b not in bank:
			bank[b] = t
		else:
			bank[b] += t
		
	return ans
# your code goes here
n = int(input())
arr = list(map(int, input().split()))

bank = dict()
su = 0

for a in arr:
	ad(a,bank)
	su += a

q = int(input())
for i in range(q):
	b,c = map(int, input().split())
	t = rp(b,c,bank)
	if b in bank:
		su = su - (t*b) + (t*c)
	
	print(su)

Submission status: Accepted

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())
    if b not in dic:
        print(ans)
        continue
    else:
        pass
    if c not in dic:
        ans+=(c-b)*dic[b]
        print(ans)
        dic[c]=dic[b]
        dic[b]=0
        continue
    else:
        pass
    ans+=(c-b)*dic[b]
    dic[c]+=dic[b]
    dic[b]=0
    print(ans)

Submission status: Accepted

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(a)
for i in range(q):
  d[c[i]]+=d[b[i]]
  ans+=(c[i]-b[i])*d[b[i]]
  d[b[i]]=0
  print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

for _ 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:
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)]

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

total = sum(a)

for j in range(q):
    b = bc[j][0]
    c = bc[j][1]
    cnt[c] += cnt[b]
    total += (c-b)*cnt[b]
    cnt[b] = 0
    print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
ans = sum(A)
count_A = collections.defaultdict(int)
for a in A:
    count_A[a] += 1
for b,c in BC:
    if b in count_A:
        ans += (c-b)*count_A[b]
        count_A[c] += count_A[b]
        count_A[b] = 0
    print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
def main():
    n=int(input())
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(input())
    bc=[]
    for _ in range(q):
        b,c=map(int,input().split())
        bc.append((b,c))
    s = sum(k*v for k,v in d.items())
    for b,c in bc:
        if b in d:
            s += (c-b)*d[b]
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print(s)
main()

Submission status: Accepted

Solution code:
import collections

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

clist = collections.Counter(l)

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

Submission status: Accepted

Solution code:
from collections import Counter
a=int(input())
l=list(map(int,input().split()))
b=int(input())
s=Counter(l)
initial_sum=sum(l)

for _ in range(b):
  x,y=map(int,input().split())
  k=s[x]
  s[x]=0
  s[y]+=k
  initial_sum+=(y-x)*k
  print(initial_sum)

Submission status: Accepted

Solution code:
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

# Problem A
# alpha = input()
# if alpha.isupper():
#     print("A")
# else:
#     print("a")

# Problem B
# input_scan()
# [n, k] = [int(x) for x in next_line().split(" ")]
# fruits = sorted([int(x) for x in next_line().split(" ")])
# print(sum(fruits[:k]))

# Problem C
# num = int(input())

# dictionary = "abcdefghijklmnopqrstuvwxyz"

# def convert_to_alpha(num):
#     if num == 0:
#         return ""
#     quot = num // 26
#     rem = num % 26
#     if rem == 0:
#         return "z" + convert_to_alpha(quot - 1)
#     else:
#         return dictionary[rem - 1] + convert_to_alpha(quot)

# name = ""
# print("".join(reversed(convert_to_alpha(num))))

# Problem D
input_scan()

next_line()
from collections import Counter
nums = [int(x) for x in next_line().split(" ")]
total_sum = sum(nums)
nums = Counter(nums)


q = int(next_line())
for i in range(q):
    [b, c] = [int(x) for x in next_line().split(" ")]
    # print("Dict before replacing {} with {} = {}".format(b, c, str(nums)))
    diff = c - b
    total_sum += nums[b] * diff
    occ = nums[b]
    if c in nums:
        nums[c] += occ
    else:
        nums[c] = occ
    nums[b] = 0

    # print("Dict before replacing {} with {} = {}".format(b, c, str(nums)))
    append_output(str(total_sum))

print_output()

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) ]

from collections import Counter, defaultdict

ca = Counter(a)

suma = sum(a)
diff = 0
d = defaultdict(int)

for x in bc:
  d[x[0]] = d[x[1]]
  suma += ca[x[0]] * (x[1] - x[0])
  ca[x[1]] += ca[x[0]]
  ca[x[0]] = 0
  print(suma)

Submission status: Accepted

Solution code:
from collections import defaultdict
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
SA=sum(A)
Q=II()
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
cnt=defaultdict(int)
for a in A:
  cnt[a]+=1
for b,c in queries:
  if b in cnt:
    SA-=cnt[b]*b
    SA+=cnt[b]*c
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(SA)

Submission status: Accepted

Solution code:
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
SA=sum(A)
Q=II()
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
cnt={}
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
for b,c in queries:
  if b in cnt:
    SA-=cnt[b]*b
    SA+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(SA)

Submission status: Accepted

Solution code:
from sys import stdin,stdout
LI=lambda:list(map(int,input().split()))
MAP=lambda:map(int,input().split())
IN=lambda:int(input())
S=lambda:input()
import math
from collections import Counter,defaultdict


n=IN()
a=LI()
q=IN()
d=defaultdict(int)
for i in range(n):
    d[a[i]]+=1
s=sum(a)
for i in range(q):
    x,y=MAP()
    s+=(y-x)*d[x]
    d[y]+=d[x]
    d[x]=0
    print(s)

Submission status: Accepted

Solution code:
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
Sum=sum(A)
Q=II()
cnt={}
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
#print(cnt)   
for b,c in queries:
  if b in cnt:
    Sum-=cnt[b]*b
    Sum+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(Sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
L = [0]*100001
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: 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)]

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

for b, c in BC:
    b_num = a_state.get(b, 0)
    c_num = a_state.get(c, 0)
    res = res - b_num*b + b_num*c
    a_state[c] = c_num + b_num
    a_state[b] = 0

    print(res)

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)]

from collections import Counter
counter=Counter(a)
s=sum(a)
for b,c in bc:
    if b in counter:
        sa=counter[b]*(c-b)
        counter[c]+=counter[b]
        counter.pop(b)
        s+=sa
    print(s)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]]+=1
  else:
    dic[a[i]]=1

ans=sum(a)
for i in range(q):
  a,b=map(int,input().split())
  if a in dic:
    ans=ans-a*dic[a]+b*dic[a]
    if b in dic:
      keep=dic[a]
      dic[b]+=keep
      dic[a]=0
    else:
      keep=dic[a]
      dic[b]=keep
      dic[a]=0
  print(ans)

Submission status: Accepted

Solution code:
#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin
from collections import defaultdict

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    counter = defaultdict(int)
    for ai in a:
        counter[ai] += 1
    asum = sum(a)

    q = ni()
    for i in range(q):
        b, c = na()
        asum += counter[b] * (c-b)
        counter[c] += counter[b]
        counter[b] = 0
        print(asum)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * (100001)
ans = sum(A)
for a in A:
    cnt[a] += 1
Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    ans += (c - b) * cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(ans)

Submission status: Accepted

Solution code:
#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    asum = 0
    for i, num in enumerate(alist):
            asum += i*num

    q = ni()
    for i in range(q):
        b, c = na()
        asum += alist[b] * (c-b)
        alist[c] += alist[b]
        alist[b] = 0
        print(asum)

if __name__ == "__main__":
    main()

Submission status: Accepted

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(100001)]
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)