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())
cnt = sum(A)
for i in range(Q):
  b,c = map(int, input().split())
  cnt += (c-b)*A.count(b)
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import Counter
def change_dict_key(d, old_key, new_key, default_value=None):
    d[new_key] = d.pop(old_key, default_value)

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

q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if b in a_cnt.keys():
        if c in a_cnt.keys():
            a_cnt[c]+=a_cnt[b]
            tmp=a_cnt[b]
            a_cnt.pop(b)
        else:
            tmp=0
            change_dict_key(a_cnt,b,c)
    if i==0:
        ans=0
        for key,value in a_cnt.items():
            ans+=key*value
        print(ans)
    else:
        ans+=(c-b)*tmp
        print(ans)

Submission status: Wrong Answer

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

total = sum(A)
dic = {}
for i in range(q):
  b, c = map(int, input().split())
  b_count = A.count(b)
  c_count = A.count(c)
  dic[c] = c_count + b_count
  if b in dic:
    b_count = dic[b]
  diff = c - b
  total += b_count*diff
  print(total)

Submission status: Wrong Answer

Solution code:
import sys
from decimal import *
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 rep(b,c):
    if c in C:
      C[c]+=C[b]
    else:
      C[c]=C[b] 
    C[b]=0
    #print(b,c,C)

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

Submission status: Wrong Answer

Solution code:
a = int(input())
b = input().rstrip(" ").split()
B = ''.join(b)
c = int(input())
e = []
f = ""
f = B
g = 0
h = 1
x = ""
y = ""
X = 0
for i in range(c):
    e = input().rstrip().split()
    f = f.replace(e[0],e[1])
    for i in range(a):
        x = x + " " + f[g:h]
        g = g + 1
        h = h + 1
    x = x.split()
    y = [int(s) for s in x]
    print(sum(y))
    x = ""
    g = 0
    h = 1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
B = list()
C = list()
D = list()
int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    bb, cc= map(int, input().split()) 
    A = [cc if k == bb else k for k in A]
    D.append(sum(A))
for i in range(3):
    print(D[i])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)


'''
N = ii()
ans = ''
def keta(N):
    for i in range(1,15):
        if 26**i 
K = keta(N)
alpha = 0
for i in range(26):
    if 26**(K-1) + (26*i) + 1 <= N and N <= (26**(K-1)) + (26*(i+1)):
        alpha = i
        break
table = [chr(i) for i in range(97,123)]
alpha = table[i]
ans += (K-1) * alpha
last = (N-26**(K-1) - (26*memo) - 1)%26
last = table[last]
ans += last
print(ans)
'''

N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    ans = sum(A)
    B,C = mips()
    if B in check:
        ans -= check[B] * B
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        ans += check[C] * C
        A.append(-(B*check[B]))
        A.append(C*check[C])
        check[B] = 0
    print(ans)

Submission status: Wrong Answer

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

n = int(readline())
a = Counter(map(int, readline().split()))
q = int(readline())
L = []
for i in range(q):
  L.append(list(map(int, readline().split())))

s = sum(a)
for b, c in L:
  print(a)
  if b in a:
    a[c] += a[b]
    del a[b]
  # print(sum(x * y for x, y in a.items()))
    
print(a)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

_ = sys.stdin.readline()
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

previous_sum = 0
for search, replace in replace_map:
    if search in nums:
        replaced_count = nums[search]
        nums[replace] = nums[replace] + replaced_count if replace in nums else replaced_count
        del nums[search]
    if previous_sum == 0:
        previous_sum = np.dot(list(nums.keys()), list(nums.values()))
    else:
        previous_sum += replaced_count * (replace - search)
    print(previous_sum)

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
A = list(map(int,input().split()))
S = sum(A)
c = collections.Counter(A)

Q = int(input())

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

adic=dict()

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

Submission status: Wrong Answer

Solution code:
import sys
from decimal import *
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 rep(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:
    rep(b,c)
  for i in range(1,q-1):
    b,c=input().strip("\n").split()
    if b in C:
      tmp+=(Decimal(c)-Decimal(b))*Decimal(C[b])
      rep(b,c)
    ans+=str(tmp)+"\n"
  b,c=input().strip("\n").split()
  if b in C:
    tmp+=(Decimal(c)-Decimal(b))*Decimal(C[b])
  ans+=str(tmp)
  print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
a = list(map(int, input().split()))
q =int(input())
listb = []
listc = []
listnumcon = []
rest = 0
numcon = 0
for i in range(q):
  b,c = map(int, input().split())
  listb.append(b)
  listc.append(c)
  numb = a.count(b)
  listnumcon.append(numb)
  if numb > 0:
    for i in a[:]:
      if i == b :
        a.remove(b)
  for j in listc:
    if j == b:
      numcon += listnumcon[listc.index(j)]
  sumresta = sum(a)
  s = sumresta + (c * (numb+numcon)) + rest
  rest = s - sumresta -rest
  numcon = 0
  print(s)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
s=[0]*(q+1)
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],[])
for i in range(n):
	f[a[i]].append(i)
for g,h in f.items():
	#print(len(h)*g)
	s[0]=s[0]+len(h)*g
#print(f)
for i in range(1,q+1):
	if b[i-1] in f:
		if c[i-1] in f:
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]=s[i]+len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
		else:
			f.setdefault(c[i-1],[])
			f[c[i-1]]=f[c[i-1]]+f[b[i-1]]
			s[i]=s[i-1]-len(f[b[i-1]])*b[i-1]
			#print(s[i])
			
			s[i]+=len(f[b[i-1]])*c[i-1]
			f.pop(b[i-1])
	#print(f)
	#for g,h in f.items():
		#print(len(h)*g)
		#s[i]=s[i]+len(h)*g
	print(s[i])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def getCalc(NUM, num_list):
    sum = 0
    for i in num_list:
        sum += i * NUM[i]

    return sum


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)

max_num = max(max(A), max(B), max(C))

NUM = [0] * (max_num + 1)

num_list = []

for a in A:
    NUM[a] += 1
    num_list.append(a)

for i in range(Q):
    if(B[i] in num_list):
        NUM[B[i]], NUM[C[i]] = 0, NUM[B[i]] + NUM[C[i]]
        num_list.remove(B[i])
        num_list.append(C[i])

    print(getCalc(NUM, num_list))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 21 21:17:02 2020

@author: akinari
"""


N = int(input())

if N <= 26: #1
    ans = chr(N+96)
    
elif N <= 26 ** 2 + 26: #2
    N2 = int(N / 26)
    N1 = N % 26
    ans = chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26 ** 3 + 26 **2 +26: #3
    N3 = int(N / 26**2)
    N2 = int((N - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)

elif N <= 26**4 + 26**3 + 26**2 * 26: #4
    N4 = int(N / 26**3)
    N3 = int((N - 26**3*N4) / 26**2)
    N2 = int((N - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N5 = int(N / 26**4)
    N4 = int((N - 26**4*N5 )/ 26**3)
    N3 = int((N - 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26**6 + 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N6 = int(N /26**5)
    N5 = int((N - 26**5*N6)/ 26**4)
    N4 = int((N - 26**5*N6- 26**4*N5 )/ 26**3)
    N3 = int((N - 26**5*N6- 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**5*N6- 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N6 + 96) + chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26**7 + 26**6 + 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N7 = int(N / 26**6)
    N6 = int((N - 26**6*N7) /26**5)
    N5 = int((N - 26**6*N7 - 26**5*N6) / 26**4)
    N4 = int((N - 26**6*N7 - 26**5*N6 - 26**4*N5 ) / 26**3)
    N3 = int((N - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N7 + 96) + chr(N6 + 96) + chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26**8 + 26**7 + 26**6 + 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N8 = int(N / 26**7)
    N7 = int((N - 26**7*N8) / 26**6)
    N6 = int((N - 26**7*N8 - 26**6*N7) /26**5)
    N5 = int((N - 26**7*N8 - 26**6*N7 - 26**5*N6) / 26**4)
    N4 = int((N - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 ) / 26**3)
    N3 = int((N - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N8 + 96) + chr(N7 + 96) + chr(N6 + 96) + chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
    
elif N <= 26**9 + 26**8 + 26**7 + 26**6 + 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N9 = int(N / 26**8)
    N8 = int((N - 26**8*N9) / 26**7)
    N7 = int((N - 26**8*N9 - 26**7*N8) / 26**6)
    N6 = int((N - 26**8*N9 - 26**7*N8 - 26**6*N7) /26**5)
    N5 = int((N - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6) / 26**4)
    N4 = int((N - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 ) / 26**3)
    N3 = int((N - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N9 + 96) + chr(N8 + 96) + chr(N7 + 96) + chr(N6 + 96) + chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
 
elif N <= 26**10 + 26**9 + 26**8 + 26**7 + 26**6 + 26**5 + 26**4 + 26**3 + 26**2 * 26:
    N10 = int(N / 26**9)
    N9 = int((N - 26**9*N10)/ 26**8)
    N8 = int((N - 26**9*N10 - 26**8*N9) / 26**7)
    N7 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8) / 26**6)
    N6 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8 - 26**6*N7) /26**5)
    N5 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6) / 26**4)
    N4 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 ) / 26**3)
    N3 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4) / 26**2)
    N2 = int((N - 26**9*N10 - 26**8*N9 - 26**7*N8 - 26**6*N7 - 26**5*N6 - 26**4*N5 - 26**3*N4  - 26**2*N3) / 26)
    N1 = N % 26
    ans = chr(N10 + 96) + chr(N9 + 96) + chr(N8 + 96) + chr(N7 + 96) + chr(N6 + 96) + chr(N5 + 96) + chr(N4 + 96) + chr(N3 + 96) + chr(N2 + 96) + chr(N1 + 96)
   
    
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  N = int(input())
  A = list(map(int, input().split()))
  Q = int(input())
  map_ = []
  for _ in range(Q):
    b, c = list(map(int, input().split()))
    map_.append([b, c])

  mem = {}
  for a in A:
    if not(a in mem):
      mem[a] = 0
    mem[a] += 1

  sum_ = sum(A)
  for i in range(Q):
    from_, to_ = map_[i]
    idiff = 0
    if not(from_ in mem):
      print(sum_)
      break
    idiff = mem[from_]
    if not(to_ in mem):
      mem[to_] = 0
    mem[to_] += mem[from_]
    mem[from_] = 0
    diff = idiff * (to_ - from_)
    sum_ += diff
    # print(mem)
    print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = set(map(int, input().split()))
q = int(input())
ll = []
for i in range(q):
	l = list(map(int, input().split()))
	ll.append(l)
    
for i in ll:
  if i[0] in a:
    a = [i[1] if j == i[0] else j for j in a]
    print(sum(a))
  else:
    print(sum(a))

Submission status: Wrong Answer

Solution code:
n, *bc = map(int, open(0).read().split())   
a = [bc.pop(0) for _ in range(n)]           
q = bc.pop(0)

Submission status: Wrong Answer

Solution code:
import numpy as np
 
N = int(input())

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

npA = np.array(A)

Q = int(input())

Anslist = [0] * Q

for i in range(Q):

    b, c = map(int, input().split())

    A_replace = np.where(npA == b, 1, npA)

    npA = A_replace
    
    Anslist[i] += sum(A_replace)

for k in range(Q):

    print(Anslist[k])

Submission status: Wrong Answer

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

while True:
    try:
        bc=list(input().split())
        b = bc[0]
        c = bc[1]

        # a = [c if i == b else i for i in a]
        a = map(str,a)
        a = "".join(a)
        a = a.replace(b,c)
        a.split()
        a = list(map(int, a))
        print(sum(a))
    except:
        break;

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input().split())
# aa = str("".join(a))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int,input().split())))

for i in range(q):
    mae = str(bc[i][0])
    ato = str(bc[i][1])
    for i,k in enumerate(a):
        if mae == k:    
            a[i] = a[i].replace(mae,ato)
            break
    s = 0
    for j in range(len(a)):
        s += int(a[j])
    print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
res = []
a_sum = sum(a)

for i in range(q):
    b,c = list(map(int,input().split()))
    co = a.count(b)
    if co != 0:
        a[a.index(b)] = c
        a_sum += (c - b)*co
    
    res.append(a_sum)
        
for i in res:
    
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())

a_list = [int(i) for i in input().split()]
q = int(input())

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

for i in a_list:
    ary[i] += i


for _ in range(q):
    b, c = map(int, input().split())
    print('BC', b, c)
    ary[c] += (ary[b]/b)*c
    ary[b] = 0
    ans = int(sum(ary))
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=''
for i in input().split():
    A +=i
Q=int(input())
B=list(input().split() for i in range(Q))
for j in  range(Q):
    A.replace(B[j][0],B[j][1])
    for k in A:
        c=0
        c +=int(k)
        print(c)

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
q = int(input())
info = []
for i in range(q):
    info.append(list(map(int, input().split())))

a_dict = dict()
for num in a:
    if num in a_dict.keys():
        a_dict[num] += 1
    else:
        a_dict[num] = 1

a_key = list(a_dict.keys())
a_value = list(a_dict.values())
ans = 0
for k in range(len(a_dict)):
    ans += a_key[k] * a_value[k]

for j in range(q):
    if info[j][0] in a_dict.keys():
        if info[j][1] in a_dict.keys():
            ans -= info[j][0] * a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            a_dict[info[j][1]] += a_dict[info[j][0]]
            del a_dict[info[j][0]]
        else:
            ans -= info[j][0] * a_dict[info[j][0]]
            a_dict[info[j][1]] = a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            del a_dict[info[j][0]]


    print(ans)

Submission status: Wrong Answer

Solution code:
table = list("abcdefghijklmnopqrstuvwxyz")
N = int(input())

ans = []
while True:
    ans.append(N%26)
    if N%26 == 0:
        N -= 1
    if  N//26 == 0:
        break
    N//=26
for x in reversed(ans):
    print(table[x-1],end='')

print()

Submission status: Wrong Answer

Solution code:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
 
    print(str(sum))

Submission status: Wrong Answer

Solution code:
n, *bc = map(int, open(0).read().split())     
a = [bc.pop(0) for _ in range(n)]             
q = bc.pop(0)                                 
                                              
d = {}                                        
for x in a:                                   
    if x in d:                                
        d[x] += 1                             
    else:                                     
        d[x] = 1                              
ret = sum(a)

Submission status: Wrong Answer

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)
N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    ans = sum(A)
    B,C = mips()
    if B in check:
        ans -= check[B] * B
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        check[B] = 0
        ans += check[C] * C
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from decimal import *
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 rep(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:
    rep(b,c)
  for i in range(1,q-1):
    b,c=input().strip("\n").split()
    if b in C:
      tmp+=(Decimal(c)-Decimal(b))*C[b]
      rep(b,c)
    ans+=str(tmp)+"\n"
  b,c=input().strip("\n").split()
  if b in C:
    tmp+=(Decimal(c)-Decimal(b))*C[b]
  ans+=str(tmp)
  print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
lsn = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    print(sum(np.where(lsn==b,c,lsn)))

Submission status: Wrong Answer

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



    l = {i:0 for i in range(1,100001)}
    for num in A:
        l[num] += 1

    s = sum(A)

    for _ in range(Q):
        B, C = map(int, input().split())
        num_of_before = l[B]
        l[C] += num_of_before

        s = s - num_of_before*B + num_of_before*C
        print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

operations = []

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for op in operations:
    target, goto = op

    a.sort()

    try:
        index = a.index(target)
        
        while True:
            if a[index] != target:
                break

            a[index] = target
            index += 1

        print(sum(a))

    except:
        print(sum(a))

    #for i in range(len(a)):
    #    if a[i] == target:
    #        a[i] = goto

    #print(sum(a))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math  # gcdもあるよ
import collections
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    q = int(sys.stdin.readline())
    queue = [list(map(int,sys.stdin.readline().split())) for _ in range(q)]
    count = collections.Counter(a)
    print(count)
    print(list(count))
    for b, c in queue:
        if  b in count:
            if c in count:
                count[c] += count[b]
                del count[b]
            else:
                count[c] = count[b]
                del count[b]
        ans = 0
        for k in count:
            ans += k * count[k]
        print(ans) 
    
    
    return 0

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)
N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    ans = sum(A)
    B,C = mips()
    if B in check:
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        check[B] = 0
        ans -= check[C] * C
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
for n in range(N):
    B[n+1] = B[n] + A[n]
C = B.pop()

Z = {}
for n in range(N):
    Z[A[n]] = 0
for n in range(N):
    Z[A[n]] += 1

D = []
Q = int(input())
for q in range(Q):
    x, y = map(int, input().split())
    if x in Z:
        C = C - x*Z[x]
        C = C + y*Z[x]
        D.append(C)
        if x == y:
            continue
        if y in Z:
            Z[y] += 1
        else:
            Z[y] = Z[x]
        Z.pop(x)
    else:
        D.append(C)
for d in D:
    print(d)

Submission status: Wrong Answer

Solution code:
import sys
from decimal import *
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+=(Decimal(c)-Decimal(b))*C[b]
      re(b,c)
    ans+=str(tmp)+"\n"
  b,c=input().strip("\n").split()
  if b in C:
    tmp+=(Decimal(c)-Decimal(b))*C[b]
  ans+=str(tmp)
  print(ans)

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())
    print(sum(np.where(a == B, C, a)))

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
q = int(input())
info = []
for i in range(q):
    info.append(list(map(int, input().split())))

a_dict = dict()
for num in a:
    if num in a_dict.keys():
        a_dict[num] += 1
    else:
        a_dict[num] = 1

a_key = list(a_dict.keys())
a_value = list(a_dict.values())
ans = 0
for k in range(len(a_dict)):
    ans += a_key[k] * a_value[k]

for j in range(q):
    if info[j][0] in a_dict.keys():
        if info[j][1] in a_dict.keys():
            ans -= info[j][0] * a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            a_dict[info[j][1]] += a_dict[info[j][0]]
            del a_dict[info[j][0]]
        else:
            ans -= info[j][0] * a_dict[info[j][0]]
            a_dict[info[j][1]] = a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            del a_dict[info[j][0]]
    ans = 0

    print(ans)

Submission status: Wrong Answer

Solution code:
def sol():
    n=int(input())
    A=[int(i)for i in input().split()]
    q=int(input())
    for i in range(q):
        b,c=map(int,input().split())
        if(b in A):
            p=A.index(b)
            A[p]=c
        print(sum(A))
if(__name__=='__main__'):
    sol()

Submission status: Wrong Answer

Solution code:
a = int(input())
sequence = list(map(int, input().split(" ")))
if len(sequence) == a:
    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:
N = int(input())
Ac = {}
for a in input().strip().split(' '):
    if a in Ac:
        Ac[a] += int(a)
    else:
        Ac[a] = int(a)

Q = int(input())
total = sum(Ac.values())
for q in range(Q):
    B, C = input().strip().split(' ')
    
    if B in Ac:
        if C in Ac:
            Ac[C] += Ac[B] // int(B) * int(C)
        else:
            Ac[C] = Ac[B] // int(B) * int(C)
        del Ac[B]
    else:
        print(total)
    total = sum(Ac.values())
    print(total)

Submission status: Wrong Answer

Solution code:
if __name__=='__main__':
    N = int(input())
    A = input().split(" ")
    A = [int(s) for s in A]
    Q=int(input())
    str_list=[]
    S=[]
    banlist=[]
    for j in range(Q):
        str_list=input().split(" ")

        if int(str_list[0]) not in banlist:
            if int(str_list[0]) in A:
                #A=[s.replace(int(str_list[j][0]),int(str_list[j][1])) for s in A]
                A=[(int(str_list[1])) if i == int(str_list[0]) else i for i in A]
            else:
                banlist.append(int(str_list[0]))

        print(sum(A))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l={}
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    if i in l:
        l[i]+=1
    else:
        l[i]=1
for (f,t) in move:
    if f in l:
        if t in l:
            l[t]+=l[f]
            l[f]=0
        else:
            l[t]=l[f]
            l[f]=0
        sum = 0
        for k,v in l.items():
            sum += k*v
        print(sum)
    else:
        print(sum)