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())
la = list(map(int,input().split()))
q = int(input())
lb = [0]*q
lc = [0]*q
ld = [0]*q
for i in range(q):
  lb[i],lc[i] = map(int,input().split())
  ld[i] = lc[i] - lb[i]
sum = sum(la)
max = max(la)
la2 = [0]*(10**5)
for i in range(max):
  la2[i] = la.count(i+1)
for i in range(q):
  m = la2[lb[i]-1]
  sum = sum + ld[i]*m
  la2[lc[i]-1]=la2[lc[i]-1]+m
  print(sum)

Submission status: Wrong Answer

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

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

for i in A:
    data[i] += 1

sum_val = my_sum(data)

for i in range(Q):
    B, C = map(int,input().split())

    sum_val = sum_val - (data[B] * B) + ((data[C] + data[B]) * C)

    data[C] += data[B]
    data[B] = 0

    print(sum_val)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())

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

Q = int(input())
for _ in range(Q):
    x, y = map(int, input().split())
    S += (c[x]*(y-x))
    print(S)
    c[y] = c[x]
    c[x] = 0

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
C = Counter(A)

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

Submission status: Wrong Answer

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

sum = 0
for i in range(N):
    sum += A[i]
dic = cl.Counter(A)

l = []
for _ in range(Q):
    b, c = map(int, input().split())
    x = dic.get(b)
    if x == None:
        l.append(sum)
        continue
    if b < c:
        sum += (c - b) * x
    else:
        sum -= (b- c) * x
    l.append(sum)
    if c not in dic:
        dic[c] = dic.pop(b)
    else:
        dic[c] = dic.pop(b) + dic[c]
    print(dic)    

for s in l:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

counter = Counter(A)
ans = 0
for num, count in counter.items():
    ans += num * count

for i in range(Q):
    if B[i] in counter:
        ans += counter[B[i]] * C[i] - counter[B[i]] * B[i]
    print(ans)

Submission status: Wrong Answer

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

ans=[]

for i in B:
  b=i[0]
  c=i[1]
  a=A.count(b)
  ans.append(sum(A)+(c*a-b*a))
  
for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

result = {}

for target in target_list:
    print(result)
    for key, trans in trans_list.items():
        if target == trans[0]:
            target = trans[1]

        if key in result:
            result[key] = result[key] + target
        else:
            result[key] = target


for r in result.values():
    print(r)

Submission status: Wrong Answer

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

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

for i in A:
    data[i] += 1

sum_val = my_sum(data)

for i in range(Q):
    B, C = map(int,input().split())

    sum_val = sum_val - (data[B] * B) + (data[C] * C)

    data[C] += data[B]
    data[B] = 0

    print(sum_val)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
C = Counter(A)

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

Submission status: Wrong Answer

Solution code:
N = int(input())

abc = 'abcdefghijklmnopqrstuvwxyz'
ans = ''
while N > 0:
    N -= 1
    ans = abc[(N%26)] + ans
    N //= 26
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a =np.array([int(x) for x in input().split()])
q = int(input())
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]

ans = np.sum(a)
for i in range(q):
    if np.count_nonzero(a == b[i]) > 0:
        a = np.where(a == b[i], c[i],a)
        ans += np.count_nonzero(a == b[i])*(c[i]-b[i])
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

counter = Counter(A)
ans = 0
for num, count in counter.items():
    ans += num * count

for i in range(Q):
    if B[i] in counter:
        ans += counter[B[i]] * C[i] - counter[B[i]] * B[i]
        counter[C[i]] = counter[C[i]] + counter[B[i]]
    print(ans)

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)

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

NUM = [0] * (max_num + 1)

num_list = []

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

total = sum(A)

for i in range(Q):

    if (B[i] in num_list):
        total += (C[i] - B[i]) * B[i]
        NUM[B[i]], NUM[C[i]] = 0, NUM[B[i]] + NUM[C[i]]

    print(total)

Submission status: Wrong Answer

Solution code:
_ = input()
s = list(map(str, input().split()))
n = int(input())
maps={}
for _ in range(n):
  k,v = map(str, input().split())
  maps[k]=v
key_list = list(maps.keys())
flg=True

for k in key_list:
  for i in range(len(s)):
    if s[i]==k:
      s[i] = maps[s[i]]
  ans = 0
  for num in s:
    ans += int(num)
  print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

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

Submission status: Wrong Answer

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

for j in range(Q):
    res = 0
    A_fake = A_ori
    for k in range(N):
        if A_fake[k] == BC[j][0]:
            A_fake[k] = BC[j][1]
            res += A_fake[k]
    print(res)

Submission status: Wrong Answer

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

a.sort()
old=0
count=-1
for i in range(n):
  if a[i]!=old:
    x.append([a[i],1])
    count=count+1
  else:
  	x[count][1]=x[count][1]+1
  old=a[i]
for i in range(q):
  xlen=len(x)
  flag=0
  x.sort()
  s=0
  b,c=map(int,input().split())
  for j in range(xlen):
    if x[j][0]==b:
      c1=x[j][1]
      del(x[j])
      flag=1
      break
  xlen=len(x)
  for j in range(xlen):
    if x[j][0]==c:
      x[j][1]=x[j][1]+c1
      flag=2
      break
  if flag==1:
    x.append([c,c1])
  xlen=len(x)
  for j in range(xlen):
    s=s+x[j][0]*x[j][1]
  print(s)

Submission status: Wrong Answer

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

aa = set()

for i in range(q):
    b,c = map(int,input().split())
    if b not in aa:
        aa.add(b)
        l = [c if j == b else j for j in l ]
    print(sum(l))

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())

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

Q = int(input())
for _ in range(Q):
    x, y = map(int, input().split())
    S += (c[x]*(y-x))
    print(S)

Submission status: Wrong Answer

Solution code:
#-*- using:utf-8 -*-

import collections

N = int(input())
A = list(map(int, input().split()))
ACC = collections.Counter(A)
Q = int(input())
ASum = []

for _ in range(Q):
    B,C = map(int, input().split())
    ACC[C] += ACC[C]
    ACC[B] = 0
    goukei = 0
    for i in ACC.keys():
        goukei += int(i) * int(ACC[i])
    ASum.append(goukei)
    
for i in range(len(ASum)):
    print(ASum[i])

Submission status: Wrong Answer

Solution code:
N=int(input())
List=list(map(int,input().split()))
Q=int(input())
sum=0
Number=[]
Alphabet=[]
for i in range(Q):
    n,a=input().split()
    Number.append(int(n))
    Alphabet.append(int(a))
for i in range(N):
    sum+=List[i]
for i in range(Q):
    sum+=(List.count(Number[i]))*(Alphabet[i]-Number[i])
    print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
b_list = [list(input().split()) for _ in range(q)]
print(b_list[1][1])

j = 0
while j < q:
  a_replace = [b_list[j][1] if i == b_list[j][1] else i for i in a_list]
  s = sum(a_replace)
  print(s)
  j += 1

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
a_sum = sum(a_list)
q = int(input())
b = []
c = []
for i in range(q):
  bi, ci = map(int, input().split())
  b.append(bi)
  c.append(ci)

b_num = []
for i in range(q):
  num = 0
  for j in range(len(a_list)):
    if a_list[j] == b:
      num += 1
  b_num.append(num)

for i in range(q):
  if b[i] <= c[i]:
    a_sum += (c[i] - b[i]) * b_num[i]
  else:
    a_sum -= (b[i] - c[i]) * b_num[i]
  print(a_sum)

Submission status: Wrong Answer

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

from collections import defaultdict
count = defaultdict(int)

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

for _ in range(Q):
  B, C = map(int, input().split())
  if count[B] > 0:
    count[C] += count[B]
    count[B] = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = Counter(list(map(int, input().split())))
Q = int(input())
S = []
for i in range(Q):
    B, C = list(map(int, input().split()))
    if A[B] not in A:
        pass
    else:
        A[B], A[C] = 0, A[C]+A[B]
    s = sum([k*v for k, v in A.items()])
    S.append(s)
for s in S:
    print(s)

Submission status: Wrong Answer

Solution code:
N=int(input())
alist=list(map(int, input().split()))
Q=int(input())
bclist=[]
bcdic={}
for i in range(Q):
    bclist.append(list(map(int, input().split())))
def repsum(alist,bc):
    alist.sort()
    for i in range(N):
        if alist[i]==bc[0]:
            alist[i]=bc[1]
        elif alist[i]>bc[0]:
            break
    return alist
n=0
for bc in bclist:
    if n==0:
        alist=repsum(alist,bc)
        s=sum(alist)
        print(s)
        n=1
    else:
        alist=repsum(alist,bc)
        nb=alist.count(bc[0])
        s=s+(bc[1]-bc[0])*nb
        print(s)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
    
n = int(input())
print(n)

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

base=sum(A)

Q=int(input())
for i in range(Q):
    b, c = map(int, input().split())
    change_num=A.count(b)
    for i in range(change_num):
        A[A.index(b)]=c
    base+=change_num*(c-b)
    print(base)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = input()
    Q = input()

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

    operateList = (open(0).read().split("\n"))[:-1]

    for operate in operateList:
        try:
            B, C = operate.split()
            numB = A.count(B)

            answer = answer + (int(C) - int(B)) * numB
            print(answer)

            A = A.replace(B, C)

        except:
            pass

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

aa = set()

for i in range(q):
    b,c = map(int,input().split())
    if b not in aa:
        aa.add(c)
        l = [c if j == b else j for j in l ]
    print(sum(l))

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
    if(a not in num_list):
        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:
import math

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

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

bc.sort()
a2.sort()
for x in bc:
    for y in a2:
        if y[0] == x[0]:
            y[0] = x[1]
        elif y[0] > x[0]:
            break
    a2.sort()

s = math.floor(sum([float(x[0]) * x[1] for x in a2]))
print(s)

Submission status: Wrong Answer

Solution code:
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())
for i in range(q):
    b, c = map(int, input().split(" "))

    try:
        num1 = counter[b]
    except: 
        num1 = 0
        counter[b] = 0
    try:
        counter[c] += num1
    except:
        counter[c] = num1

    diff = (c - b) * num1

    cur_sum += diff
    print(cur_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
a_sum = sum(a_list)
q = int(input())
b = []
c = []
b_num = []
for i in range(q):
  bi, ci = map(int, input().split())
  b.append(bi)
  c.append(ci)
for i in range(q):
  num = 0
  for j in range(len(a_list)):
    if a_list[j] == b:
      num += 1
  b_num.append(num)

for i in range(q):
  if b[i] <= c[i]:
    a_sum += (c[i] - b[i]) * num
  else:
    a_sum -= (b[i] - c[i]) * num
  print(a_sum)

Submission status: Wrong Answer

Solution code:

import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
ans = [sum(a)]
for i in range(q):
  b, c = map(int, input().split())
  if b in dic and c in dic:
    ans.append(ans[-1] - b * dic[b] + c * dic[c])
    dic[c] += dic[b]
    dic.pop(b)
  elif b in dic and c not in dic:
    dic[c] = dic[b]
    ans.append(ans[-1] - b * dic[b] + c * dic[c])
    dic.pop(b)
  else:
    ans.append(ans[-1])
for i in range(1, q + 1):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def main():
    n=int(input())
    a=list(map(int,input().split()))
    q=int(input())
    total=sum(a)
    memo = defaultdict(int)
    answer = 0
    for i in range(n):
        memo[a[i]]+=1
    for i in range(q):
        b,c=map(int,input().split())
        temp1=memo[b]*b
        temp2=memo[b]*c
        ded=temp2-temp1
        print(total+ded)
        total+=ded










if __name__ == "__main__":

    main()

Submission status: Wrong Answer

Solution code:
count =int(input())
nums=[]
nums=input().split(" ")
count2=int(input())

nums2=[]
for i in range(count):
    nums2.append(int(nums[i]))
print(nums2)

totals=[]
for i in range(count2):
    nums3=[]
    nums3=input().split(" ")
    num1=nums3[0]
    num2=nums3[1]
    
    total=0
    for k in range(count):
        if nums2[k]==num1:
            nums2[k]=num2
        total=total+nums2[k]
        if k == count-1:
            totals.append(total)
            

print(totals)
for i in range(1,len(totals)-1,+1):
    print(totals[i])

Submission status: Wrong Answer

Solution code:
import numpy as np

def sum(n, a):
  ans = 0
  n = int(n)
  for num in range(n):
    ans += int(a[num])
  print(ans)

n = input()
n = int(n)
a = [e for e in input().split()]
q = input()
q = int(q)

for num in range(q):
  b1,b2 = input().split()
  ans = 0
  for num in range(n):
    if a[num] == b1:
      ans += int(b2)
    else:
      ans += int(a[num])
  print(ans)

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):
    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(sum(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
num_list = num_list = [i for i in map(int,input().split())]

x = int(input())
num_new = []
for i in range(x):
  b, p = map(int, input().split())
  for j in num_list:
    if j == b:
      j = p
      num_new.append(j)
    else:
      num_new.append(j)
    print(sum(num_new[-N:]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from operator import mul
import collections
N=input()
AList = list(map(int, input().split()))
Q=int(input())
BCList=[list(map(int, input().split())) for i in range(Q)]

CountList = collections.Counter(AList)
CountList=dict(CountList)
#print(CountList)

def KAKEZAN(CountList):
    KARI=(map(mul,(CountList.keys()),(CountList.values())))
    print(sum(list(KARI)))

A=0
for i in range(Q):
    #print(CountList)
    try:
        SUCHI=CountList[BCList[i][0]]
        A=1
    except KeyError:
        pass
    try:
        del CountList[BCList[i][0]]
        A+=1
    except KeyError:
        pass
    if A==2:
        try:
            CountList[BCList[i][1]]+=SUCHI
        except KeyError:
            pass
    A=0
    KAKEZAN(CountList)

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)
    print(co)
    for j in range(co):
        a[a.index(b)] = c
        a_sum += (c - b)*co
        print((c - b)*co)
    
    res.append(a_sum)
        
for i in res:
    
    print(i)

Submission status: Wrong Answer

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

import collections
cca = collections.Counter(a)

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

Submission status: Wrong Answer

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    _sum = sum(seq)
    for i in range(num):
        b, c = trials[i]
        if seq.count(b) == 0:
            print(_sum)
            continue
        seq.remove(b)
        for i in range(c):
            seq.append(c)
        _sum += (c-b)*seq.count(b)
        print(_sum)


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:
import collections as cl
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

sum = 0
for i in range(N):
    sum += A[i]
dic = cl.Counter(A)
print(A, dic, sum)

l = []
for _ in range(Q):
    b, c = map(int, input().split())
    if dic.get(b) == None:
        continue
    if b < c:
        sum += (c - b) * dic[b]
    else:
        sum -= (b- c) * dic[b]
    l.append(sum)
    if c not in dic:
        dic[c] = dic.pop(b)
    else:
        dic[c] = dic.pop(b) + dic[c]
    print(dic)    


for s in l:
    print(s)

Submission status: Wrong Answer

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

aa = set()

for i in range(q):
    b,c = map(int,input().split())
    if c not in aa:
        aa.add(c)
        l = [c if j == b else j for j in l ]
    print(sum(l))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=''
for h in input().split():
    A +=h 
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 numpy as np

def main():
  n = int(input())
  a = np.array(list(map(int, input().split())))
  q = int(input())
  
  al = len(a)
  b = []
  c = []
  for _ in range(q):
    bi, ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
    d = b + c
    a_in_not = [i for i in a if i not in d]
    print(sum(a_in_not) + ci * (al - len(a_in_not)))
 
main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

num_A = [A.count(i) for i in range(10**5+1)]
first = sum(np.array(num_A)*np.array([j for j in range(10**5+1)]))

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().replace(' ', '')

q = int(input())
tmp = []
for i in range(q):
    bc = input().split()
    b = bc[0]
    c = bc[1]
    tmp.append([b, c])

for j in range(len(tmp)):
    a = a.replace(tmp[j][0], tmp[j][1])
    answer = ''
    for k in range(len(a)):
        answer += a[k]
        answer += ' '
    answer_list = answer.split()
    answer_list = [int(x) for x in answer_list]
    print(sum(answer_list))

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):
    bc=list(map(int,input().split()))
list=np.array(a)
sum=list.sum()
ans=[]
for i in range(q):
    sa=bc[1]-bc[0]
    num=a.count(bc[0])
    ans.append(sum+num)
    
for i in range(q):
    print(ans[i])

Submission status: Wrong Answer

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

print(A)

Submission status: Wrong Answer

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

q = int(input())
int_list = [list(map(int, input().split())) for _ in range(q)]
sum = 0
for i in range(q):
    if int_list[i][0] in a:
        while int_list[i][0] in a:
            index = a.index(int_list[i][0])
            a[index] = int_list[i][1]
        for j in a:
                sum += j

        print(sum)
        sum = 0

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    if A[0] <= B <= A[-1]:
        for x in A:
            if x == B:
                x = C
        print(sum(A))
    else:
        print(sum(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
B = [0]*(10**5+1)
C = [0]*(10**5+1)

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

cou = [0]*(10**5+1)
for i in range(1,N+1):
  cou[i] = a.count(i)
allS = sum(a)

for i in range(Q): 
  N_b=0
  N_b = cou[B[i]]
  cou[B[i]] -= N_b
  cou[C[i]] += N_b

  allS = allS + (C[i]-B[i])*N_b
  print(allS)

Submission status: Wrong Answer

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

dic = {}
for i in a:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

sum = 0
for k in dic:
    v = dic[k]
    sum += k*v

for i in bc:
    if i[0] in dic:
        if i[1] in dic:
            dic[i[1]] += dic[i[0]]
        else:
            dic[i[1]] = dic[i[0]]
        o = (i[1]- i[0])*dic[i[0]]
        dic[i[0]] = 0
        sum += o
        print(sum)

Submission status: Wrong Answer

Solution code:
def main():
    import collections
    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
    ans = sum(A_list)
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())

        #num = A_list.count(B[i])
        num_dict = collections.Counter(A_list)
        num = num_dict[B[i]]
        #print(num)
        if B[i] > C[i] and B[i] in A_list :
            ans = ans - (B[i] - C[i]) * num
        elif B[i] < C[i] and B[i] in A_list  :
            ans = ans + (C[i] - B[i]) * num
        else :
            print(ans)
            break
        for j in range(N) :
            if B[i] in A_list :
                rep = A_list.index(B[i])
                A_list[rep] = C[i]
            else :
                #print("in")
                break
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
d = dict()
for i in a :
    d[i] = d.get(i,0) + 1
s = sum(a)
for i in range(q) :
    b,c = map(int,input().split())
    if b in d.keys() :
        s += (c*d[b] - b*d[b])
        if c in d.keys() :
            d[c] += 1
        else :
            d[c] = 1
        del d[b]
    print(s)

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] = [c] * (right-left)
            A.sort()
            tmp_sum = tmp_sum + c *(right-left) - b * (right - left)
            print(tmp_sum)
    else:
        print(tmp_sum)

Submission status: Wrong Answer

Solution code:
def main():
    import collections
    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
    ans = sum(A_list)
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())

        #num = A_list.count(B[i])
        num_dict = collections.Counter(A_list)
        num = num_dict[B[i]]
        #print(num)
        if B[i] > C[i] and B[i] in A_list :
            ans = ans - (B[i] - C[i]) * num
        elif B[i] < C[i] and B[i] in A_list  :
            ans = ans + (C[i] - B[i]) * num
        else :
            break
        for j in range(N) :
            if B[i] in A_list :
                rep = A_list.index(B[i])
                A_list[rep] = C[i]
            else :
                #print("in")
                break
        print(ans)

if __name__ == '__main__':
    main()