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:
from collections import Counter

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

L = Counter(a_L)
all_sum = sum(a_L)

print(0)

# ans_L = []
# for i in range(q):
#     b,c = map(int,input().split())
#     if L[b] != 0:
#         diff = (c-b) * L[b]
#         all_sum = all_sum+diff
#         L[c] += L[b]
#     ans_L.append(all_sum+1)


# for i in ans_L:
#     print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ind = [ [] for x in range(10**6)]

for i in range(n):
	ind[a[i]].append(i)

q = int(input())
s = 0
for v in a:
	s += v

for _ in range(q):
	b, c = map(int, input().split())
	ab = len(ind[b])
	if ab == 0:
		print(s)
		continue

	s += (ab*c)-(ab*b)
	for i in range(ab):
		ind[c].append(i)

	print(s)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = Counter(a_L)
print(0)
# all_sum = sum(a_L)



# ans_L = []
# for i in range(q):
#     b,c = map(int,input().split())
#     if L[b] != 0:
#         diff = (c-b) * L[b]
#         all_sum = all_sum+diff
#         L[c] += L[b]
#     ans_L.append(all_sum+1)


# for i in ans_L:
#     print(i)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=input()
A=A.replace(' ','')
q=int(input())
su=[]
for i in range(q):
    s=0
    b,c=map(str,input().split())
    A=A.replace(b,c)
    for i in range(len(A)):
        s+=int(A[i])
    su.append(s)
for i in su:
    print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np
s = list(map(int, input().split()))
n=s[0]
a = np.array(list(map(int, input().split())))
s = list(map(int, input().split()))
q=s[0]
plog={}
for i in range(q):
    s = list(map(int, input().split()))
    plog[(s[0],s[1])]=1
for s in plog:
    a=np.where(a==s[0],s[1],a)
    print(np.sum(a))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=int(input())
for j in range(m):
  a,b=map(int,input().split())
  for i in l:
    if i==a:
      i=b
  print(sum(l))

Submission status: Wrong Answer

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

q = int(input())
ans = sum(arr)

hashmap = {}
for val in arr:
    if val in hashmap:
        hashmap[val] += 1
    else:
        hashmap[val] = 1


while q > 0:
    print(hashmap)
    [x, y] =  [int(x) for x in input().split()]
    if x in hashmap:
        if y in hashmap:
            hashmap[y] += hashmap[x]
            ans -= hashmap[x]*x
            ans += y*hashmap[x]
        else:
            diff = hashmap[x] * x
            ans -= diff
            hashmap[y] = hashmap[x]
            del hashmap[x]
            add = hashmap[y] * y
            ans += add
        
    print('ans', ans)
    q -=1

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
A=list(map(int,input().split()))
Adic={}
for i in A:
  if Adic.get(i)==None:
    Adic[i]=1
  else:
    Adic[i]+=1
Q=int(input())
ope=[]

for i in range(Q):
  ope.append(list(map(int,input().split())))

A=np.array(A)

sum=0
for key,value in Adic.items():
  sum+=key*value

for b,c in ope:
  if Adic.get(b)!=None:
    if Adic.get(c)==None:Adic[c]=0
    sum=sum-b*Adic[b]+c*Adic[c]
    Adic[c]+=Adic.pop(b)
    print(sum)
    
  else:
    print(sum)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
k={}
ans=sum(A)
for i in A:
  if i in k:
    k[i]+=1
  else:
    k[i]=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  if b in k:
    f=c*k[b]-b*k[b]
    ans+=f
    del k[b]
    if c in k:
      k[c]+=1
    else:
      k[c]=1
  print(ans)

Submission status: Wrong Answer

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

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

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

print()
for i in range(Q):
    if BC[i][0] not in A:
        continue
    else:
        for j in range(len(A)):
            if A[j]==BC[i][0]:
                A[j]=BC[i][1]
    print(sum(A))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = Counter(a_L)
#all_sum = sum(a_L)
all_sum = 0


ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    if L[b] != 0:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    ans_L.append(all_sum+1)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A.sort()
    ans = sum(A)
    for i in range(q):
        b,c = map(int, input().split())
        count = 0
        for j,a in enumerate(A):
            if a == b:
                A[j]=c
                count += 1
            elif a > b:
                break
        ans += (c-b)* count
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
Q = INT()
L = [LIST() for i in range(Q)]
class UnionFind():
    def __init__(self, n):
        self.n = n
        # parents[i]: 要素iの親要素の番号
        # 要素iが根の場合、parents[i] = -(そのグループの要素数)
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    # 要素xが属するグループの要素数を返す
    def size(self, x):
        return -self.parents[self.find(x)]
    def same(self, x, y):
        return self.find(x) == self.find(y)
    # 要素xが属するグループに属する要素をリストで返す
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    # 全ての根の要素をリストで返す
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
    # グループの数を返す
    def group_count(self):
        return len(self.roots())
    # 辞書{根の要素: [そのグループに含まれる要素のリスト], ...}を返す
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
    # print()での表示用
    # all_group_members()をprintする
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
# tree = UnionFind(10**5)
# for i in range(N):
#     a, b = L[i]
graph = defaultdict(list)

for a, b in L:
    # tree.union(b-1, a-1)
    graph[b].append(a)
    for c in graph[a]:
        if c not in graph[b]:
            graph[b].append(c)
    graph[a] = []

    gra2 = defaultdict(int)
    for g, s in graph.items():
        for i in s:
            gra2[i] = g
# for k, v in c.items():
#     if graph[]
    # print(gra2)
    ans = 0
    c = Counter(A)

    for k, v in c.items():
        if gra2[k] == 0:
            ans += k
        else:
            ans += gra2[k] * v
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

def main():
    input = sys.stdin.readline
    n = int(input())
    a = list(map(str, input().strip().split()))
    q = int(input())
    bc = [map(str, input().split()) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]

    for i in range(q):
        aa = [n.replace(b[i],c[i]) for n in a]
        print(sum(list(map(int, aa))))
        

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ans = 0
for _ in range(q):
    b, c = map(int, input().split())
    if b in A:
        A = [c if a==b else a for a in A]
        ans = sum(A)
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
a,*b=map(int,input().split())
l.append(a)
for i in range(len(b)):
    l.append(b[i])
q=int(input())
b=[]
c=[]
for i in range(q):
    y,z=map(int,input().split())
    b.append(y)
    c.append(z)
for j in range(len(b)):
    for k in range(len(l)):
        if b[j]==l[k]:
            l.remove(l[k])
            l.insert(k,c[j])
            print(sum(l))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = Counter(a_L)
all_sum = sum(a_L)

ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    if L[b] != 0:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    ans_L.append(all_sum+1)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
A=list(map(int,input().split()))
Adic={}
for i in A:
  if Adic.get(i)==None:
    Adic[i]=1
  else:
    Adic[i]+=1
Q=int(input())
ope=[]

for i in range(Q):
  ope.append(list(map(int,input().split())))

A=np.array(A)

sum=0
for b,c in ope:
  if Adic.get(b)!=None:
    if Adic.get(c)==None:Adic[c]=0
    Adic[c]+=Adic.pop(b)
    sum=0
    for key,value in Adic.items():
      sum+=key*value
    print(sum)
  else:print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = {}
total = 0
for i in a:
  total += i
  d[i] = d.get(i, 0) + 1

#クエリによって、それぞれの数値の個数を変更し、
#対象となる個数×差分で合計を修正し出力する

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

Submission status: Wrong Answer

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

import bisect

n = I()
a = LI()
a.sort()
q = I()
val = sum(a)
for i in range(q):
    b,c = LI()
    l = bisect.bisect_left(a,b)
    r = bisect.bisect_right(a,b)
    print(l,r)
    val += (c-b)*(r-l)
    for j in range(l,r):
        a[j] = c
    a.sort()
    print(val)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
numl = [0] * 100005
resl = []
suma = sum(a)
for i in range(n):
    numl[a[i]]+=1
for i in range(q):
    b, c = map(int, input().split())
    bn = numl[b]
    suma += (c - b)* bn
    resl.append(suma)
    numl[c] += bn
for i in resl:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import Counter
from numba import jit

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit('void(i8, i8[:], i8[:], i8, i8[:])')
def solve(n, a, arr, q, bc):
    for k, v in Counter(a).items():
        arr[k] = v

    q = int(input())

    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
l=[]
for _ in range(Q):
    l.append(list(map(int,input().split())))
c = collections.Counter(A)
sum=sum(A)
for i in range(Q):
    print(c)
    if c[l[i][0]] == 0:
        print(sum)
        continue
    else:
        sum+=c[l[i][0]]*(l[i][1]-l[i][0])
        c[l[i][1]]+=c[l[i][0]]
        del c[l[i][0]]
        print(sum)

Submission status: Wrong Answer

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

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]
    
    if B in given_list:
        given_list_replased = sorted(given_list)
        for i in range(N):
          if given_list_replased[i] == B:
              given_list_replased[i] = C
          elif int(given_list_replased[i]) > int(C):
              break
        given_list_replased = [int(i) for i in given_list_replased]  
        print(sum(given_list_replased)) #####          
    else:
      continue
    
    given_list = [str(i) for i in given_list_replased]

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

q = int(input())

L = Counter(a_L)
all_sum = sum(a_L)

ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    if L[b] != 0:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    ans_L.append(all_sum)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

Solution code:
def _print_sum(cache):
    sum_value = 0
    for v, indices in cache.items():
        sum_value += v * len(indices)
    print(sum_value)


def _main(N, A_list, Q, BC_list):
    cache = {}
    values = set()
    for i, a in enumerate(A_list):
        cache.setdefault(a, []).append(i)
        values.add(a)

    for b, c in BC_list:
        indices = cache.get(b, [])
        cache[b] = []
        cache.setdefault(c, []).extend(indices)
        _print_sum(cache)


def _entry():
    N = int(input())
    A_list = list(map(int, input().split()))
    Q = int(input())
    BC_list = []
    for i in range(Q):
        B, C = map(int, input().split())
        BC_list.append((B, C))
    _main(N, A_list, Q, BC_list)

    if __name__ == "__main__":
        _entry()

Submission status: Wrong Answer

Solution code:
from math import floor,ceil,sqrt
import fractions
from collections import deque
import itertools
from collections import deque
import collections
import heapq
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
q=[list(map(int,input().split())) for _ in range(Q)]
nums={}
su=sum(A)
for i in range(N):
    if nums.get(A[i])==None:
        nums[A[i]]=1
    else:
        nums[A[i]]=nums[A[i]]+1
#print(nums)
for i in range(Q):
    b,c=q[i]
    bnum=nums.pop(b,None)
    cnum=nums.pop(c,None)
    #print(anum,bnum)
    if bnum!=None:
        if cnum!=None:
            su=su+(c-b)*bnum
            nums[c]=cnum+bnum
            print(su)
        else:
            su = su + (c - b) * bnum
            nums[c]=bnum
            print(su)
    else:
        print(su)
    #print(nums)

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(' '))
    a=[c if x==b else x for x in a]
    print(a)
    print(sum(a))

Submission status: Wrong Answer

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

        ans = []

        temp_A = A

        for i in range(Q):
                temp_s = [s.replace(str(BC[i][0]),str(BC[i][1])) for s in temp_A]
                temp_i = [int(s) for s in temp_s]
                ans.append(sum(temp_i))
                temp_A = temp_s

        return ans

if __name__=="__main__":
        ans = main()
        print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    q=int(input())
    qu=[]
    pr=[]
    for i in range(q):
        qu.append(list(map(int,input().split(' '))))
    for i in range(q):
        if qu[i][0] not in a:
            continue
        for j in range(n):
            if a[j]==qu[i][0]:
                a[j]=qu[i][1]
        pr.append(str(sum(a)))
    print("\n".join(pr))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
input()
a=list(map(int,input().split()))
b=int(input())
for i in range(b):
  c,d=map(int,input().split())
  a=[d if j==c else j for j in a]
  print(' '.join(map(str, a)))

Submission status: Wrong Answer

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

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1


BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in dictA.keys():
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        dictA.pop(x[0])
        # print(dictA)

    sumA = 0
    # for k, v in dictA.items():
    #     sumA += k * v
    print(sumA)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    from collections import Counter
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    Q = int(input())
    ans = sum(A)
    for _ in range(Q):
        b,c = map(int, input().split())
        x = C[b]
        ans += (c-b) * x
        C[c] += x
        print(ans)



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

total = sum(A)

for i in range(Q):
    if(B[i] in A):
        count = A.count(B[i])

        d = C[i] - B[i]
        total += count * d
        print(total)

        A = [C[i] if x == B[i] else x for x in A]

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
  k = list(map(int, input().split()))
  print(sum([int( str(s).replace( str(k[0]),str(k[1]) ) ) for s in A]))

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
g = [0] * (10**5 + 1)
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())
BC = [list(map(int, input().split())) for i in range(Q)]
# print(N)
# print(A)
# print(Q)
# print(B)

total = sum(A)

for i in range(Q):
    Bi = BC[i][0]
    Ci = BC[i][1]

    diff = 0
    for j in range(len(A)):
        if A[j] == Bi:
            diff += A[j] - Ci
            A[j] = Ci

    total += diff
    print(total)

Submission status: Wrong Answer

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

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

    d = dict()

    # dictに格納 O(1)
    for a in A:
        try:
            d[a] += 1
        except KeyError:
            d[a] = 1
    
    ans = 0

    # 一旦ans出す
    for dat in d.items():
        ans += dat[0]*dat[1]

    # B,Cの操作
    for bc in BC:
        try:
            d[bc[1]] += d[bc[0]]
        except KeyError:
            try:
                d[bc[1]] = d[bc[0]]
            except KeyError:
                d[bc[0]] = 0
                d[bc[1]] = 0
        finally:
        # 増減分だけ変更
            ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
            d[bc[0]] = 0
        print(ans)

main()

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict

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

ans = sum(nums)
d = Counter(nums)

for query in queries:
	k = d[query[0]]
	if k >= 1:
		ans += (query[1] - query[0]) * k
		d[query[1]] += k
	print(ans)

Submission status: Wrong Answer

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

S = sum(List)

arr = [0 for i in range(10**5+1)]

for i in range(N):
  a = List[i]
  arr[a] += 1
  
for i in range(Q):
  a,b = map(int,input().split())
  aa = arr[a]
  arr[b] += aa
  S += int ( (b-a) * aa )
  print(int(S))

Submission status: Wrong Answer

Solution code:
N=int(input())
lst=list(map(int,input().split()))[:N]
Q=int(input())
for i in range(Q):
    x,y=map(int,input().split())
    lst.sort()
    for i in range(len(lst)):
        if lst[i]==x:
            lst[i]=y
        else:
            break
    print(sum(lst))

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

print(BC)

for i in range(Q):
    for j in range(N):
        if A[j] == BC[i][0]:
            A[j] = BC[i][1]
    print(sum(A))

Submission status: Wrong Answer

Solution code:
input()
a=list(map(int,input().split()))
b=int(input())
for i in range(b):
  c,d=map(int,input().split())
  a=[d if j==c else j for j in a]
print(' '.join(map(str, a)))

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
#a,b = map(int,input().split())
a_L = list(map(int,input().split()))

q = int(input())

L = Counter(a_L)
all_sum = sum(a_L)

ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    if L[b] != 0:
        #print(b,c,L[b])
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        ans_L.append(all_sum)
        L[c] += L[b]
    else:
        ans_L.append(all_sum)


L[1] += 1

for i in ans_L:
    print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = input().split()
np_a=np.array(a, dtype=int)

sum=np.int(sum(np_a))
count=np.zeros(100001, dtype='int64')
for i in range(0,n):
  count[np_a[i]]+= 1
  
q = int(input())
for i in range(0,q):
  b, c= map(int, input().split())
  count[c]=count[c]+count[b]
  count[b]=0
  sum=sum+(c-b)*count[b]
  
  print (sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
x = sum(a)
ans = [0]*q
for i in range(q):
    b, c = map(int, input().split())
    num_b = a.count(b)
    x -= num_b * b
    x += num_b * c
    ans[i] = x
for i in range(q):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N = 4
ARR = [1, 1, 1, 1]
Q = 3
SRR = [
    [1, 2],
    [2, 1],
    [3, 5]
]


N = 4
ARR = [1, 2, 3, 4]
Q = 3
SRR = [
    [1, 2],
    [3, 4],
    [2, 4],
]



N = 2
ARR = [1, 2]
Q = 3
SRR = [
    [1, 100],
    [2, 100],
    [100, 1000]
]

N = int(input())
ARR = list(map(int,input().split()))
Q = int(input())
SRR = []

for i in range(Q):
    SRR.append(list(map(int,input().split())))


def calculate(n, arr, q, srr):
    dict = {}
    sumValue = 0
    for i in range(n):
        value = arr[i]
        sumValue += value

        if dict.get(value) == None:
            dict.__setitem__(value, 1)
        else:
            dict.__setitem__(value, dict.get(value) + 1)

    for i in range(q):
        fromValue = srr[i][0]
        toValue = srr[i][1]

        if dict.get(fromValue) == None:
            print(sumValue)
        else:
            fromNum = dict.get(fromValue)
            dict.setdefault(fromValue, 0)
            sumValue += fromNum * (toValue - fromValue)
            if dict.get(toValue) == None:
                dict.__setitem__(toValue,fromNum)
            else:
                dict.__setitem__(toValue,dict.get(toValue) + fromNum)
            print(sumValue)


calculate(N, ARR, Q, SRR)

Submission status: Wrong Answer

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

for i in range(Q):
    B, C = map(str, input().split())
    if B not in A:
        continue
    else:
        Ast = ",".join(A)
        Ast_new = Ast.replace(B, C)
        A = Ast_new.split(',')
        INT = list(map(int, A))                
        print(sum(INT))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input()
q = int(input())
l = [list(map(str, input().split())) for _ in range(q)]

for i in range(q):
    if l[i][0] in a:
        a = a.replace(l[i][0], l[i][1])
        new_a = a.split()
        new_a = [int(j) for j in new_a]
        print(sum(new_a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
line = list(map(int, input().split()))
q = int(input())
for i in range(0, q):
	b, c = map(int, input().split())
	line = [c if j is b else j for j in line]
	print(sum(line))

Submission status: Wrong Answer

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

List = [0]*100000
for i in A:
  List[i-1] += 1
  ans += i

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())
    #print(A_list)
    ans_list = []
    for i in range(Q) :
        val_list = my_index_multi(A_list,B[i])
        #print(val_list)
        for j in val_list :
            #print(A_list[j])
            #print(B[i])
            #print(C[i])
            A_list[j] = C[i]
            #A_list[j].replace(str(B[i]), str(C[i]))
        #print(A_list)
        print(sum(A_list))

Submission status: Wrong Answer

Solution code:
import collections
def change_dict_key_exist_setdefault(d, old_key, new_key):
    if old_key in d:
        tmp = d[new_key]
        d.setdefault(new_key, d.pop(old_key))
        d[new_key] += tmp
        #print(d)
N = int(input())
A_li = list(map(int,input().split()))
ans = sum(A_li)
c = collections.Counter(A_li)
#print(c)
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    ans -= c[B] * (B-C)
    print(ans)
    change_dict_key_exist_setdefault(c,B,C)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = input().split()
np_a=np.array(a, dtype=int)

sum=np.int(sum(np_a))
count=np.zeros(100001, dtype='int64')
for i in range(0,n):
  count[np_a[i]]+= 1
  
change=np.array(range(100001))

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

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
g = [0] * (10**5 + 1)
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())
    s += g[b] * c
    g[c] += g[b]
    g[b] == 0
    print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
ans = []
for x in range(Q):
    B, C = [int(x) for x in input().split()]
    A = [x if x != B else C for x in A]
    ans.append(sum(A))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    A = np.array(A)

    SUM = []
    for i in range(Q):
        B, C = list(map(int, input().split()))
        if i == 0:
            A = np.where(A == B, C, A)
            SUM.append(sum(A))
        else:
            A = np.where(A == B, C, A)
            SUM.append(sum(A))
        print(A)

    for s in SUM:
        print(s)

Submission status: Wrong Answer

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

S = sum(List)

arr = [0 for i in range(10**5+1)]

for i in range(N):
  a = List[i]
  arr[a] += 1
  
for i in range(Q):
  a,b = map(int,input().split())
  aa = arr[a]
  arr[b] += aa
  S = (b-a) * aa + S
  print(int(S))