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())
A = list(map(int, input().split()))
Q = int(input())
num_dic = {}
for i in A:
    if i not in num_dic:
        num_dic[i] = 1
    else:
        num_dic[i] += 1
ans = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    if not b in num_dic:
        print(ans)
        continue
    else:
        if c in num_dic:
            ans -= num_dic[b] * b
            ans += num_dic[b] * c
            num_dic[c] += num_dic[b]
            num_dic[b] = 0
        else:
            ans -= num_dic[b] * b
            ans += num_dic[b] * c
            num_dic[c] = num_dic[b]
            num_dic[b] = 0
        print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ai = [int(i) for i in input().split()]
q = int(input())
bc = [[int(i) for i in input().split()] for j in range(q)]

kosu = [0]*(10**5+1)
for i in range(n):
    kosu[ai[i]] += 1
    
num = [0]*(10**5+1)
tmp = sum(ai)

for i in range(q):
    tmp += kosu[bc[i][0]] * (bc[i][1]-bc[i][0])
    kosu[bc[i][1]] += kosu[bc[i][0]]
    kosu[bc[i][0]] = 0
    print(tmp)

Submission status: Accepted

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

T = [0]*200000
#T[i] = A中の数値iの数

for i in A:
  T[i]+=1
S = sum(A)
for i in BC:
  b,c = i
  n = int(T[b])
  sa = c*n - b*n
  S = S + c*n - b*n
  print(S)
  T[b]=0
  T[c]+=n
  #print(S, [b,T[b], 0],[c,T[c],T[c]+T[b]], sa, S+sa)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from collections import Counter
def resolve():
    N,=pin()
    An=lispin()
    count_An=dict(Counter(An))
    S=sum(An)
    Q,=pin()
    erachan=0
    for i in range(Q):
        a,b=pin()
        temp=0
        try:
            temp=count_An[a]
            count_An[a]=0
            count_An.setdefault(b,0)
            count_An[b]+=temp
        except KeyError:erachan+=1
        S=S+(b-a)*temp
        print(S)
        
#%%submit!
resolve()

Submission status: Accepted

Solution code:
from collections import Counter 

n = int(input())
a = list(map(int, input().split()))
co = Counter(a)
q = int(input())
ans = 0
for i in co.items():
    ans += i[0] * i[1]
for _ in range(q):
    b, c = map(int, input().split())
    if b in co.keys():
        ans -= b * co[b]
        co[c] += co[b]
        ans += c * co[b]
        co[b] = 0
    print(ans)

Submission status: Accepted

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
INF = 1000000000000

import bisect

n = I()
a = LI()
q = I()

cnt = [0]*1000000
for i in a:
    cnt[i] += 1
sm = sum(a)

for i in range(q):
    b,c = LI()
    sm += (c-b)*cnt[b]
    if(c != b):
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(sm)

Submission status: Accepted

Solution code:
#coding:utf-8

N = int(input())

A = [int(y) for y in input().split()]

lif = [0] * (10 ** 5 + 1)
sasa = 0

for y in A:
    lif[y] += 1
    sasa += y

Q = int(input())

k = 0

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

    k = lif[b]

    lif[c] += k
    lif[b] = 0
    sasa += (k * (c - b))

    print("{}".format(sasa))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [ int(s) for s in input().split() ]

data = {}

total = 0
for i in range(N):
  total += A[i]
  if A[i] in data:
    data[A[i]] += 1
  else:
    data[A[i]] = 1

Q = int(input())
for i in range(Q):
  B, Q = map(int, input().split())
  if B in data:
    num = data[B]
    data[B] = 0
    if Q in data:
      data[Q] += num
    else:
      data[Q] = num
    total = total - B * num + Q * num
  print(total)

Submission status: Accepted

Solution code:
import math

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

def ii():
    return int(input())

def main():
    N = ii()
    A = list(mi())
    Q = ii()
    BC = [map(int, input().split()) for _ in range(Q)]
    B, C = [list(i) for i in zip(*BC)]
    ans = sum(A)

    dic = {i+1: 0 for i in range(10**5)}
    for v in A:
        dic[v] += 1
    for i in range(Q):
        k = dic[B[i]]
        ans += k*(C[i]-B[i])
        dic[B[i]] -= k
        dic[C[i]] += k

        print(ans)
    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as cl
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

res = sum(A)
c = cl.Counter(A)
for i in range(Q):
  BC = [int(j) for j in input().split()]
  B = BC[0]
  C = BC[1]
  if B in c:
    res += (C-B)*c[B]
    if C in c:
      c[C] += c[B]
    else:
      c[C] = c[B]
    del(c[B])
  print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
Q = int(input())
BC = []
for _ in range(Q):
    b,c = input().split()
    BC.append([b,c])
dic = {}
for i in range(N):
    if A[i] in dic:
        dic[A[i]] += 1
    else: dic[A[i]] = 1
S = sum([int(a) for a in A])
for i in range(Q):
    b,c = BC[i]
    if b in dic:
        num = dic[b]
        S += num*(int(c)-int(b))
        print(S)
        if c in dic:
            dic[c] += num
            dic.pop(b)
        else:
            dic[c] = num
            dic.pop(b)
    else: print(S)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = {i:0 for i in range(10**5+1)}
for a in A:
    num[a] += 1
ans = sum(A)
for _ in range(Q):
    B, C = map(int, input().split())
    num_B = num[B]
    num_C = num[C]
    num[C] += num_B
    num[B] = 0
    #print(B, num_B)
    #print(C, num_C)
    ans += -num_B*B + (num[C]-num_C)*C
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
def solve():
    N = int(input())
    A = np.array(list(map(int, input().split())),dtype=np.int64)
    Q = int(input())
    BC = np.array([list(map(int, input().split())) for _ in range(Q)])

    nums = np.zeros(max(A.max(), BC.max())+1)
    sm = np.sum(A, dtype=np.int64)
    for i in A:
        nums[i]+=1

    for b,c in BC:
        sm += nums[b]*(c-b)
        nums[c] +=nums[b]
        nums[b] = 0
        print(int(sm))








if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
coll=collections.Counter(a)
q=int(input())
suma=sum(a)
lis=[0]*(10**5+1)
for i in a:
    lis[i-1]=coll[i] #lisの「番め」の数字が、aに何個あるか

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

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    db=[0]*(10**5+10)

    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))

    sm=0
    for a in arr:
        db[a]+=1
        sm+=a

    q=int(readline())

    for i in range(q):
        b,c=map(int, readline().rstrip().split())
        sm-=b*db[b]
        sm+=c*db[b]
        db[c]+=db[b]
        db[b]=0
        print(sm)
    
    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

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

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

ls = [0] * 10 ** 6

for a in A:
    ls[a] += 1

s = sum(A)

for _ in range(Q):
    B, C = map(int, input().split())
    s += ls[B] * (C - B)
    ls[C] += ls[B]
    ls[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())

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

counter = Counter(A)

sums = 0
for key, value in counter.items():
    sums += key * value

for t in chlist:
    if t[0] in counter.keys():
        t0s = counter[t[0]]

        if t[1] in counter.keys():
            counter[t[1]] += counter[t[0]]
        else:
            counter[t[1]] = counter[t[0]]

        counter.pop(t[0])
        sums += (t[1] - t[0]) * t0s

    print(sums)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    import sys
    def input():
        return sys.stdin.readline()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    C = Counter(A)
    s = set(C.keys())
    ans = sum(A)
    for _ in range(q):
        b, c = list(map(int, input().split()))
        if b in s:
            d = C.pop(b)
            ans = ans + (c - b) * d
            C[c] += d
            s.remove(b)
            s.add(c)
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for a in A:
  ans += a
  if a in memo:
    memo[a] = memo[a] + 1
  else:
    memo[a] = 1
    
Q = int(input())

for _ in range(Q):
  B, C = map(int, input().split())
  
  if B in memo and memo[B] != 0:
    n = memo[B]
    memo[B] = 0
    ans -= B * n
    if C in memo:
      memo[C] += n
    else:
      memo[C] = n
    ans += C * n
    
    print(ans)
  else:
    print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

m = max(max(A), max(B), max(C))+1
res = np.arange(m,dtype=int)
newA = np.zeros((m),dtype=int)


for a in A:
    newA[a] += 1

sum_r = np.dot(res,newA)


for i in range(Q):
    b = B[i]
    c = C[i]
    sum_r = sum_r - b*newA[b] + c*newA[b]
    print(sum_r)
    newA[c] += newA[b]
    newA[b] = 0

Submission status: Accepted

Solution code:
n = int(input())
nums = [0] * 100001
A = [int(i) for i in input().split()]
for a in A:
    nums[a] += 1
q = int(input())
B = [None] * q
C = [None] * q
for i in range(q):
    B[i], C[i] = (int(i) for i in input().split())

ans = 0
for i in range(100001):
    ans += i * nums[i]
      
for i in range(q):
    if nums[B[i]] == 0:
        print(ans)
        continue
    t = nums[B[i]]
    ans += (C[i]-B[i])*t
    nums[B[i]] = 0
    nums[C[i]] += t
    print(ans)

Submission status: Accepted

Solution code:
N = int(input().rstrip())
As = {}
answer = 0
for a in [int(x) for x in input().rstrip().split()]:
    answer += a
    if a in As.keys():
        As[a] += 1
    else:
        As[a] = 1
# print(As)
Q = int(input().rstrip())
d = {}
for i in range(Q):
    b, c = map(int, input().rstrip().split())
    d[i] = {'b': b, 'c': c}
# print(d)
for i in sorted(d.keys()):
    if d[i]['b'] in As.keys():
        diff = d[i]['c'] - d[i]['b']
        diff_num = As.pop(d[i]['b'])
        if d[i]['c'] in As.keys():
            As[d[i]['c']] += diff_num
        else:
            As[d[i]['c']] = diff_num
        answer += (diff * diff_num)
    print(answer)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(x) for x in input().split()]

count = {}
for x in arr:
    if x in count:
        count[x] += 1
    else:
        count[x] = 1

s = sum(arr)

q = int(input())
for i in range(q):
    b, c = [int(x) for x in input().split()]

    if b in count and count[b] > 0:
        s -= b*count[b]
        s += c*count[b]

        if c in count:
            count[c] += count[b]
        else:
            count[c] = count[b]

        count[b] = 0

    print(s)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
ans = 0
for j in dic:
    ans += dic[j]*j

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

Submission status: Accepted

Solution code:
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())))

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

for i in range(q):
    deg = bc[i][1] - bc[i][0]
    s += deg*count[bc[i][0]]
    count[bc[i][1]] += count[bc[i][0]]
    count[bc[i][0]] = 0
    print(s)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
al = list(map(int, input().split()))
ans = sum(al)
d = defaultdict(lambda: 0)
for a in al:
    d[a] += 1
q = int(input())
for _ in range(q):
    b, c = list(map(int, input().split()))
    d[c] += d[b]
    ans += (c-b)*d[b]
    d[b] = 0
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(str,input().split()))
s=0
box=[]
dp={}
for x in a:
    s+=int(x)
    if x not in dp.keys():
        dp[x]=1
    else:
        dp[x]=dp[x]+1
q=int(input())
for i in range(q):
    b,c=map(str,input().split())
    if b not in dp.keys():
        box.append(s)
        continue
    s+=int(dp[b])*(int(c)-int(b))
    if c not in dp.keys():
        dp[c]=dp[b]
    else:
        dp[c]+=dp[b]
    del dp[b]
    box.append(s)
for x in box:
    print(x)

Submission status: Accepted

Solution code:
import numpy

tmp=input()
list=input().split(" ")
list=[int(x) for x in list]
array=numpy.array(list,dtype=">i4")
sum=numpy.sum(array)
all=numpy.zeros(100001,dtype=">i4")
for i in array:
  all[i]+=1
#for i in range(len(list)):
#  array[i]=int(list[i])

a=int(input())

for i in range(a):
  b=input().split(" ")
  b0=int(b[0])
  b1=int(b[1])
  diff=all[b0]*(b1-b0)
  sum+=diff
  all[b1]+=all[b0]
  all[b0]=0
  print(sum)

Submission status: Accepted

Solution code:
data = {}
total = 0
N = int(input())

for a in input().split():
    a = int(a)
    data[a] = data.get(a, 0) + 1
    total += a

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    diff = c - b
    count = data.get(b)
    if count:
        data[c] = data.get(c, 0) + data.pop(b)
        total += diff * count
    print(total)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = [int(v) for v in input().split()]
Q = int(input())

dic = defaultdict(int)

ans = 0
for a in A:
    ans += a
    dic[a] += 1

for _ in range(Q):
    b,c = [int(v) for v in input().split()]
    if b in dic:
        value = dic.pop(b)
        ans += c * value
        ans -= value * b
        dic[c] = dic[c] + value
    print(ans)

Submission status: Accepted

Solution code:
import sys
import math as mt
input=sys.stdin.buffer.readline  
#t=int(input())
t=1
for __ in range(t):
    #n,k=map(int,input().split())
    n=int(input())
    l=list(map(int,input().split()))
    suma=sum(l[:])
    q=int(input())
    d=[0]*(100001)
    for i in l:
        d[i]+=1
    for ______ in range(q):
        b,c=map(int,input().split())
        suma-=(d[b]*b)
        suma+=(d[b]*c)
        d[c]+=d[b]
        d[b]=0
        print(suma)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
s = 0
for a in A:
    d[a] += 1
    s += a

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

for i in range(Q):
    b = B[i]
    c = C[i]
    now_b = d[b]
    d[c] += now_b
    d[b] = 0
    s += (c - b) * now_b
    print(s)

Submission status: Accepted

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


res = {}
for a_i in a:
    if not a_i in res:
        res[a_i] = 0
    res[a_i] += 1

ans = 0
for key, val in res.items():
    ans += key * val

for b_i, c_i in zip(b, c):
    num = res[b_i] if b_i in res else 0
    res[b_i] = 0
    if not c_i in res:
        res[c_i] = 0
    res[c_i] += num

    ans += (c_i - b_i) * num

    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    A_max = pow(10, 5)+1
    N = int(input())
    A_lsit = list(map(int, input().split()))
    Q = int(input())
    B_list = [0]*Q
    C_list = [0]*Q
    for q in range(Q):
        B, C = map(int, input().split())
        B_list[q] = B
        C_list[q] = C
    
    bucket_list = [0]*A_max
    for A in A_lsit:
        bucket_list[A] += 1

    S = 0
    for i in range(A_max):
        S += i*bucket_list[i]
    for q in range(Q):
        B = B_list[q]
        C = C_list[q]
        S += bucket_list[B] * (C-B)
        print(S)
        bucket_list[C] += bucket_list[B]
        bucket_list[B] = 0
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
sm = sum(a)
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if cnt.get(b) != None:
        num = cnt[b]
        del cnt[b]
        sm += num * (c-b)
        if cnt.get(c) != None:
            cnt[c] += num
        else:
            cnt[c] = num
    print(sm)

Submission status: Accepted

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

from collections import*
C=Counter(A)
ans=sum(A)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
  b_temp, c_temp = map(int,input().split())
  B.append(b_temp)
  C.append(c_temp)
  
sum = 0
n_dic = {}
  
for i in range(N):
  sum += A[i]
  if A[i] not in n_dic.keys():
    n_dic[A[i]] = 1
  else:
  	n_dic[A[i]] += 1
  
for i in range(Q):
  #print(n_dic)
  #print(B[i], C[i])
  if B[i] not in n_dic.keys():
    print(sum)
  else:
    #print(B[i] * n_dic[B[i]], C[i] * n_dic[B[i]])
    sum = sum - (B[i] * n_dic[B[i]]) + (C[i] * n_dic[B[i]])
    print(sum)
    if C[i] not in n_dic.keys():
      n_dic[C[i]] = n_dic[B[i]]
      n_dic[B[i]] = 0
    else:
      n_dic[C[i]] += n_dic[B[i]]
      n_dic[B[i]] = 0

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
import collections

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

    ans = sum(a)
    a = collections.Counter(a)
    for b, c in bc:
        if b not in a:
            print(ans)
            continue
        b_num = a[b]
        ans -= b_num * b
        ans += b_num * c
        if c in a:
            a[c] += b_num
        else:
            a[c] = b_num
        a[b] = 0
        print(ans)

    return


if __name__ == "__main__":
    main()
    sys.exit(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import numpy as np

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0 for _ in range(Q)]
C = [0 for _ in range(Q)]
for i in range(Q):
  B[i], C[i] = map(int, input().split())
  
a = collections.Counter(A)
S = np.sum(A)
for i in range(Q):
  S += (C[i]-B[i]) * a[B[i]]
  a[C[i]] += a[B[i]]
  a[B[i]] = 0

  print(S)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    q = int(input())
    bc_lst = []
    for i in range(q):
        bc_lst.append(list(map(int, input().split())))
    cnt_lst = [0 for _ in range(100001)]
    s = 0

    for a in a_lst:
        cnt_lst[a] += 1
        s += a

    for bc in bc_lst:
        b, c = bc
        b_cnt = cnt_lst[b]
        s += b_cnt * (c - b)
        cnt_lst[b] = 0
        cnt_lst[c] += b_cnt
        print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
import collections

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return np.array(x)

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

c = collections.Counter(A)

ans = sum(A)

for i in range(0,Q):
    Bi = BC[i,0]
    Ci = BC[i,1]
    ans = ans-c[Bi]*Bi + c[Bi]*Ci
    print(ans)
    if Ci in c:
        c[Ci] = c[Ci] + c[Bi]
        del c[Bi]
    else:
        c[Ci] = c[Bi]
        del c[Bi]

Submission status: Accepted

Solution code:
import collections
n=int(input())
A=list(map(int,input().split()))
q=int(input())
A.sort()
B=collections.Counter(A)
S=sum(A)
C=[]
for _ in range(q):
  b,c=map(int,(input().split()))
  C.append(B[b]*(c-b)+S)
  S+=B[b]*(c-b)
  B[c]+=B[b]
  B[b]=0

for i in C:
  print(i)

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

import collections
ac = collections.Counter(A)

sumA = sum(A)

for b,c in BC:
    cha = 0
    cha += (c * ac[b])
    cha -= (b * ac[b])
    ac[c] += ac[b]
    ac[b] = 0
    sumA += cha
    print(sumA)

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 defaultdict
# 1から10**5までの辞書を作成
memo = defaultdict(int)

for a in A:
    memo[a] += 1

ans = []
now_sum = sum(A)
# print(memo[2])

for ele in BC:

    now_sum += memo[ele[0]]*ele[1] - memo[ele[0]]*ele[0]
    memo[ele[1]] += memo[ele[0]]
    memo[ele[0]] = 0
    ans.append(now_sum)

for an in ans:
    print(an)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for _ in range(Q):
    B.append(list(map(int,input().split())))
s=sum(A)
from collections import Counter
a=Counter(A)
b=list(a.keys())
c=list(a.values())
for i in range(Q):
    s-=B[i][0]*a[B[i][0]]
    s+=B[i][1]*a[B[i][0]]
    a[B[i][1]]+=a[B[i][0]]
    a[B[i][0]]=0
    print(s)

Submission status: Accepted

Solution code:
import collections 
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = collections.Counter(A)
suma = sum(A)
output = []

for q in range(Q):
    b, c = map(int, input().split())
    if b in dic:
        suma += dic[b] * c
        suma -= dic[b] * b
        dic[c] += dic[b]
        dic[b] = 0

    output.append(suma)

print(*output, sep="\n")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
 
import sys
import string
from collections import Counter

def get_int(): return int(sys.stdin.readline().strip())
def get_ints(): return map(int, sys.stdin.readline().strip().split()) 
def get_list(): return list(map(int, sys.stdin.readline().strip().split())) 
def get_string(): return sys.stdin.readline().strip()
def out(data): return sys.stdout.write(str(data))
def out_list(arr): return sys.stdout.write(' '.join(str(e) for e in arr))

MAX = 10**9
MOD = 10**9 + 7

n = get_int()
arr = get_list()
d = Counter(arr)
q = get_int()
sum_ = sum(arr)

for _ in range(q):
	a, b = get_ints()
	if a in d.keys() :
		sum_ += d[a] * (b - a)
		count = d[a]
		d.pop(a)
		if not d[b]:
			d[b] = count
		else:
			d[b] += count
	out(sum_)
	out('\n')

Submission status: Accepted

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

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

Submission status: Accepted

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

li_b = [list(map(int, input().split())) for _ in range(q)]

ans = [0]*10**6

sum = 0
for i in li_a:
    ans[i] += 1
    sum += i

    
for i in range(q):
    b = li_b[i][0]
    c = li_b[i][1]
    
    cnt = ans[b]
    ans[b] = 0
    ans[c] += cnt
    
    sum += (c-b)*cnt
    print(sum)

Submission status: Accepted

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

summed = sum(a_series)
num = [0 for i in range(100005)]

for a in a_series:
    num[a] += 1

for i in range(q):
    summed += ((bc[i][1] - bc[i][0]) * num[bc[i][0]])
    num[bc[i][1]] += num[bc[i][0]]
    num[bc[i][0]] = 0
    print(summed)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
sum_ = sum(A)
q = int(input())
counter = Counter(A)
for _ in range(q):
    b, c = map(int, input().split())
    x = counter[b]
    counter[c] += x
    counter[b] = 0
    sum_ += (c-b) * x
    print(sum_)

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from collections import Counter

    N = int(input())
    A = list(map(int, input().split()))
    CA = Counter(A)
    sumA = 0
    for key, value in CA.items():
        sumA += key*value

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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import collections
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    N=int(input())
    A=list(map(int,input().split()))
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    answer=sum(A)
    c=collections.Counter(A)
    for x in BC:
        B=x[0]
        C=x[1]
        if B in c.keys():
            n=c[B]
            answer=answer-(B-C)*n
            c[B]=0
            c[C]=c[C]+n
        print(answer)



if __name__ == '__main__':
    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)]
Alist=[0]*(10**5+1)
ans=[]

for a in A:
    Alist[a]+=1

sum=0
for i in range(1,len(Alist)):
    sum+=i*Alist[i]

for bc in BC:
    sum=sum-bc[0]*Alist[bc[0]]+bc[1]*Alist[bc[0]]
    Alist[bc[1]]+=Alist[bc[0]]
    Alist[bc[0]]=0
    ans.append(sum)
ans='\n'.join(map(str,ans))
print(ans)

Submission status: Accepted

Solution code:
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1
total = sum([k * v for k, v in A.items()]) 

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        dif = (C - B) * A[B]
        A[C] += A[B]
        del A[B]
        total += dif
    print(total)

Submission status: Accepted

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
import bisect as bs
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
def adj(n): return [[] for _ in range(n)]

n = iinput()
a = iarr()
cnt = col.Counter(a)
sum = sum(a)

q = iinput()
anss = []
for _ in range(q):
    b,c = imap()
    tmp = cnt[b]
    cnt[b] -= tmp
    cnt[c] += tmp
    sum += (c - b) * tmp
    anss.append(sum)

for ans in anss: print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l= list(map(int, input().split()))
table= {}
for num in l:
	if num in table:
		table[num]+=1
	else:
		table[num]=1

tot= 0
for num in l:
	tot+= num

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