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: Wrong Answer

Solution code:
import sys

def L(): 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())

import re
def solver(N,A,Q,BC):

    for b,c in BC:
        #print(A,':',b,'->',c)
        bef = '[\s]*'+str(b)
        aft = ' '+str(c)
        #print("bef,aft=",bef,aft)
        A = re.sub(bef, aft, A)
        #A = A.replace(' '+str(b), ' '+str(c))
        #print(A)
        print(sum(map(int,A.split())))


def main():
    N = I()
    #A = LI()
    A = L()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    print("N,Q=",N,Q)
    print("A=",A)
    print("BC=",BC)

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s = sum(a)

# 辞書型で度数を持つ
hist = {}
for i in range(n):
    if a[i] in hist:
        hist[a[i]] += 1
    else:
        hist[a[i]] = 1
# print(hist)

for i in range(q):
    diff = c[i] - b[i]
    if b[i] in hist:
        s += hist[b[i]]*diff
        print(s)
        if c[i] in hist:
            hist[c[i]] += hist[b[i]]
        else:
            hist[c[i]] = hist[b[i]]
            del hist[b[i]]
    else:
        print(s)
    # print(hist)

Submission status: Wrong Answer

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

s = sum(a)

# 辞書型で度数を持つ
hist = {}
for i in range(n):
    if a[i] in hist:
        hist[a[i]] += 1
    else:
        hist[a[i]] = 1
print(hist)

for i in range(q):
    diff = c[i] - b[i]
    if b[i] in hist:
        s += hist[b[i]]*diff
        print(s)
        if c[i] in hist:
            hist[c[i]] += hist[b[i]]
        else:
            hist[c[i]] = hist[b[i]]
            del hist[b[i]]
    else:
        print(s)
    # print(hist)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(1 in list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  if s in list:
    sum = 0
    for j in range(len(list)):
      if list[j] == s:
        list[j] = t
      sum += list[j]
  print(sum)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(1 in list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum = 0
  if s in list:
    for j in range(len(list)):
      if list[j] == s:
        list[j] = t
      sum += list[j]
  print(sum)

Submission status: Wrong Answer

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

counter=[0]*(10**5+1)
for i in a:
  counter[i]+=1
  
for i in range(q):
  new_sum=suma-b[i]*counter[b[i]]+c[i]*counter[b[i]]
  suma=new_sum
  counter[c[i]]+=1
  counter[b[i]]=0
  print(new_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int,input().split()))
t = int(input())
su = sum(arr)
print(su)
while t:
    t-=1
    k,r = map(int,input().split())
    p = arr.count(k)
    su += (r-k)*p
    print(su)

Submission status: Wrong Answer

Solution code:
n = int(input())+1
a = list(map(int, input().split()))
cnt = []

for i in range(100005):
    cnt.append(0)
sum = 0
for i in range(n):
    cnt[i] = a.count(i)
    sum += i * cnt[i]

q = int(input())
ans = []

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

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    a = input().replace(' ', '+')
    q = int(input())
    for _ in range(q):
        b, c = input().split()
        a = a.replace(b+'+', c+'+')
        a = a.replace('+'+b+'+', '+'+c+'+')
        a = a.replace('+'+b, '+'+c)
        print(eval(a))


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc =[list(map(int, input().split())) for _ in range(q)]
temp = [0] * (10**5 + 1)

for i in a:
    temp[i] += 1
ans = sum(a)
for i in range(q):
    b,c = bc[i][0],bc[i][1]
    ans = ans - temp[b] * b + temp[b] * c 
    print(ans)
    temp[c] += temp[b]

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum = 0
  for j in range(len(list)):
    if s in list:
      if list[j] == s:
        list[j] = t
      sum += list[j]
  print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = 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)

d = [0] * (10**5 + 7)
for a in A:
    d[a] += 1
sum_a = sum(A)
for j in range(Q):
    d[C[j]] += d[B[j]]
    sum_a -= d[B[j]] * B[j]

    sum_a += (d[C[j]] - d[B[j]]) * C[j]
    d[B[j]] = 0
    print(sum_a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_cnt = [0] * 100001
A_sum = 0

# 各数値の数を数えておく
for a in A:
    A_cnt[a]+=1
    # ついでに現時点の総和も出しておく
    A_sum+=a

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

Submission status: Wrong Answer

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

s = ','.join(map(str, a)) + ','
ans = []

for _ in range(q):
    b, c = map(str, input().split())
    s = s.replace(b+',', c+',')
    ans.append(s)
    
for i in ans:
    print(i[:-1])
    print(sum(int(str) for str in i[:-1].split(',')))

Submission status: Wrong Answer

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

a_sum = sum(a)

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in dic:
        buf = dic[str(bc[i][0])]
        if str(bc[i][1]) in dic:
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

        a_sum += buf*(-dic[str(bc[i][0])] + dic[str(bc[i][1])])
        dic[str(bc[i][0])] = 0


    print(a_sum)

Submission status: Wrong Answer

Solution code:
n=int(input())
*a,=map(int, input().split())
from collections import Counter
ca=Counter(a)
q=int(input())
sm=sum(a)
for _ in range(q):
    bi,ci=map(int, input().split())
    if ci in ca:
        if bi in ca:
            sm+=(ci-bi)*ca[ci]
            ca[ci]+=ca[bi]
            del ca[bi]
    else:
        if bi in ca:
            sm+=(ci-bi)*ca[bi]
            ca[ci]=ca[bi]
            del ca[bi]
    print(sm)

Submission status: Wrong Answer

Solution code:
n=int(input())
*a,=map(int, input().split())
from collections import Counter
ca=Counter(a)
q=int(input())
sm=sum(a)
for _ in range(q):
    bi,ci=map(int, input().split())
    if ci in ca:
        if bi in ca:
            sm+=(ci-bi)*ca[ci]
            ca[ci]+=ca[bi]
            del ca[bi]
    else:
        if bi in ca:
            sm+=(ci-bi)*ca[ci]
            ca[ci]=ca[bi]
            del ca[bi]
    print(sm)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import copy

N = int(input())

target_list = map(int, input().split())
trans_list = {}

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

check = []
check.append(Counter(target_list))
answer = []

for index, trans in trans_list.items():
    if trans[0] in check[index].keys():
        result = copy.deepcopy(check[index])
        if trans[1] in result:
            result[trans[1]] = result[trans[1]] + result.pop(trans[0])
        else:
            result[trans[1]] = result.pop(trans[0])
    else:
        result = copy.deepcopy(check[index])
    check.append(Counter(result))

for r in check:
    print(sum([x * y for x, y in r.items()]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().split(' ')
Q = int(input())
bc = [input().split() for _ in range(Q)]
A = [int(i) for i in A]
for i in range(Q):
    bc[i] = [int(j) for j in bc[i]]
print(bc)

for i in range(Q):
    for j in range(len(A)):
        if A[j] == bc[i][0]:
            A[j] = bc[i][1]
    ans = sum(A)
    print(ans)

Submission status: Wrong Answer

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

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(10**5+1)
for i in range(10**5+1):
  numlist[i]=listA.count(i)
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  sum=sum+(C-B)*numlist[B]
  print(C)
  numlist[C]+=numlist[B]
  numlist[B]=0
  # print(numlist)
  print(sum)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    if a[i] in dic:
        dic[a[i]]+=1
    else:
        dic.setdefault(a[i],1)
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if b not in dic:
        dic.setdefault(b,0)
    elif c not in dic:
        dic.setdefault(c,dic[b])
    else:
        dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    print(answer)

Submission status: Wrong Answer

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

dp=[0 for n in range(100000)]
for n in range(len(A)):
    dp[A[n]-1]+=1

init=0
for n in range(1,len(dp)):
    init+=n*dp[n-1]

ans=init
for n in range(Q):
    B,C=map(int,input().strip().split())
    ans=ans-dp[B-1]*B+dp[B-1]*C
    dp[C-1]+=dp[B-1]
    dp[B-1]=0
    print(ans)

Submission status: Wrong Answer

Solution code:
#D
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
mp = defaultdict(int)
for i in a:
    mp[i] += 1
q = int(input())
for _ in range(q):
    print(mp)
    s = 0   
    b,c=map(int,input().split())
    if c in mp:
        mp[c] = mp[c]+mp[b]
        mp.pop(b)
    elif b in mp:
        mp[c] = mp.pop(b)
        
    for i in mp:
        s += (mp[i])*i
    print(s)

Submission status: Wrong Answer

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

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

S=sum(A)
#まずはじめの合計求める

num=[0]*(10**5+1)
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num[i]=cnt

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    change=num[B]*(C-B)
    num[B]-=num[B]
    num[C]+=num[B]
    S+=change
    print(S)

Submission status: Wrong Answer

Solution code:
from collections import Counter, deque
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
L = deque()
for _ in range(Q):
    b, c = map(int, input().split())
    L.append([b, c])
d = Counter(A)
sum_a = sum(A)
for j in range(Q):
    brep = d[L[j][0]]
    sum_a -= L[j][0] * brep
    if (d[L[j][1]]):
        d[L[j][1]] += brep
        d[L[j][0]] -= brep
    else:

        d[L[j][1]] = brep
    sum_a += L[j][1] * brep

    print(sum_a)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
sumA = sum(A)
counter = [0]*(10**5+1)
for i in range(n):
    counter[A[i]] += 1

print(counter[:30])
q = int(input())

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

for b,c in queue:    
    b_cnt = counter[b]
    sumA -= (b*b_cnt)
    counter[b] = 0
    c_cnt = counter[c]
    sumA += c*(b_cnt)
    counter[c] = b_cnt+c_cnt
    print(sumA)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
L = []
for _ in range(Q):
    b, c = map(int, input().split())
    L.append([b, c])
d = Counter(A)
sum_a = sum(A)
for j in range(Q):
    brep = d[L[j][0]]
    sum_a -= L[j][0] * brep
    if (d[L[j][1]]):
        d[L[j][1]] += brep
    else:
        d[L[j][1]] = brep
    sum_a += L[j][1] * brep

    print(sum_a)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)

sum = 0
for k , v in count.items():
    sum += (k * v)

for B,C in pala:
    diff = (C - B) * count[B]
    res = sum + diff
    print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [0] * (10 ** 5 + 1)
ans = sum(A)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

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

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())
B_len = len(B)
ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  jg = False
  for j in range(B_len):
    if X[j][0] == y:
      jg = True
      jgx = j
  for j in range(B_len):
    if X[j][0] == x:
      X[j][0] = y
      if jg == True:
        X[j][1] += X[jgx][1]
        X[jgx][0] = 0
        B_len = B_len-1
  for j in range(B_len):
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)

Submission status: Wrong Answer

Solution code:
import bisect

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

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  jg = False
  for j in range(len(B)):
    if X[j][0] == y:
      jg = True
      jgx = j
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
      if jg == True:
        X[j][1] += X[jgx][1]
        X[jgx][0] = 0
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
# stdin = open("sample.txt")

MOD = 1000000007
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
def keta(kazu): # 入力されたintを桁ごとに分解，リストで出力
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): # 最小公倍数
    return reduce(math.gcd, numbers)

def combination(m,n): # mCn
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def main():
    n = int(stdin.readline().rstrip())
    a = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    dic = {}
    ans = 0
    for i in range(len(a)):
        if a[i] in dic:
            dic[a[i]] += 1
        else:
            dic[a[i]] = 1
    for k, v in dic.items():
        if v != 0:
            ans += k*v
    q = int(stdin.readline().rstrip())
    for i in range(q):
        b, c = [int(x) for x in stdin.readline().rstrip().split()]
        if b in dic:
            ans += (c-b)*dic[b]
            print(ans)
            if c in dic:
                dic[c] += dic[b]
            else:
                dic[c] = dic[b]
            dic[b] = 0

main()

Submission status: Wrong Answer

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

a = [0] * (10 ** 5 + 1)
ans = sum(A)

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

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

Submission status: Wrong Answer

Solution code:
# # ####################################################################
# import io
# import sys

# _INPUT = """\
# 1
# 10
# 3
# 10 1
# 1 2
# 3 1
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################


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

#「groupby」を使って、
from itertools import groupby
L = [[k, len(list(g))] for k, g in groupby(A)]
# print( L )

sumA = 0
from collections import defaultdict
d = defaultdict(int)
for k,v in L:
  d[k] = v
  sumA += k*v

# print(d)

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

  sumA += (c-b) * d[b]
  print(sumA)

  d[c] += d[b]
  d[b] = 0

Submission status: Wrong Answer

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

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

sum = sum(a)

for _ in range(q):
    b, c = map(int, input().split())
    if b in l:
        sum += (c - b) * l[b]
        l[c] = l[c] + l[b] if l.get(c) else l[b]
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
l = list(map(int, input().split()))
a = int(input())
c = collections.Counter(l)
answer = sum(l)
for i in range(a):
    j,k =  map(int, input().split())
    save = c[j]
    del c[j]
    c[k] += save
    answer = answer + c[k] -save
    print(answer)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_numbers = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_numbers:
        numbers[num] += 1

    total = sum(raw_numbers)

    Q = int(input())

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

Submission status: Wrong Answer

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

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

for i in range(N):
    a[A[i]] += 1
    ans += A[i]

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

Submission status: Wrong Answer

Solution code:
k = [i for i in range(1, 10 ** 5 + 1)]
v = [0] * (10 ** 5)
dic = {key: val for key, val in zip(k, v)}
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  dic[a[i]] += 1
print(dic)
q = int(input())
ans = [sum(a)]
for i in range(q):
  b, c = map(int, input().split())
  ans.append(ans[-1] + (c - b) * dic[b])
  print(dic[b])
  print(dic[c])
  dic[c] += dic[b]
  dic[b] = 0
for i in range(1, q + 1):
  print(ans[i])

Submission status: Wrong Answer

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

from collections import defaultdict
dic = defaultdict(int)

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

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

Submission status: Wrong Answer

Solution code:
import collections
import random
inp = list(range(1, 100001))
dic = dict(collections.Counter(inp))
wa = 0
for i, j in dic.items():
	wa += i * j
se = set(dic.keys())
for i in range(int(input())):
	a = random.randrange(1, 10001)
	b = random.randrange(1, 10001)
	if a in se:
		wa += dic[a] * (b-a)
		if b in se:
			pass
		else:
			dic[b] = 0
		se.add(b)
		dic[b] += dic[a]
		dic[a] = 0
	print(wa)

Submission status: Wrong Answer

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

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

sum = 0
for k, v in l.items():
    sum += k * v

for _ in range(q):
    b, c = map(int, input().split())
    if b in l:
        sum += (c - b) * l[b]
        l[c] = l[c] + l[b] if l.get(c) else l[b]
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
from collections import defaultdict
R = defaultdict(int)
for a in A:
    R[a] += 1
Q = int(input())
for i in range(Q):
    b,c = map(int,input().split())
    cnt = R[b]
    del R[b]
    R[c] += cnt
    if i == 0:
        ans = 0
        for key in R.keys():
            ans += (R[key]*key)
            print(ans)
    else:
        ans = ans - cnt*b + cnt*c
        print(ans)

Submission status: Wrong Answer

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


for i in range(Q):
    B, C = map(int, input().split())
    for j in range(N):
        if A[j] == B:
            A[j] = C

print(sum(A))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
X = Counter(a)
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())

ans = sum(a)
for num, rep in zip(b, c):
    ans += (rep * X[num] - num * X[num])
    X[rep] += X[num]
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = []
c = 0
s = sum(A)
table = [0] * 100010
# A = pd.Series(A)
# for a in A:
#     print(a)

for i in A:
    table[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    # if B in A:
    # c = A.count(B)
    c = table[B]
    s += (C - B) * c
    table[C] = table[B]
    table[B] = 0
    # print(s)
        # print((C - B) * c) 
        # string = ",".join(A)
        # string_new = string.replace(B,C)
        # A =  string_new.split(",")
        # s = sum(map((lambda x: int(x)), A))
    S.append(s)

        # A.replace(B, C,c)
        # while B in A:
        #     A[A.index(B)] = C
    # for a in A:
    #     print(a)
    # else:
        # S.append(s)



for s in S:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
raw_numbers = list(map(int, input().split()))
numbers = defaultdict(int)

for num in raw_numbers:
    numbers[num] += 1
    
total = sum(raw_numbers)
    
Q = int(input())

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= a * numbers[a]
    numbers[b] += numbers[a]
    total += b * taken
    print(total)

Submission status: Wrong Answer

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

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

sum = 0
for k, v in l.items():
    sum += k * v

for _ in range(q):
    b, c = map(int, input().split())
    if b in l:
        sum += (c - b) * l[b]
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = []
c = 0
s = sum(A)
table = [0] * 100010
# A = pd.Series(A)
# for a in A:
#     print(a)

for i in A:
    table[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    # if B in A:
    # c = A.count(B)
    c = table[B]
    s += (C - B) * c
    table[C] = table[B]
    table[B] = 0
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
raw_numbers = list(map(int, input().split()))
numbers = defaultdict(int)

for num in raw_numbers:
    numbers[num] += 1
    
total = sum(raw_numbers)
    
Q = int(input())

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

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int,input().split()))
#print(a)
q = int(input())
z = [0 for i in range(100001)]
for i in range(len(a)):
    z[a[i]] += 1

b = []
for _ in range(q):
    b.append(list(map(int,input().split())))
#print(b)


for i in range(q):
    z[b[i][1]] += z[b[i][0]]
    z[b[i][0]] = 0
    #print(z[:5])
    s = 0
    for j in range(n+1):
        s += j * z[j]
    print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
M = 10**5+1
g = [0] * M
s = 0
for A in a:
    g[A] += 1
    s += A

q = int(input())
for loop in range(q):
    b,c = map(int,input().split())
    if g[b] == 0:
        print(s)
        continue
    s += g[b] * (c - b)
    g[c] += g[b]
    g[b] == 0
    print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list( map(int, input().split()) )
Q = int(input())
ans = sum(A)
for i in range(Q):
    b, c = map(int, input().split())
    x = A.count(b)
    ans = ans + x*(c-b)
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
X = Counter(a)
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())

ans = sum(a)
for num, rep in zip(b, c):
    ans += (rep - num) * X[num]
    X[rep] += X[num]
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
q = int(input().strip())
bc = list()
for i in range(q):
    b,c = [int(x) for x in input().strip().split(' ')]
    bc.append((b,c))

a2 = list()
clear_list = a[:]
while clear_list:
    t = clear_list[0]
    a2.append([t,a.count(t)])
    clear_list = [x for x in clear_list if x != t]            

a2.sort()
for x in bc:
    for y in a2:
        print(x,y)
        if y[0] == x[0]:
            y[0] = x[1]
        elif y[0] > x[0]:
            break
    a2.sort()
    s = math.floor(sum([float(x[0]) * x[1] for x in a2]))
    print(s)

Submission status: Wrong Answer

Solution code:
import numpy as np
M = 100002

if __name__ == "__main__":
  N = int(input())
  A = [int(x) for x in input().split()]
  Q = int(input())
  Num = np.zeros((M))
  Sum = sum(A)
  for i in range(N):
    Num[i] += 1
  for i in range(Q):
    B, C = [int(x) for x in input().split()]
    if(Num[B] == 0):
      print(int(Sum))
    else:
      Sum = Sum + (C - B)*Num[B]
      Num[C] += Num[B]
      Num[B] = 0
      print(int(Sum))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

adic=dict()

for ai in a:
    if ai in adic:
        adic[ai]+=1
    else:
        adic[ai]=1
        
q=int(input())
b=[0]*(q+1)
c=[0]*(q+1)
for i in range(q):
    b[i],c[i]=map(int,input().split())
    
#print(adic)    
for bi,ci in zip(b,c):
    if bi in adic and ci in adic:
        adic[ci]=adic[ci]+adic[bi]
        del adic[bi]
    elif bi in adic and (not ci in adic):
        adic[ci]=adic[bi]
        del adic[bi]
#    print(i,adic)
    asum=0
    for i,ad in adic.items():
        asum+=i*ad
    print(asum)

Submission status: Wrong Answer

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