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:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
x=[0]*10**6
sum_a=sum(a)
for _ in range(q):
  b,c=list(map(int,input().split()))
  tmp=x[c]
  x[c]=a.count(b)#今hitした数
  sum_a+=(c-b)*(x[c]+x[b])
  x[c]+=tmp
#  print(x)
  print(sum_a)

Submission status: Wrong Answer

Solution code:
n = int(input())
list = input().split()
for a in range(int(input())):
    b, c = input().split()
    list = map((lambda x: x.replace(b, c)), list)
    list_int = map((lambda x: int(x)), list)
    print(sum(list_int))

Submission status: Wrong Answer

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

l.sort()
m = []
count = [0 for i in range(Q)]
flag = [0 for i in range(Q)]

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

for i in range(Q):
    for j in range(N):
        if l[j] == m[i][0]:
            count[i] += 1
        elif l[j] > m[i][0]:
            break


for i in range(Q):
    print(i)
    for j in range(i-1,-1,-1):
        if m[j][1] == m[i][0]:
            if flag[j] == 0:
                count[i] += count[j]
                flag[j] = 1

ans = sum(l)
for i in range(Q):
    ans += (m[i][1]-m[i][0])*count[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
#input = raw_input
n = int(input())
As = list(map(int, input().split(" ")))

counter = Counter(As)
cur_sum = sum(As)


q = int(input())
bs = []
cs = []
for i in range(q):
    b, c = map(int, input().split(" "))
    bs.append(b)
    cs.append(c)

set_bc = set(bs) | set(cs)
sa = set_bc - set(list(counter.keys()))
#print("sa", sa)

for key in sa:
    counter[key] = 0
#print(counter)

for i in range(q):
    b = bs[i]
    c = cs[i]

    num1 = counter[b]
    counter[c] += num1

    diff = (c - b) * num1

    cur_sum += diff
    print(cur_sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for num, rep in zip(b, c):
    if num in set(a):
        a = [rep if ele == num else ele for ele in a]
    else:
        continue
    print(sum(a))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
s = sum(A)
d = defaultdict(int)
for key in A:
    d[key] += 1
d[s] = s

Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    change_sum = d[b] * (c-b)
    d[s] += change_sum
    d[c] += d[b]
    d.pop(b)
    print(d[s])

Submission status: Wrong Answer

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

L = list(map(int,input().split()))
sum_all = sum(L)
L = Counter(L)
#print(L)
q = int(input())
sum1 = sum_all
for i in range(q):
    
    a,b = map(int,input().split())
    #print(L)
    if a not in L:
        L[b] = 0
    else:
        L[b] +=L[a] 
    #print(L)
    sum1 = sum1 + b*L[a] - a*L[a]
    #L[a] =0
    
    #print(L)
    print(sum1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
x=[0]*10**6
sum_a=sum(a)
for _ in range(q):
  b,c=list(map(int,input().split()))
  x[c]=a.count(b)#今hitした数
  sum_a+=(c-b)*(x[c]+x[b])
  x[c]+=a.count(b)
#  print(x)
  print(sum_a)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int,input().split()))
t = int(input())
while t:
    t-=1
    k,r = map(int,input().split())
    arr.sort()
    if k in arr:
        for i in range(n):
            if arr[i]>k:
                break
            if arr[i]==k:
                arr[i]=r
            
    print(arr)
    print(sum(arr))

Submission status: Wrong Answer

Solution code:
#D - Replacing
import collections
N= int(input())
A= list(map(int,input().split()))
Q = int(input())
B = [list(map(int,input().split())) for i in range(Q)]
DP = []
A.sort()
sub = 0
ans = sum(A)

for i in range(Q):
    if A.count(B[i][0]) > 0 and DP.count(B[i][0]) == 0:
        #for j in range(A.count(B[i][0])):
        ans = ans +(B[i][1] - B[i][0])*A.count(B[i][0])
        DP.append(B[i][0])
        for j in range(N):
            if A[j] == B[i][0]:
                A[j] = B[i][1]
            else:
                pass
        print(ans)
    else:
        print(ans)

Submission status: Wrong Answer

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

# _INPUT = """\
# 1
# 1
# 3
# 1 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 )

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

# print(d)

sumA = sum(A)

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

  if b==c:
    print(sumA)
    continue

  if d[b] == 0:
    print(sumA)
    continue

  if d[c] == 0:
    sumA += (c-b) * d[b]
    print(sumA)
    d[c] += d[b]
    d[b] = 0
    continue

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

BC = []
i = 0
for i in range(Q):
    BC.append(input().split())

j = 0
k = 0
l = 0
S = 0
for j in range(Q):
    for k in range(N):
        if A[k] == int(BC[j][0]):
            A[k] = int(BC[j][1])
    print(A)
    for l in range(N):
        S = S + A[l]
    print(S)
    S = 0

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().replace(' ', '')
S = A
Q = int(input())
S_sum = 0

for _ in range(Q):
    B, C = map(str, input().split())
    S = S.replace(B, C)
    for s in list(S):
        S_sum += int(s)
    else:
        print(S_sum)
        S_sum = 0

Submission status: Wrong Answer

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split())) for i in range(Q)]
a_list=sorted(a_list)
for i in range(Q):
    if B[i][0] in a_list:
        for j in range(N):
            if a_list[j]==B[i][0]:
                a_list[j]=B[i][1]
                break
    print(sum(a_list))

Submission status: Wrong Answer

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

Line = [int(s) for s in input().split()]
c = collections.Counter(Line)

# print(c)

Q = int(input())


for _ in range(Q):
    Line = [int(s) for s in input().split()]
    try:
        c[Line[1]] += c[Line[0]]
        c.pop(Line[0])
    except:
        print('Error')
    print(c)
    
    s=0
    for i in c:
        # print(i)
        s += i * c[i]
    print(s)

Submission status: Wrong Answer

Solution code:
import numpy as np

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(0, len(a)):
    #     if b == a[j]:
    #         a[j] = c
    print(sum(np.where(a == B, C, a)))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=str(input())
Q=int(input())
BC=[list(input().split()) for _ in range(Q)]
B_i=[0]*N
for i in range(Q):
    if BC[i][0] in set(A):
        A=A.replace(BC[i][0],BC[i][1])
        B_i = [int(s) for s in A.split(' ')]
    print(sum(B_i))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
from decimal import *

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()
	Q = inputInt()

	A.sort()
	#seg = SegTree(A, segfunc, ide_ele)

	for i in range(Q):
		B,C = inputMap()
		l = bisect_left(A, B)
		r = bisect_right(A, B)

		for i in range(l,min(N,r+1)):
			A[i] = C
		A.sort()
		print(sum(A))


#####segfunc#####
def segfunc(x, y):
    return x + y
#################

#####ide_ele#####
ide_ele = 0
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

npdata = np.arange(10**5+1)

kosuu=np.zeros(10**5+1)
kosuumoto=kosuu
for i in range(N):
    a=A[i]
    kosuu[a]+=1
SUM=sum(A)

BC_list=[]

for i in range (Q):
    bc=list(map(int,input().split()))
    BC_list.append(bc)
S=SUM

for i in range(Q):
    b=BC_list[i][0]
    c=BC_list[i][1]
    minus=b*kosuu[b]
    kosuu[c]=kosuu[c]+kosuu[b]
    plus=c*kosuu[c]
    kosuu[b]=0
    S=S-minus+plus

    print(int(S))

Submission status: Wrong Answer

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

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

for i, j in l.items():
    for x, k in enumerate(a):
        if i == k:
           a[x] = j
    print(sum(a))

Submission status: Wrong Answer

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    b, c = trials[0]
    for i in range(num):
        seq = [e if e != b else c for e in seq]
        print(sum(seq))
        if i == num - 1:
            break
        b, c = trials[i+1]
        if b not in seq:
            print(sum(seq))
            break



if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
Query = [tuple(map(int, input().split())) for _ in range(Q)]
from collections import defaultdict
cntr = defaultdict(int)
for a in A:
    cntr[a] += 1
keys = [x for x in range(10**5+1)]
mp = dict([(x,[x]) for x in range(10**5+1)])
for b, c in Query:
    mp[c].append(b)
    for k in mp[b]:
        keys[k] = c
    ans = 0
    for k, v in cntr.items():
        ans += keys[k] * v
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
dicy = {}
def swap(a,b):
	tmp = 0
	try:
		tmp = dicy.pop(a)
	except Exception:
		tmp = 0

	try:
		dicy[b] += tmp
	except Exception:
		dicy[b] = tmp

	return (int(b)-int(a)) * tmp
  

max = input()
dicy = dict(Counter(input().split(' ')))
result = 0
for v,k in dicy.items():
	result += int(v)*int(k)
print(result)
num_oper = int(input())
for i in range(num_oper):
	a,b = input().split(' ')
	diff = swap(a,b)
	result += diff
	print(result)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = [int(s) for s in input().split()]
Q = int(input())

d = Counter(A)
total = sum(A)

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

Submission status: Wrong Answer

Solution code:
a = int(input())
sequence = list(map(int, input().split(" ")))
q = int(input())
dictionary = {}
for i in range(q):
    pair = list(map(int, input().split(" ")))
    dictionary[pair[0]] = pair[1]

for key in dictionary:
    Sum = 0
    for n in range(len(sequence)):
        if key==sequence[n]:
            sequence[n]=dictionary[key]          
    print(sum(sequence))

Submission status: Wrong Answer

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

N = int(input())

AAA = list(map(int, input().split()))

Q = int(input())

goukei_AAA = sum(AAA)

for i in range(0, Q):
    BC = list(map(int, input().split()))
    if not BC[0] in AAA:
        print(goukei_AAA)
        pass
    else:
        AAA = [BC[1] if m == BC[0] else m for m in AAA]
        goukei_AAA = goukei_AAA + (AAA.count(BC[0]) * (BC[1] - BC[0]))
        print(goukei_AAA)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = map(int, input().split())
a = np.array(A)

Q = int(input())
for i in range(Q):
    B,C = map(int, input().split())
    a = np.where(a == B, C , a)
    print(np.sum(a))

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
q = ni()
l = [na() for _ in range(q)]

import collections

c = collections.Counter(a)
ans = sum(a)
for i in l:
  if c[i[0]] >0:
    ans = ans - c[i[0]]*i[0] + i[1]*(c[i[0]])
    c[i[1]] = c[i[1]] + c[i[0]]
    c[i[0]] = 0
    print(ans)

Submission status: Wrong Answer

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

if __name__ == '__main__':
  n=int(input())
  A_str=input().strip("\n")
  A=A_str.split()
  q=int(input())

  C={}
  for a in A:
    if a in C:
      C[a]+=1
    else:
      C[a]=1
  
  def re(b,c):
    if c in C:
      C[c]+=C[b]
    else:
      C[c]=C[b] 
    C[b]=0
    #print(b,c,C)

  ans=''
  b,c=input().strip("\n").split()
  tmp=eval(A_str.replace(b,c).replace(" ","+"))
  ans+=str(tmp)
  ans+="\n"
  if b in C:
    re(b,c)
  for i in range(1,q-1):
    b,c=input().strip("\n").split()
    if b in C:
      tmp=tmp+(int(c)-int(b))*C[b]
      re(b,c)
    ans+=str(tmp)+"\n"
  b,c=input().strip("\n").split()
  if b in C:
    tmp=tmp+(int(c)-int(b))*C[b]
  ans+=str(tmp)
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
input = raw_input
n = int(input())
As = map(int, input().split(" "))

counter = Counter(As)
cur_sum = sum(As)


q = int(input())
bs = []
cs = []
for i in range(q):
    b, c = map(int, input().split(" "))
    bs.append(b)
    cs.append(c)

set_bc = set(bs) | set(cs)
sa = set_bc - set(counter.keys())

for key in sa:
    counter[key] = 0

for i in range(q):
    b = bs[i]
    c = cs[i]

    num1 = counter[b]
    counter[c] += num1

    diff = (c - b) * num1
    cur_sum += diff
    print(cur_sum)

Submission status: Wrong Answer

Solution code:
#D - Replacing
import collections
N= int(input())
A= list(map(int,input().split()))
Q = int(input())
B = [list(map(int,input().split())) for i in range(Q)]

A.sort()
sub = 0
ans = sum(A)

for i in range(Q):
    if A.count(B[i][0]) > 0:
        #for j in range(A.count(B[i][0])):
        ans = ans +(B[i][1] - B[i][0])*A.count(B[i][0])
        for j in range(N):
            if A[j] == B[i][0]:
                A[j] = B[i][1]
            elif A[j] > B[i][0]:
                break
            else:
                pass
        print(ans)

    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
for i in range(q):
  a.sort()
  su = 0
  b,c = map(int,input().split())
  for j in range(n):
    if(a[j] > b ):
      break
    if(a[j] == b):
      a[j] = c
    su += a[j]
  print(su)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=[' ' for _ in range(11)]
ans_=[]
letter=[' ','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']
while n:
  new=n%26
  if new==0:
    new=26
    n+=-26
  ans_.append(new)  
  n=int(n/26)  
ans_=ans_[::-1]
for i in range(len(ans_)):
  ans[i]=letter[ans_[i]]
print(''.join(ans))

Submission status: Wrong Answer

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


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


sumA = sum(alist)
adict = Counter(alist)

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

    bnum = adict[b]
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Submission status: Wrong Answer

Solution code:
n = int(input())
als = list(map(int,input().split()))
ans = sorted(als)
q = int(input())
for i in range(q):
    b, c = map(int,input().split())
    if b in ans:
        cot = ans.count(b)
        ans =[ans[j] if ans[j] != b else c for j in range(n)]        
        for j in range(cot):
            ans.append(c)
    print(sum(ans))

Submission status: Wrong Answer

Solution code:
N = int(input()) #ここで標準入力
A = list(map(int, input().split()))
Q= int(input()) #ここで標準入力


list = []
for i in range(Q):
    a,b=input().split()
    list.append((int(a), int(b)))

for k in range(Q):
    for j in range(N):
        if list[k][0] == A[j]:
            A.remove(list[k][0])
            A.append(list[k][1])
        
    print(sum(A))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import collections
def find(tuple_list, num, top_idx):
    length = len(tuple_list)

    if length == 0:
        return top_idx, False

    if length == 1:
        hikaku = tuple_list[0][0]
        if num == hikaku:
            return top_idx, True
        elif num < hikaku:
            return top_idx-1, False
        elif num > hikaku:
            return top_idx, False
    
    if length == 2:
        hikaku1 = tuple_list[0][0]
        hikaku2 = tuple_list[1][0]
        if num == hikaku1:
            return top_idx, True
        elif num == hikaku2:
            return top_idx+1, True
        elif num < hikaku1:
            return top_idx-1, False
        elif hikaku1 < num < hikaku2:
            return top_idx, False
        elif hikaku2 < num:
            return top_idx+1, False

    place = int(length/2)
    hikaku = tuple_list[place][0]
    if num == hikaku:
        return top_idx+place, True
    elif num < hikaku:
        return find(tuple_list[:place], num, top_idx)
    elif num > hikaku:
        return find(tuple_list[place+1:], num, top_idx+place+1)


N = int(input())
A = list(map(int, input().split()))
A_amount = list(collections.Counter(A).items())
A_amount = list(sorted(A_amount, key=lambda x:x[0]))

result = sum(A)

Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
    B_idx, is_B_exist = find(A_amount, B, 0)
    if is_B_exist:
        old_tuple = A_amount[B_idx]
        C_idx, is_C_exist = find(A_amount, C, 0)
        if is_C_exist:
            new_tuple = (C, A_amount[B_idx][1]+A_amount[C_idx][1])
            A_amount[C_idx] = new_tuple
            A_amount_before = A_amount[:B_idx]
            A_amount_after = A_amount[B_idx+1:]
            A_amount_before.extend(A_amount_after)
            A_amount = A_amount_before
        else:
            new_tuple = (C, A_amount[B_idx][1])
            if B_idx < C_idx:
                A_amount_1 = A_amount[:B_idx]
                A_amount_2 = A_amount[B_idx+1:C_idx+1]
                A_amount_3 = A_amount[C_idx+1:]
                A_amount_1.extend(A_amount_2)
                A_amount_1.append(new_tuple)
                A_amount_1.extend(A_amount_3)
            else:
                A_amount_1 = A_amount[:C_idx+1]
                A_amount_2 = A_amount[C_idx+1:B_idx]
                A_amount_3 = A_amount[B_idx+1:]
                A_amount_1.append(new_tuple)
                A_amount_1.extend(A_amount_2)
                A_amount_1.extend(A_amount_3)
            A_amount = A_amount_1
    
        result += (C-B)*old_tuple[1]
    
    print(result)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
A = [int(x) for x in A]
Q = int(input())
BC = []
for i in range(Q):
    bc = input().split()
    bc = [int(x) for x in bc]
    BC.append(bc)


for j in range(len(BC)):
    for k in range(len(A)):
        if A[k] == BC[j][0]:
            A[k] = BC[j][1]
    total = sum(A)
    print(A)

Submission status: Wrong Answer

Solution code:
import collections
s=str(input())
s = list(map(int, input().split()))
q = int(input())
x = [list(map(int,input().split())) for i in range(q)]
c = collections.Counter(s)
ans=s
for i in range(q):
    if x[i][0] not in c.keys():
        print(ans)
        continue
    tmp = c[x[i][0]]
    del c[x[i][0]]
    c[x[i][1]] = c[x[i][1]]+tmp
    ans=0
    for j in c.keys():
        ans+=j*c[j]
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
g = []
M = 10**5+1
for i in range(1,M):
    g.append((i,0))
g = dict(g); 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:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
nums = Counter(a)
m = sum(a)
print(nums)
for _ in range(q):
  b,c = map(int,input().split())
  nums[c] += nums[b]
  k = nums[b]
  nums[b] = 0
  m = m + (c-b)*k
  print(m)
print(nums)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = input()
N = int(n)
A = [int(a) for a in input().split()]
q = input()
Q = int(q)
B, C = [], []
for i in range(Q):
    b, c = input().split()
    B.append(int(b))
    C.append(int(c))


my_dict = Counter(A)

def cals_sum(my_dict):
    values = [ my_dict[key]*key for key in my_dict.keys() if my_dict[key] != 0]
    #for key in my_dict.keys():
    #    value = my_dict[key]
    #    sum += int(key) * int(value)
    return sum(values)
  
my_sum = cals_sum(my_dict)

for i in range(len(B)):
    b = B[i]
    c = C[i]
    tmpb = my_dict[b] 
    my_dict[b] = 0
    tmpc = my_dict[c]
    my_dict[c] = tmpb + tmpc
    
    my_sum += ((tmpb + tmpc)*c - tmpb*b)

    print(my_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = 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_list[j] == b:
          a_list[j] = c
          ans = sum(a_list)
          print(ans)
          continue

Submission status: Wrong Answer

Solution code:
n = int(input())

a = input()
#print(a)
q = int(input())

b = []
for _ in range(q):
    b.append(input().split())
#print(b)
for i in range(q):
    a.replace(b[i][0],b[i][1],100000)
    c = 0
    d = a.split(" ")
    #print(d)
    for j in range(len(d)):
        c += int(d[j])
    print(c)

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)

for B,C in pala:
    res = 0
    if B in count:
        if C in count:
            count[C] += count[B]
        else:
            count[C] = count.pop(B)

    for k , v in count.items():
        res += (k * v)
    
    print(res)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A.sort()
tmp_sum = sum(A)
for i in range(Q):
    b,c = map(int, input().split())
    left = bisect.bisect_left(A,b)
    if left < N:
        if A[left] == b:
            right = bisect.bisect_right(A,b)
            A[left:right] = []
            n_left = bisect.bisect_left(A,c)
            A = A[:n_left] + [c] * (right - left) + A[n_left:]
            tmp_sum = tmp_sum + c *(right-left) - b * (right - left)
            print(tmp_sum)
    else:
        print(tmp_sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
X = {}

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

for i in range(N):
  a = A[i]
  if not a in X:
    X[a] = 1
  else:
    X[a] += 1

Q = int(input())
B = []
C = []

ANS = []
for i in range(Q):
  b, c = map(int, input().split())
  if b in X:
    if X[b] == 0:
      continue
    if c in X:
      X[c] += X[b]
    else:
      X[c] = X[b]
    X[b] = 0
  ans = 0
  for i in X.keys():
    if X[i] == 0:
      continue
    ans += i * X[i]
  ANS.append(ans)

for i in range(len(ANS)):
  print(ANS[i])

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  a = list(map(int,input().split()))
  q = int(input())
  for _ in range(q):
    b, c = map(int, input().split())
    sum=0
    for i in range(0,n):
      if(a[i]==b):
        a[i]=c
        sum+=a[i]
    print(sum)
main()

Submission status: Wrong Answer

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

for num, rep in zip(b, c):
    if num in a:
        a = [rep if ele == num else ele for ele in a]
    else:
        continue
    print(sum(a))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
BC = [list(map(int,input().split())) for _ in range(q)]
A = np.array(A)
 
def main(A,BC):
    for bc in BC:
        A = np.where(A==bc[0],bc[1],A)
        print(max(A))
        
if __name__ == '__main__':
    main(A,BC)

Submission status: Wrong Answer

Solution code:
summ=0
mapper=dict()
n=int(input())
arr=[int(i) for i in input().split()]
print(arr)
for i in range(0,n):
    temp=arr[i]
    if temp not in mapper:
        mapper[temp]=0
    mapper[temp]+=1
    summ+=temp
q=int(input())
for i in range(0,q):
    temp,temp1=[int(j) for j in input().split()]
    if((temp in mapper) and  temp1 not in mapper):
        summ-=temp*mapper[temp]
        summ+=temp1*mapper[temp]
        mapper[temp1]=0
        mapper[temp1]+=mapper[temp]
        mapper.pop(temp)
    elif((temp in mapper) and (temp1 in mapper)):
        summ-=temp*mapper[temp]
        summ+=temp1*mapper[temp]
        val=mapper[temp]
        mapper.pop(temp)
        mapper[temp1]=mapper[temp1]+val
    print(summ)

Submission status: Wrong Answer

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

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

    exc = []
    for j in range(q):
        if (bc[j][0] in exc):
            print(sum(a))
            continue
        elif (bc[j][0] not in a):
            print(sum(a))
            exc.append(bc[j][0])
            continue
        else:
            rep = [bc[j][1] if i == bc[j][0] else i for i in a]
            a = rep
            print(sum(a))

main()

Submission status: Wrong Answer

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

L = list(map(int,input().split()))
sum_all = sum(L)
L = Counter(L)
#print(L)
q = int(input())
sum1 = sum_all
for i in range(q):
    
    a,b = map(int,input().split())
    #print(L)
    if a not in L:
        L[b] = 0
    else:
        L[b] +=L[a] 
    #print(L)
    sum1 = sum1 + b*L[a] - a*L[a]
    L[a] =0
    
    #print(L)
    print(sum1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sa=sum(a)
bc=[]
for _ in range(q):
    b,c=map(int,input().split())
    bc.append([b,c])
d={}
for i in range(n):
    if a[i] not in d:
        d[a[i]]=1
    else:
        d[a[i]]+=1

for i in range(q):
    b=bc[i][0]
    c=bc[i][1]
    cb=c-b
    if b in d:
        sa+=cb*d[b]
        if c in d:
            d[c]+=d[b]
            d[b]=0
        else:
            d[c]=d[b]

    print(sa)

Submission status: Wrong Answer

Solution code:
a = int(input())
sequence = list(map(str, input().split(" ")))
q = int(input())
dictionary = {}
for i in range(q):
    pair = list(map(int, input().split(" ")))
    dictionary[pair[0]] = pair[1]

for key in dictionary:
    Sum = 0
    sequence = "".join((sequence))

    sequence = sequence.replace(str(key),str(dictionary[key]))

    sequence = list(sequence)

    for i in range(len(sequence)):
        Sum += int(sequence[i])
    print(Sum)