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())
S = []
for _ in range(q):
    x, y = map(int, input().split())
    S.append((x, y))

sum = sum(A)
# print(sum)
R = [0 for _ in range(10**5+1)]
for a in A:
    R[a-1] += 1

for (x,y) in S:
    sum += (y-x)*R[x-1]
    R[y-1] += R[x-1]
    R[x-1] = 0
    print(sum)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int,input().split()))
sum1 = sum(Alist)
dic = {}
for a in Alist:
    if not a in dic:
        dic[a] = 0
    dic[a]+=1

Q = int(input())
Qlist = []
for _ in range(Q):
    Qlist.append(list(map(int,input().split())))
for q in Qlist:
    b,c = q
    n = 0
    if b in dic:
        n = dic[b]
        dic[b] = 0
    else:
        pass

    if c in dic:
        dic[c] += n
    else:
        dic[c] = n
    sum1 += n*(c-b)

    print(sum1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    q = int(input())
    bc_lst = [list(map(int, input().split())) for _ in range(q)]

    a_max = max(a_lst)
    bc_max = 0
    for i in range(q):
        b = bc_lst[i][0]
        c = bc_lst[i][1]
        bc_max = max(b, bc_max)
        bc_max = max(c, bc_max)
    maximum = max(a_max, bc_max)

    count_lst = [0] * maximum
    for i in range(n):
        a = a_lst[i]
        count_lst[a - 1] += 1
    tmp = sum(a_lst)

    for i in range(q):
        b = bc_lst[i][0]
        c = bc_lst[i][1]

        count = count_lst[b - 1]
        tmp += count * (c - b)
        count_lst[b - 1] = 0
        count_lst[c - 1] += count

        print(tmp)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
v = 0
ans = []

for a in A:
    d[a] += 1
    v += a

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

for a in ans:
    print(a)

Submission status: Accepted

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

d = {}
v = 0
ans = []

for a in A:
    d[a] = d.get(a, 0) + 1
    v += a

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

for a in ans:
    print(a)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter
N=int(input())
Alist=list(map(int,input().split()))
Q=int(input())
bc=[]
for _ in range(Q):
    bc.append(list(map(int,input().split())))
count=Counter(Alist)
result=0
for key,value in count.items():
    result+=key*value
for i in range(Q):
    if bc[i][0] in count.keys():
        result+=(bc[i][1]-bc[i][0])*count[bc[i][0]]
        count[bc[i][1]]+=count[bc[i][0]]
        count[bc[i][0]]=0
    print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
List = list(map(int, input().split()))
Row = int(input())
QList = []
for i in range (Row):
  QList.append(list(map(int, input().split())))
dictA ={}
res = 0
for i in range (N):
  dictA.setdefault(List[i],0) 
  dictA[List[i]] += 1
  res += List[i]
num = 0
for i in range(Row):
  if QList[i][0] not in dictA:
    pass
  else:
    num = dictA[QList[i][0]] 
    res = res - dictA[QList[i][0]] * QList[i][0]
    dictA[QList[i][0]] = 0
    dictA.setdefault(QList[i][1],0) 
    dictA[QList[i][1]] += num
    res += QList[i][1] * num
  print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
num_list = [0] * 10**5
ans = 0

for i in range(N):
    num_list[int(A[i])-1] += 1

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

Q = int(input())

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

    ans = ans + C * num_list[B-1] - B * num_list[B-1]
    num_list[C-1] += num_list[B-1]
    num_list[B-1] = 0

    print(ans)

Submission status: Accepted

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

cnt_dict = {}


s = sum(A)

ans = 0

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

for _ in range(Q):
    X,Y = map(int,input().split())

    if X not in cnt_dict:
        cnt_dict[X] = 0
    if Y not in cnt_dict:
        cnt_dict[Y] = 0

    s -= cnt_dict[X] * X
    s += cnt_dict[X] * Y

    cnt_dict[Y] += cnt_dict[X]
    cnt_dict[X] = 0


    print(s)





'''
#初期案　遅すぎ

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



for i in range(Q):
    X,Y = map(int,input().split())

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)

    '''

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math , sys

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

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

for each in A:
    count[each]+=1
ans = sum(A)
for _ in range(Q):
    B , C = list( map( int , input().split() ) )
    D = count[B]
    ans += (C-B) * D
    count[B] = 0
    count[C] += D
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
ans = sum(a)
ans_li = []
q = int(input())

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

for i in range(q):
    B,C = map(int,input().split())
    if B not in d:
        ans_li.append(ans)
    else:
        num = d[B]
        dis = abs(B - C)
        if B >= C:
            ans_li.append(ans - (num * dis))
            ans = ans_li[-1]
        else:
            ans_li.append(ans + (num * dis))
            ans = ans_li[-1]

        if C not in d:
            d[C] = num
            d[B] = 0
        else:
            d[C] += num
            d[B] = 0

for aa in ans_li:
    print(aa)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    sum_a = sum(a_list)

    a_dict = {}
    for a in a_list:
        if a not in a_dict:
            a_dict[a] = 1
        else:
            a_dict[a] += 1

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

        if b not in a_dict:
            a_dict[b] = 0
        sum_a += (c - b) * a_dict[b]

        if c not in a_dict:
            a_dict[c] = 0
        a_dict[c] += a_dict[b]
        a_dict[b] = 0

        print(sum_a)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections

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

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in a_dict.items()}
    answer = sum(a_dict.values())
    for _ in range(q):
        b, c = map(int, input().split())
        diff = 0
        if b in a_dict:
            b_sum = a_dict.pop(b)
            c_add = b_sum//b * c
            diff = c_add - b_sum
            if c in a_dict:
                a_dict[c] += c_add
            else:
                a_dict[c] = c_add
        answer += diff
        print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
# 正数列A
A = list(map(int, input().split()))
Q = int(input())
# B_iである要素をC_iに置き換える
B_C = [list(map(int, input().split())) for _ in range(Q)]

all_count = Counter(A)
S = sum(A)

for B, C in B_C:

    count_B = all_count[B]
    all_count[B] = 0
    all_count[C] += count_B
    S = S + (C-B)*count_B

    print(S)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Q = int(input())
 
sumAs = 0
dictAs = [0] * 100000
for i in range(len(As)):
  sumAs += As[i]
  dictAs[As[i] - 1] += 1


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

Submission status: Accepted

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

s = sum(a)
nums = {}
for i in range(n):
    if a[i] in nums:
        nums[a[i]] += 1
    else:
        nums[a[i]] = 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 01:13:08 2020

@author: liang
"""
"""
ABC  171 D 差分更新　パケット変換
【無駄な探索は行わない（メモリ化） ⇒　パケット変換】
パケット
　値vが出現する回数を記録しておく

dict()方式　⇒　探索にO(logn) or O(n)がかかる
配列方式　⇒　探索がO(1)となる!

【差分更新】
値Bが全て値Cになる
　⇒　(C - B) * 値Bの個数だけ増加する
 ⇒　値Cの個数　+= 値Bの個数
 ⇒　値Bの個数　= 0
"""

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

d = [0]*(10**5)

for i in A:
    d[i-1] += 1

ans = sum(A)

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

Submission status: Accepted

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

d = [0]*(10**5)

for i in A:
    d[i-1] += 1

ans = sum(A)

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

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)  # はじめの合計、名前がsumだとsum関数とかぶってよくないので

# q回操作します。ループ変数は使わないので_とします（Pythonの慣習）
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:
if __name__ == '__main__':

    n = int(input())
    B = [0]*(10**5+1)
    A = list(map(int,input().split()))
    for a in A:
        B[a] += 1
    #合計
    sm = sum(A)
    m = int(input())

    for j in range(m):
        b,c = map(int,input().split())
        #bの個数の増減をチェック
        cnt_b = B[b]
        cnt_c = B[c]
        if cnt_b == 0:
            #変換できないので何もしない
            print(sm)
        else:
            B[b] = 0
            B[c] += cnt_b
            sm = sm + c*cnt_b - b*cnt_b
            print(sm)

Submission status: Accepted

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

for i in range(n):
    if a[i] not in d.keys():
        d[a[i]] = 1
    else:
        d[a[i]] += 1

s = sum([k * v for k, v in d.items()])

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

Submission status: Accepted

Solution code:
def Replacing():
    N = int(input())
    A = list(map(int,input().split()))
    lis = [0 for i in range(10 ** 5)]
    res = 0
    for i in A:
        lis[i-1] += 1
        res += i
    
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        num = lis[b-1]
        res += (c - b) * num
        lis[b-1] = 0
        lis[c-1] += num
        print(res)
        
Replacing()

Submission status: Accepted

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

cnt_dict = {}

s = sum(A)
ans = []

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

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

    if b not in cnt_dict:
        cnt_dict[b] = 0
    if c not in cnt_dict:
        cnt_dict[c] = 0

    s -= b * cnt_dict[b]
    s += c * cnt_dict[b]

    cnt_dict[c] += cnt_dict[b]
    cnt_dict[b] = 0

    ans.append(s)

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    S = sum(A)
    cunt_A = Counter(A)
    for q in range(Q):
        b, c = map(int,input().split())
        S += (c-b)*cunt_A[b]
        cunt_A[c] += cunt_A[b]
        cunt_A[b] = 0
        print(S)

if __name__=='__main__':
    main()

Submission status: Accepted

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

cnt = [0 for i in range(10**5)]
a = sorted(a)
s = sum(a)

for i in range(n):
    cnt[a[i]-1] += 1

q = int(input().strip())

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

Submission status: Accepted

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

a_dict = {}

for i in a:
    if not i in a_dict:
        a_dict[i] = 0
    a_dict[i] += 1

q = int(input())

s = sum(a)


for i in range(q):
    b, c = map(int, input().split())
    if not c in a_dict:
        a_dict[c] = 0
    s -= c * a_dict[c]

    if b in a_dict:
        s -= b * a_dict[b]
        a_dict[c] += a_dict[b]
    s += c * a_dict[c]

    a_dict[b] = 0

    print(s)

Submission status: Accepted

Solution code:
from collections import Counter

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

sum_a = sum(A)

d = Counter(A)

for _ in range(int(input())):
    b, c = map(int, input().split())
    temp = d[b]         
    d[b] -= temp
    d[c] = d.get(c, 0) + temp
    sum_a += (c - b) * temp
    print(sum_a)

Submission status: Accepted

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

sum_a = sum(A)

d = dict()

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

for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:

        temp = d[b]         
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

        sum_a += (c - b) * temp
    print(sum_a)

Submission status: Accepted

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

    sum_a = sum(A)

    d = dict()

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

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:

            temp = d[b]         
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

            sum_a += (c - b) * temp
        print(sum_a)
        
main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

temp = [0]*(10**5+1)
ans = 0

for i in A:
    temp[i] += 1
    ans += i
for i in range(Q):
    B, C = map(int,input().split()) 
    ans += temp[B]*(C-B)
    temp[C] += temp[B]
    temp[B] = 0

    print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    import collections
    counter = collections.Counter(A)
    #print(counter)
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]
    ans = sum(A)
    for b, c in BC:
        if b in counter:
            ans -= b*counter[b]
            ans += c*counter[b]
            if c not in counter:
                counter[c] = 0
            counter[c] += counter[b]
        counter[b] = 0
        #print(counter)
        print(ans)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b in t.keys():
            s += (c - b) * t[b]
            t[b], t[c] = 0, t[b] + t[c]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b not in t.keys():
            ans.append(s)
            continue
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if c in t.keys():
            t[c] += t[b]
        else:
            t[c] = t[b]
        s += (c - b) * t[b]
        del t[b]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

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)

count = collections.Counter(a)
sums = sum(a)

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

Submission status: Accepted

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

a_sum = sum(A)
counter = [0]*10**5

for a in A:
    counter[a-1] += 1

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

Submission status: Accepted

Solution code:
N = int(input())
natural = list(map(int,input().split()))
Q = int(input())
number = [0]*(10**5+1)
for i in range(N):
    number[natural[i]] += 1
Nsum = sum(natural)
ans = []
for i in range(Q):
    B,C = map(int,input().split())
    count = number[B]
    diff = C-B
    number[C] += count
    number[B] = 0
    Nsum += diff * count
    ans.append(Nsum)

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

Submission status: Accepted

Solution code:
n = int(input())
lst = [0]*(10**5)

l = list(map(int,input().split()))
total = sum(l)  
for x in l:
  lst[x-1] += 1

  
m = int(input())  
for i in range(m):
  x,y = map(int,input().split())
  lst[y-1] += lst[x-1]
  total += (y-x) * lst[x-1]
  lst[x-1] = 0  
  print(total)

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    q = int(input())
    bc = []
    sum1 = sum(a)
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    d = defaultdict(lambda:0)
    for i in range(n):
        d[a[i]] += 1

    for i in range(q):
        if bc[i][0] in d:
            sum1 += (bc[i][1] - bc[i][0]) * d[bc[i][0]]
            d[bc[i][1]] += d[bc[i][0]]
            del d[bc[i][0]] 
        print(sum1)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

total = sum(A)

li = [0] * 100000
for i in range(N):
  li[A[i] - 1] += 1

for i in range(Q):
  j = BC[i][0]
  k = BC[i][1]
  if li[j - 1] > 0:
    li[k - 1] += li[j - 1]
    total += li[j - 1] * (k - j)
    li[j - 1] = 0
  print(total)

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

cou = Counter(a)
s = sum(a)

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from collections import defaultdict

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

d = defaultdict(int)
sub = 0
for i in range(n):
    d[a[i]] += 1
    sub += a[i]

for i in range(q):
    if b[i] <= c[i]:
        dd = c[i]-b[i]
        sub += d[b[i]] * dd
        d[c[i]] += d[b[i]]
        d[b[i]] = 0
    else:
        dd = b[i]-c[i]
        sub -= d[b[i]] * dd
        d[c[i]] += d[b[i]]
        d[b[i]] = 0
    print(sub)

Submission status: Accepted

Solution code:

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

count = [0 for i in range(int(1e5+1))]

total = 0
for a in A:
    count[a] += 1
    total += a

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

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
dic = defaultdict(int)
ans = sum(A)
for a in A:
    dic[a] += 1
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    ans -= dic[b] * b
    ans -= dic[c] * c
    dic[c] += dic[b]
    dic[b] = 0
    ans += dic[c] * c
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
sm = 0
d = defaultdict(int)
for a in A:
    d[a] += 1
    sm += a
Q = int(input())
for _ in range(Q):
    b,c = map(int,input().split())
    sm += d[b]*(c-b)
    d[c] += d[b]
    d[b] = 0
    print(sm)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Submission status: Accepted

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

l=[0]*10**6
total=0

for i in range(N) :
    l[A[i]]+=1   #各数字の個数
    total+=A[i]  #初期総和

for i in range(Q) :
    s0=S[i][0]  #Bの値、交換した値
    s1=S[i][1]  #Cの値、交換する値
    total+=s1*l[s0]-s0*l[s0]
    l[s1]+=l[s0]
    l[s0]=0
    print(total)

Submission status: Accepted

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

num_dic = {}
for i in range(N):
    if A[i] in num_dic:
        num_dic[A[i]] += 1
    else:
        num_dic[A[i]] = 1

tmp = sum(A)
for query in querys:
    if query[0] in num_dic:
        tmp -= num_dic[query[0]] * query[0]
        tmp += num_dic[query[0]] * query[1]

        if query[1] in num_dic:
            num_dic[query[1]] += num_dic[query[0]]
        else:
            num_dic[query[1]] = num_dic[query[0]]
        num_dic[query[0]] = 0

    print(tmp)

Submission status: Accepted

Solution code:
import math
from itertools import combinations
from itertools import combinations_with_replacement
from itertools import product
from collections import defaultdict
from collections import deque
from collections import Counter
import heapq


def input_li():
    return list(map(int, input().split()))


N = int(input())
A_LI = input_li()
Q = int(input())
cnt = Counter(A_LI)
ans = sum(A_LI)
BC_LI = []
for _ in range(Q):
    BC_LI.append(input_li())
for b, c in BC_LI:
    ans -= b * cnt[b]
    ans += c * cnt[b]
    print(ans)
    if c in cnt:
        cnt[c] += cnt[b]
    else:
        cnt[c] = cnt[b]
    cnt[b] = 0

Submission status: Accepted

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

Submission status: Accepted

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

l = [0] * 10**6
tot = 0
for i in range(N) :
    l[A[i]]+=1
    tot += A[i]
for i in range(Q):
    s0 = S[i][0]
    s1 = S[i][1]
    tot += s1 * (l[s0] + l[s1]) - (s0 * l[s0] + s1*l[s1])
    l[s1] += l[s0]
    l[s0] = 0
    print(tot)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = [list(map(int, input().split())) for l in range(Q)]
 
numList = {}
sum = 0

for n in range(N):
  if A[n] not in numList:
    numList[A[n]] = 0
  numList[A[n]] += 1
  sum += A[n]

for q in range(Q):
  if S[q][0] in numList:
    if S[q][1] not in numList:
      numList[S[q][1]] = 0
    sum += (S[q][1] - S[q][0]) * numList[S[q][0]]
    numList[S[q][1]] += numList[S[q][0]]
    numList[S[q][0]] = 0
  print(sum)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
#======================================================#
def main():
    n = II()
    aa = MII()
    q = II()
    bc = [MII() for _ in range(q)]
    sumv = sum(aa)
    numbers = [0]*(10**5+1)
    for a in aa:
        numbers[a] += 1

    for b,c in bc:
        numb = numbers[b]
        sumv += numb*(c-b)
        print(sumv)
        numbers[b] = 0
        numbers[c] += numb

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    v_freq = collections.Counter(A)
    v_sum = 0
    for v, freq in v_freq.items():
        v_sum += v * freq
    Q = int(input())
    for _ in range(Q):
        B, C = map(int, input().split())
        if B in v_freq:
            v_sum -= (B - C) * v_freq[B]
            if C in v_freq:
                v_freq[C] += v_freq[B]
            else:
                v_freq[C] = v_freq[B]
            del v_freq[B]
            print(v_sum)
        else:
            print(v_sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
cnt = [0]*(10**5+1)

s = sum(A)
for i in A:
  cnt[i] += 1

for _ in range(Q):
  B, C = map(int, input().split())
  s += (C-B)*cnt[B]
  print(s)
  cnt[C] += cnt[B]
  cnt[B] = 0

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

total=sum(a)

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

for i in a:
    count[i]+=1

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = tuple(map(int,input().split()))
Q = int(input())
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0