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 numpy as np
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
Q=int(input())
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=map(int,input().split())
#print(A)
D=[0]*10**5;W=[0]*10**5
for i in range(10**5):
  D[i]=A.count(i+1)
  W[i]=i+1

for i in range(Q):
    D[C[i]-1]+=D[B[i]-1]
    D[B[i]-1]=0
    print(int(np.average(D, weights=W)*10**5*(10**5+1)//2))

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_sum = sum(al)
al_c = Counter(al)
# print(al_c)

q = int(input())
for _ in range(q):
    bi, ci = map(int, input().split())
    al_sum += al_c[bi]*(ci - bi)

    al_c.update({ci:al_c[bi]})
    del al_c[al_c[bi]]

    print(al_sum)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_sum = sum(al)
al_c = Counter(al)
# print(al_c)

q = int(input())
for _ in range(q):
    bi, ci = map(int, input().split())
    al_sum += al_c[bi]*(ci - bi)

    al_c.update({ci:al_c[bi]})
    del al_c[al_c[bi]]

    print(al_sum)

Submission status: Wrong Answer

Solution code:
import collections

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

ad = collections.Counter(a)
print(ad)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
i = 0
j = 0
while i < Q:
    B, C = map(int, input().split())
    for j in range(len(A)):
        if B == j:
            A[j] = C
        print(sum(A))
    i += 1

Submission status: Wrong Answer

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

sum=0
for i in ar:
    cnt[i]+=cnt[i]+1;
    sum+=i;
q=int(input())
for i in range(0,q):
    a,b=map(int,input().split(' '))
    sum-=cnt[a]*a
    sum+=cnt[a]*b
    cnt[b]+=cnt[a]
    cnt[a]=0
    print(sum)

Submission status: Wrong Answer

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(N):
    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: Wrong Answer

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[Y] * Y

    cnt_dict[X] += cnt_dict[Y]
    cnt_dict[Y] = 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: Wrong Answer

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

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

@author: liang
"""
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

d = dict()
for i in A:
    if i in d.keys():
        d[i] += 1
    else:
        d[i] = 1

ans = sum(A)

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

Submission status: Wrong Answer

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

from collections import Counter

counter = Counter(a)
ans = sum(a)

for _ in range(q):
    before, after = list(map(int,input().split()))

    ans -= before * counter[before]
    ans += after * counter[after] 
    
    counter[after] += counter[before] 
    counter[before] -= 0

    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
for _ in [0]*Q:
    b, c = map(int, input().split())
    A[A==b] = c
    print(A)
    print(np.sum(A))

Submission status: Wrong Answer

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):
        t[c] += t[b]
        t[b] = 0
        s += (c - b) * t[b]
        ans.append(s)
    return "\n".join(map(str, ans))


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

Submission status: Wrong Answer

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

for _ in range(Q):
  B, C = map(int, input().split())
  count = A.count(B)
  if count >0:
    A.remove(B)
    A.extend([C]*count)
  print(sum(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i], C[i] = map(int,input().split())
for i in range(Q):
    if B[i] not in A:
        continue
    for j in range(N):
        if A[j] == B[i]:
            A[j] = C[i]
    print(sum(A))

Submission status: Wrong Answer

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

asum = sum(a)
adic = {}

for i in a:
  if i in adic:
    adic[i] += 1
  else:
    adic[i] = 1


for i in range(q):
  b, c = map(int, input().split())
  if b in adic:
    if c not in adic:
      adic[c] = 0
    cnt = adic.pop(b)
    adic[c] += cnt
  if b in adic:
    asum += (c-b) * cnt
  print(asum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
sums = sum(a)
d = {}

for i in a:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1

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

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 i in range(q)]

asum=sum(a) #当初の数列Aの要素の和
d={}

for i in a:
  if i in d:
    d[i]+=1
  else:
    d[i]=1

for b,c in bc:
  print(b)
  print(c)
  if b in d:
    asum+=d[b]*(c-b)
    if c in d:
      d[c]+=d[b]
    else:
      d[c]=d[b]
    d[b]=0
  print(asum)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

C = Counter(a)
s = sum(a)

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

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)



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

tot = sum(A)
cnt = [0] * 100001
for i in A:
    cnt[i] += 1


for l in B_C:
    cnt[l[1]] += cnt[l[0]]
    cnt[l[0]] = 0
    tot += cnt[l[0]] * (l[1] - l[0])
    print(tot)

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)



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

tot = sum(A)
cnt = [0] * 100001
for i in A:
    cnt[i] += 1

# print(cnt[:6])

for l in B_C:
    # print(l)
    cnt[l[1]] += cnt[l[0]]
    tot += cnt[l[0]] * (l[1] - l[0])
    print(tot)

Submission status: Wrong Answer

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

tot = sum(A)
cnt = [0] * 100001
for i in A:
    cnt[i] += 1


Q = int(input())

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
#N, K = map(int, input().split( ))
#L = list(map(int, input().split( )))
N = int(input())
A = list(map(int, input().split( )))
Q = int(input())
Sum = sum(A)
#Counterなるものがあるらしい
coun = Counter(A)
#Q個の整数の組(B,C)が与えられてB \to Cとして合計を計算
#合計は(Ci-Bi)*(A.count(Bi))変化する.
for i in range(Q):
  E= list(map(int, input().split( )))
  b, c = E[0], E[1]
  dis = c - b 
  Sum = Sum + dis*coun[b]
  print(Sum)
  coun[c] += coun[b]
  coun[b] == 0
  # replaceするとタイムエラーになるのか…
  #  A = [c if a == b else a for a in A]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]

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

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

Submission status: Wrong Answer

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

mappings = [ 0 for __ in range(10**5+1)]
answer = 0

for b, c in operations:
  mappings[b] = c

for num in nums:
  answer += mappings[num]

print(answer)

Submission status: Wrong Answer

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

L=[0]*(10**5+1)
S=0
for i in range(N):
    L[A[i]]+=1
    S+=A[i]

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

Submission status: Wrong Answer

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

L=[0]*(10**5+1)
B=dict(Counter(A))
for k in B.keys():
    L[k]=B[k]
# print(L)

sum = 0
for k in B.keys():
    sum += k*B[k]
for i in range(q):
    x=Q[i][0]
    y=Q[i][1]
    if L[x]==0:
        print(sum)
        continue
    L[y]+=L[x]
    L[x]=0
    sum=0
    for k in B.keys():
        sum+=k*L[k]
    print(sum)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()
q = INT()
count = [0]*(10**5+1)
for x in a:
    count[x] += 1
ans = 0
for i in range(n+1):
    ans += i * count[i]
for i in range(q):
    b, c = MAP()
    ans += (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    print(ans)

Submission status: Wrong Answer

Solution code:
s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
  l[A[i]] += 1
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  l[C] += l[B]
  if l[C] > 0:
    s.add(C)
  l[B] = 0
  s.discard(B)
  sum=0
  for i in s:
    sum += i * l[i]
  print(sum)

Submission status: Wrong Answer

Solution code:
s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
  l[A[i]] += 1
  s.add(A[i])
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  l[C] += l[B]
  s.add(C)
  s.discard(B)
  sum=0
  for i in s:
    sum += i * l[i]
  print(sum)

Submission status: Wrong Answer

Solution code:
s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
  l[A[i]] += 1
  s.add(A[i])
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  l[C] += l[B]
  s.add(C)
  s.discard(B)
  sum=0
  for i in s:
    sum += i * l[i]

Submission status: Wrong Answer

Solution code:
def freq(my_list):
    count = {} 
    for i in my_list: 
        count[i] = count.get(i, 0) + 1
    return count

n = int(input())
l = list(map(int, input().split()))
s,f = sum(l), freq(l)
for _ in range(int(input())):
    x, y = map(int, input().split())
    if x in f:
        v = f[x]
        if y in f:    f[y]+=v
        else:    f[y]=f.pop(x)
        s += (y-x)*v
    print(s)

Submission status: Wrong Answer

Solution code:
def freq(my_list):
    count = {} 
    for i in my_list: 
        count[i] = count.get(i, 0) + 1
    return count

n = int(input())
l = list(map(int, input().split()))
s,f = sum(l), freq(l)
for _ in range(int(input())):
    x, y = map(int, input().split())
    if x in f.keys():
        v = f[x]
        if y in f.keys():
            f[y]+=v
            f.pop(x)
        else:
            f[y]=f.pop(x)
        s += (y-x)*v
        print(s)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
A = [int(i) for i in input().split()]
all = sum(A)

Q = int(input())
bc = [[int(i) for i in input().split()] for _ in range(Q)]

nums = dict(Counter(A).items())
for i in range(Q):
    #print(nums)
    b, c = bc[i][0], bc[i][1]
    if b in nums.keys(): all += c * nums[b] - b * nums[b]
    print(all)

    if c in nums.keys() and b in nums.keys():
        nums[c] += nums[b]
    elif b in nums.keys():
        nums[c] = nums[b]
    else:
        nums[c] = 0
    nums[b] = 0

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

B=[]
C=[]
S=[]
for i in range(Q):
    b,c=map(int,input().split())
    num=A.count(b)
    if num!=0:
        for j in range(num):
            index_A=A.index(b)
            A[index_A]=c
   
    S.append(sum(A))

print(S)

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):
    b,c=map(int,input().split())
    B.append(b)
    C.append(c)

S=[]
for i in range(Q):
    num=A.count(B[i])
    for j in range(num):
        index_A=A.index(B[i])
        A[index_A]=C[i]
    S.append(sum(A))

print(S)

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)

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

N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]

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

for pair in BC:
    num_from = pair[0]
    num_to = pair[1]
    if num_from in c:
        diff = (num_to - num_from) * c[num_from]
        sum_A += diff
        print(sum_A)
        c[num_to] += c[num_from]
        c[num_from]=0
       

# def convert(a,mapping):
#     for i in range(N):
#         if a[i]==mapping[0]:
#             a[i]=mapping[1]
#     return a

# for i in range(Q):
#     A = convert(A,BC[i])
#     print(A.sum())



# def convert(n):
#     return d[n]

# d = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9}
# for i in range(Q):
#     for key, value in d.items():
#         if BC[i][0] == value:
#             d[key]=BC[i][1]
#     ans = np.array(list(map(convert,A)),dtype='int')
#     print(ans.sum())


# ans = list(map(convert,A))
# for item in ans:
#     print(item)
# print('\n'.join(str(ans)))

#A,B = zip(*AB)
#Ap = np.array(A)
#C = np.zeros(N + 1)

# if ans:
#     print('Yes')
# else:
#     print('No')

Submission status: Wrong Answer

Solution code:
from sys import stdin
input=stdin.readline
input()
a=list(map(int,input().split()))
ans=sum(a)
b=[0]*(max(a)+1)
y=max(a)+1
for i in a:
  b[i]+=1
c=int(input())
for i in range(c):
  n,m=map(int,input().split())
  if n<y and m<y:
    x=b[n]
    if x>0:
      b[m]+=x
      b[n]=0
      ans+=(m-n)*x
  print(ans)

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 l in range(q)]
cnt=[0]*100001
Sum=0
for i in range(n):
  cnt[a[i]]+=1
  Sum+=a[i]
for i in range(q):
  Sum+=(bc[i][1]-bc[i][0])*cnt[bc[i][1]]                           
  cnt[bc[i][1]]+=cnt[bc[i][0]]
  cnt[bc[i][0]]=0
  print(Sum)

Submission status: Wrong Answer

Solution code:
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    counter = collections.Counter(A)
    q = int(readline().rstrip())
    BCList = [list(map(int, readline().rstrip().split())) for _ in range(q)]
    aList = [0] * (10**5 + 10)
    sumA = sum(A)
    for i in range(n):
        aList[A[i] - 1] += 1

    for i in range(q):
        b, c = BCList[i][0], BCList[i][1]
        if aList[b - 1] == 0:
            continue
        sumA += (c - b) * aList[b - 1]
        aList[c - 1] += aList[b - 1]
        aList[b - 1] = 0
        print(sumA)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

counter = Counter(A)
sum_res = sum(A)

for _ in range(Q):
    bf,af = map(int,input().split())
    sum_res -= bf*counter[bf]
    sum_res += af*counter[af]
    
    counter[af] += counter[bf]
    counter[bf] = 0
    
    print(sum_res)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
q = int(input())
lst = [0] * q
for i in range(q):
    lst[i] = list(map(int, input().split()))
for i in range(q):

    pl = 0
    pr = len(A) - 1

    while True:
        pc = (pl + pr)//2
        if A[pc] == lst[i][0]:
            b = 0
            c = 0
            while A[pc+b] == lst[i][0]:
                A[pc+b] = lst[i][1]
                b += 1
                if pc + b == len(A):
                    break
            while A[pc+c] == lst[i][0]:
                A[pc+c] = lst[i][1]
                c -= 1
                if pc + c == 0:
                    break
        elif A[pc] < lst[i][0]:
            pl = pc + 1
        else:
            pr = pc -1
        if pl > pr:
            break
    
    print(sum(A))
    A = sorted(A)

Submission status: Wrong Answer

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

for _ in range(q):
    b,c=map(int,input().split())
    if b not in dic:continue
    ans += (c-b)*dic[b]
    if c not in dic:
        dic[c] = dic[b]
    else:
        dic[c]+=dic[b]
    dic[b]=0
    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):
    l = list(map(int, input().split()))
    # S = 0
    # for j in range(N):
    #     if A[j] == l[0]:
    #         A[j] = l[1]
        # S += A[j]
    S = sum([l[1] if A[j] == l[0] else A[j] for j in range(N)])
    print(S)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BL = []
CL = []
for _ in range(Q):
    B, C = map(int, input().split())
    BL.append(B)
    CL.append(C)

for i in range(Q):
    A = np.where(A == int(BL[i]), int(CL[i]), A)
    print(sum(A))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum1=sum(a)
d={}
for x in range(n):
  s=a[x]
  if s not in d:
    d[s]=0
  d[s]+=1

for y in range(q):
  h,g=map(int,input().split())
  if h in d:
    r=d[h]
    sum1=sum1-r*h
    
    if g not in d:
      d[g]=0
    d[g]+=r
    sum1+=r*g
    d[h]=0
    print(sum1)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

ans = sum(a)
a_c = Counter(a)

for i in range(q):
    b_num = a_c[b[i]]
    ans += (c[i] - b[i]) * b_num
    a_c[c[i]] += b_num
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
a = Counter(map(int, input().split()))

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

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

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

    count_A = [0]*(1000000)

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

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
result = 0
for i in a:
    result += i
for j in range(Q):
    b,c = map(int,input().split())
    while b in a:
        a[a.index(b)] = c
        result += c-b
        print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
change_rule = {}
for i in range(Q):
    B, C = map(int, input().split())
    change_rule[B] = C

for Bi in change_rule.keys():
    for j in range(N):
        if A[j] == Bi:
            A[j] = change_rule[Bi]
    print(sum(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
diff_BC = []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
    diff_BC.append(b-c)
sum_org = sum(A)

As = sorted(list(set(A)))
# 何が何個あるかdict
As_cnt = {}
for a in As:
    As_cnt[a] = A.count(a)

for i in range(Q):
    if B[i] in As_cnt.keys():
        t = As_cnt[B[i]]
        sum_org -= t*diff_BC[i]
        if C[i] in As_cnt.keys():
            As_cnt[C[i]] += t
        else:
            As_cnt[C[i]] = t
    print(sum_org)

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    d = copy.copy(a)
    sum = 0
    for j in range(len(a)):
        if d[j] == b:
            d[j] = c
            sum += d[j]
    print(sum)

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 i in range(q)]
num=[a.count(i+1) for i in range(10**5)]
sum=sum(a)
for i in range(q):
    x=bc[i][0]
    y=bc[i][1]
    sum+=(y-x)*num[x-1]
    num[y-1]+=a.count(i+1)
    num[x-1]-=a.count(i+1)
    print(sum)

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 i in range(q)]
num=[0]*(10**5)
sum=sum(a)
for i in range(q):
    x=bc[i][0]
    y=bc[i][1]
    c=a.count(x)
    ind=[j for j,m in enumerate(a) if m == x]
    sum+=(y-x)*(c+num[x-1])
    num[y-1]+=c
    num[x-1]-=c
    print(num)
    print(sum)

Submission status: Wrong Answer

Solution code:
import numpy as np
N=int(input())
A=list(map(int,input().split()))
SUM=sum(A)
Q=int(input())
index=np.array([i for i in range(10**5+1)])
List=np.array([0 for _ in range(10**5+1)])
for a in A:
    List[a]+=1
BList=np.array([0 for _ in range(10**5+1)])
for q in range(Q):
    B,C=map(int,input().split())
    BList[B]+=C-B
    print(np.sum(BList*List)+SUM)

Submission status: Wrong Answer

Solution code:
n = int(input())
l= list(map(int,input().split()))
c = [0] * (10 ** 5 + 1)
for i in l:
  c[i] += 1
n = int(input())
s = sum(c)
for i in range(n):
  be,af = map(int,input().split())
  s -= be * c[be]
  s += af * c[be]
  c[af] += c[be]
  c[be] = 0
  print(s)

Submission status: Wrong Answer

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

val = []
heavy = []

for x in A:
    if x in heavy:
        val[heavy.index(x)] += 1
    else:
        heavy.append(x)
        val.append(1)

for i in range(Q):
    B, C = input().split()
    B = int(B)
    C = int(C)
    if C not in heavy:
        heavy.append(C)
        val.append(0)
    if B in heavy:
        b = heavy.index(B)
        val[heavy.index(C)] += val[b]
        val[b] = 0
    else:
        print(sum)
    sum = 0
    for x, y in zip(val, heavy):
        sum += x * y
    print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = "".join(input().split())
q = int(input())
for i in range(q):
    b,c = input().split()
    a = a.replace(b,c)
    print(sum(map(int,a)))

Submission status: Wrong Answer

Solution code:
N = raw_input()
ns = raw_input()
ns = ns.split(' ')

data = {}
for i in ns:
    i = int(i)
    if i in data:
        data[i] += 1
    else:
        data[i] = 1

Q = raw_input()
opr = []
for _ in range(int(Q)):
    tmp = raw_input()
    tmp = tmp.split(' ')
    opr.append([int(tmp[0]), int(tmp[1])])

ans = 0
for k, v in data.iteritems():
    ans += k * v
for op in opr:
    if op[0] not in data:
        print ans
        continue

    old = data[op[0]]
    del data[op[0]]
    ans -= old * op[0]
    old2 = 0
    if op[1] in data:
        old2 = data[op[1]]
    data[op[1]] = old + old2
    ans += data[op[1]] * op[1]
    print ans

Submission status: Wrong Answer

Solution code:
N = raw_input()
ns = raw_input()
ns = ns.split(' ')

data = {}
for i in ns:
    i = int(i)
    if i in data:
        data[i] += 1
    else:
        data[i] = 1

Q = raw_input()
opr = []
for _ in range(int(Q)):
    tmp = raw_input()
    tmp = tmp.split(' ')
    opr.append([int(tmp[0]), int(tmp[1])])

ans = 0
for op in opr:
    if op[0] not in data:
        print ans
        continue
    ans = 0
    old = data[op[0]]
    del data[op[0]]
    old2 = 0
    if op[1] in data:
        old2 = data[op[1]]
    data[op[1]] = old + old2
    for k, v in data.iteritems():
        ans += k * v
    print ans