Problem:


Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
#print()
#print(N)
#print(A)
#print(Q)
#print()
counterA = dict(Counter(A))
counterAkeys=set(counterA.keys())
#print(counterAkeys)

sumA = sum(A)

#print(sumA,counterA)
for _ in range(Q):
  b,c=map(int,input().split())
  #print(b,c)
  if b not in counterAkeys:
    print(sumA)
    continue
  b_cnt = counterA[b]
  if c in counterAkeys:
    counterA[c] += b_cnt
  else:
    counterA[c] = b_cnt
    counterAkeys.add(c)

  sumA+=(c-b)*b_cnt
  print(sumA)

Submission status: Wrong Answer

Solution code:
import sys
import collections

input = sys.stdin.readline


def main():
    N = int(input())
    A =list(map(int,input().split()))
    AD = collections.Counter(A)
    Q = int(input())
    ans =0
    for key, item in AD.items():
        ans += key * item


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






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = input()
a = list(map(int, input().split()))
q =int(input())
s = 0
for i in range(q):
  b,c = map(int, input().split())
  numb = a.count(b)
  if numb > 0:
    for b in a[:]: a.remove(b)
  s = sum(a) + c * numb +s
  print(s)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    n = int(input())
    al = list(map(int, input().split()))
    q = int(input())
    cnt_dic = {}
    ans = 0
    for a in al:
        cnt_dic.setdefault(a,0)
        cnt_dic[a] += 1
        ans += a

    bcl = []
    for _ in range(q):
        b, c = map(int, input().split())
        bcl.append((b,c))


    for bc in bcl:
        b,c = bc
        if b not in cnt_dic:
            continue
        ans += cnt_dic[b]*(c-b)
        cnt_dic.setdefault(c,0)
        cnt_dic[c] += cnt_dic[b]
        cnt_dic[b] = 0
        print(ans)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
alist = [int(s) for s in input().split(" ")]
adict = {}
ans = 0
for a in alist:
  ans += a
  if a in adict:
    adict[a] += 1
  else:
    adict[a] = 1
q = int(input())
bclist = []
for _ in range(q):
  b, c = [int(s) for s in input().split(" ")]
  bclist.append([b, c])

for b, c in bclist:
  if c in adict and b in adict:
    ans += c * (adict[c] + adict[b]) - b * adict[b]
    adict[c] += adict[b]
    del adict[b]
  elif b in adict:
    ans += (c - b) * adict[b]
    adict[c] = adict[b]
    del adict[b]
  print(ans)

Submission status: Wrong Answer

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

a_c = Counter(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

a.sort()

# for i in range(q):
#     if b[i] in a:
#         s = a.index(b[i])
#         while s < n and a[s] == b[i]:
#             a[s] = c[i]
#             s += 1
#     print(sum(a))

sum = sum(a)
x = [0] * (max(c) + 1)

for i in range(q):
    if (b[i] in a) or x[b[i]] != 0:
        cnt = a.count(b[i])
        sum += (cnt + x[b[i]]) * (c[i] - b[i])
        x[c[i]] += cnt
    print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int,input().split())
g = [0] * (10**5 + 1)
s = 0
for A in a:
    g[A] += 1
    s += A
q = int(input())
for loop in range(q):
    b,c = map(int,input().split())
    s += g[b] * (c - b)
    g[c] += g[b]
    g[b] == 0
    print(s)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    a = list(IL())
    q = I()
    s = deque()
    for rep in range(q):
        b,c = IL()
        tmp = 0
        for item in a:
          if item==b:
            tmp += c
          else:
            tmp += item
        s.append(tmp)
    for _ in range(q):
        print(s.popleft())
    return

if __name__=='__main__':
    Main()

Submission status: Wrong Answer

Solution code:
n = int(input())
alist = [int(s) for s in input().split(" ")]
adict = {}
ans = 0
for a in alist:
  ans += a
  if a in adict:
    adict[a] += 1
  else:
    adict[a] = 1
q = int(input())
bclist = []
for _ in range(q):
  b, c = [int(s) for s in input().split(" ")]
  bclist.append([b, c])

for b, c in bclist:
  if c in adict and b in adict:
    ans += c * adict[c] - b * adict[b]
    adict[c] += adict[b]
    del adict[b]
  elif b in adict:
    ans += (c - b) * adict[b]
    adict[c] = adict[b]
    del adict[b]
  print(ans)

Submission status: Wrong Answer

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

a_c = Counter(a)
a_sum = sum(a)
for i in range(q):
    if bc[i][0] in a_c:
        a_sum += a_c[bc[i][0]] * (bc[i][1] - bc[i][0])
        if bc[i][1] in a_c:
            a_c[bc[i][1]] += a_c[bc[i][0]]
        else:
            a_c[bc[i][1]] = a_c[bc[i][0]]
    else:
        a_sum = a_sum
    print(a_sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
Q=int(input())
cnt=Counter(A)
for _ in range(Q):
    b,c=map(int,input().split())
    if b in cnt:
        t=cnt[b]
        s-=t*b
        s+=t*c
        print(s)
        if c in cnt:
            cnt[c]+=t
            cnt.pop(b)
        else:
            cnt[c]=t
    else:
        print(s)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

import collections
counter = collections.defaultdict(int)
for key, value in collections.Counter(A).items():
    counter[key] = value

ans = sum(A)

for _ in range(Q):
    b, c = map(int, input().split())
    # print("b: {} c: {} counter: {}".format(b, c, counter))
    v = counter[b]
    counter[c] += v
    dif = v * (c - b)
    ans += dif
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

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

Submission status: Wrong Answer

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

from collections import defaultdict


def st(line):
    for l in line:
        if l == ' ':
            continue
        yield l


def solve():
    N = int(input())
    As = st(input())

    d = dict()
    total = 0
    for a in As:
        a = int(a)

        if a in d:
            d[a] += 1
        else:
            d[a] = 1
        total += a

    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))

    for que in queries:
        b, c = que

        if b not in d:
            print(total)
            continue

        n_b = d[b]
        d[b] = 0

        if c not in d:
            d[c] = 0

        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
ans = ''
while True:
    
    ans += alpha[n % 26 - 1]
    n = n // 26
    if n <= 1:
        break

print(ans[::-1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i],C[i] = map( int, input().split() )
listA = []
for q in range(Q):
    A = sorted(A)
    listA.append(sum(A))
    for i in range(len(A)):
        if A[i] < B[q]:
            break
        elif A[i] == B[q]:
            A[i] = C[q]
listA.append(sum(A))
for a in range(1,len(listA)):
    print(listA[a])

Submission status: Wrong Answer

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

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

    d = dict()

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

    for bc in BC:
        try:
            d[bc[1]] += d[bc[0]]
        except KeyError:
            try:
                d[bc[1]] = d[bc[0]]
            except KeyError:
                pass
        finally:
            d[bc[0]] = 0
        
        for dat in d.items():
            ans += dat[0]*dat[1]
        
        print(ans)
        ans = 0

main()

Submission status: Wrong Answer

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        B[i], C[i] = map(int, input().split())
        
    #output
    from collections import Counter
    D = Counter(A)
    S = sum(A)

    for i in range(Q):
        x = D[B[i]]
        temp = S - x*B[i] + x*C[i] 
        print(temp)
        D[C[i]] += x
        S = temp    

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
A = list(map(int, input().split()))
q = int(input())
readline = sys.stdin.readline
BC = [[int(i) for i in readline().split()] for _ in range(q)]

d = {}
for a in A:
    d[a] = d.get(a, 0) + 1

#MAX_N = 10**5+5
#l_conv = [0]*MAX_N
#
#for b,c in BC:
#    l_conv[b] = c

ans = []
su = sum(A)

for b,c in BC:
    if b in d:
        su = su - b*d[b] + c*d[b]
        d[c] = d.get(c, 0) + d[b]
        ans.append(su)
    else:
        ans.append(su)

print(*ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
num_list = list(map(int,input().split()))
q = int(input())

num_list.sort()

for i in range(q):
  query = list(map(int,input().split()))
  for j in range(len(num_list)):
    if num_list[j] == query[0]:
      num_list[j] = query[1]
    if num_list[j] > query[0]:
      break
  print(sum(num_list))

Submission status: Wrong Answer

Solution code:
import sys
import collections

input = sys.stdin.readline


def main():
    N = int(input())
    A =list(map(int,input().split()))
    AD = collections.Counter(A)
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        if b not in AD.keys():
            AD[c] = AD[b]
            AD[b] =0
        else:
            AD[c] += AD[b]
            AD[b] =0
        ans =0
        for key,item in AD.items():
            ans += key *item

        print(ans)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect
import collections
n=int(input())
a=sorted(list(map(int,input().split())))
q=int(input())
A=collections.Counter(a)
ans=sum(a)
for _ in range(q):
  b,c=map(int,input().split())
  k=A[b]
  ans=(c-b)*k+ans
  print(ans)
  del A[b]
  A[c]=k

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
d = dict()
for i in a:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1

q = int(input())

res = sum(a)

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
Q=int(input())
cnt=Counter(A)
for _ in range(Q):
    b,c=map(int,input().split())
    if b in cnt:
        t=cnt[b]
        if t>0:
            s-=t*b
            s+=t*c
            print(s)
            if c in cnt:
                cnt[c]+=t
                cnt[b]=0
            else:
                cnt[c]=t
        else:
            print(s)
    else:
        print(s)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]

    d = dict()
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1

    total = sum(A)
    ans = []
    Q = int(readline())
    for _ in range(Q):
        b, c = map(int, readline().split())
        if b in d:
            if c in d:
                d[c] += d[b]
            else:
                d[c] = d[b]

            total += (c - b) * d[b]
            ans.append(total)
            d.pop(b)

        else:
            ans.append(total)

        print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

# a.sort()

# for i in range(q):
#     if b[i] in a:
#         s = a.index(b[i])
#         while s < n and a[s] == b[i]:
#             a[s] = c[i]
#             s += 1
#     print(sum(a))

sum = sum(a)
x = [0] * (max(c) + 1)

for i in range(q):
    cnt = a.count(b[i])
    sum += (cnt + x[b[i]]) * (c[i] - b[i])
    print(sum)
    x[c[i]] = cnt

Submission status: Wrong Answer

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

from collections import defaultdict


def st(line):
    for l in line:
        if l == ' ':
            continue
        yield l


def solve():
    N = int(input())
    As = st(input())

    d = defaultdict(int)
    total = 0
    for a in As:
        a = int(a)
        d[a] += 1
        total += a

    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))

    for que in queries:
        b, c = que

        n_b = d[b]
        d[b] = 0
        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
dica=Counter(a)
q=int(input())
b=[]
c=[]
for i in range(q):
  b_,c_=map(int,input().split())
  b.append(b_)
  c.append(c_)

i=0
j=0

while i<=(q-1):
  num=dica[b[i]]
  new_sum=suma-b[i]*num+c[i]*num
  print(new_sum)
  
  suma=new_sum
  if c[i] in dica.keys():
    dica[c[i]]+=num
  else:
    dica.setdefault(c[i],num)
  i+=1

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
A_=sum(A)
q=int(input())
visit=[0]*10**5
for _ in range(q):
  b,c=map(int,input().split())
  u=A.count(b)
  visit[c-1]+=u
  A_=A_+(u+visit[b-1])*(c-b)
  visit[b-1]=0
  print(A_)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))

x = [0] * (10**5+5)
for a in A:
    x[a] += 1
s = sum(A)
Q = int(readline())
for _ in range(Q):
    b, c = map(int, readline().split())
    s += (c - b) * x[b]
    print(s)
    x[c] += x[b]

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
Q=int(input())
cnt=Counter(A)
for _ in range(Q):
    b,c=map(int,input().split())
    if b in cnt:
        s-=cnt[b]*b
        s+=cnt[b]*c
        print(s)
        if c in cnt:
            cnt[c]+=cnt[b]
            cnt[b]=0
            cnt+=Counter()
        else:
            cnt[c]=cnt[b]
    else:
        print(s)

Submission status: Wrong Answer

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []
    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)

    #output
    from collections import Counter
    D = Counter(A)
    S = sum(A)

    for i in range(Q):
        x = D[B[i]]
        print(S - x*B[i] + x*C[i])
        D[C[i]] += x
        S = S -x*B[i] + x*C[i]

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def sums(l):
	s = 0
	for v in l:
		s += v
	return s

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

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

q = int(input())
for _ in range(q):
	b, c = map(int, input().split())
	if b not in ind:
		continue
	for i in ind[b]:
		a[i] = c
		if c in ind:
			ind[c].append(i)

		else:
			ind[c] = [i]
			
	print(sums(a))

Submission status: Wrong Answer

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

ans = sum(A)
cnt = collections.Counter(A)

dp = [0]*(10**5+1)

for i in cnt.items():
    dp[i[0]] = i[1]

for i in range(Q):
    ans += (BC[i][1] - BC[i][0]) * dp[BC[i][0]]
    dp[BC[i][1]] += dp[BC[i][0]]
    dp[BC[i][0]] == 0
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

dic = Counter(A)

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if b in dic.keys():
        # ans = sumA+(dic[b]*(c-b))

        B = dic[b]
        # print("B", B)
        if c in dic.keys():
            C = dic[c]
            dic[c] += dic[b]
            del dic[b]
        else:
            C = B
            dic[c] = dic[b]
            del dic[b]
        # print("C", C)
        
        sumA += (C*c-B*b)
        
        # print(dic)
    # else:
    #     ans = sumA
    print(sumA)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
q = int(input())

c = Counter(l)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
new_sum = sum(arr)
for _ in range(k):
  a,b = map(int, input().split())
  s = new_sum - arr.count(a)*a + b
  print(s)

Submission status: Wrong Answer

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

from collections import defaultdict


def solve():
    N = int(input())
    As = map(int, readline().strip().split())

    d = defaultdict(int)
    total = 0
    for a in As:
        d[a] += 1
        total += a

    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))

    for que in queries:
        b, c = que

        n_b = d[b]
        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:


n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
bc = []
d = {}
suma = sum(a)
for nr in a:
    if nr not in d:
        d[nr] = 1
    else:
        d[nr] += 1

for i in range(q):
    bc = [int(i) for i in input().split()]
    b = bc[0]
    c = bc[1]
    if b not in d:
        print(suma)
        continue
    suma = suma + (c - b) * d[b]
    if c not in d:
        d[c] = d[b]
    else:
        d[c] += d[b]
    print(suma)

Submission status: Wrong Answer

Solution code:
N = int(input())

CNT = {}
S = 0

for a in map(int, input().split()):
    if not(a in CNT):
        CNT[a] = 0

    CNT[a] += 1
    S += a

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

    if B in CNT:
        cnt = CNT[B]
        S -= B*cnt
        S += C*cnt
        
        if not(C in CNT):
            CNT[C] = 0

        CNT[C] += cnt
        
    print(S)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = {}
for x in a:
  try:
    if b[x]:
      b[x] += 1
  except:
    b[x] = 1
#print(b)
ans = sum(a)
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  try:
    if b[x] != -1:
      bx = b[x]
      b[x] = 0
  except:
    #print(b)
    print(ans)
    continue
  try:
    if b[y]:
      b[y] += bx
  except:
    b[y] = bx
  #print(b)
  ans += bx * (y-x)
  print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
lis = np.zeros(10**5)

As = np.array(input().split()).astype(np.int64)
NUM = As.sum()

for n in As:
  lis[n-1] += 1

Q = int(input())
for q in range(Q):
  B, C = map(int, input().split())
  nnn = lis[B-1]
  lis[B-1] = 0
  lis[C-1] += nnn
  NUM = NUM - B*nnn + C*nnn
  print(NUM)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = Counter(map(int, input().split()))
    q = int(input())
    bc = [map(int, input().split()) for _ in range(q)]
    temp = suma(a)
    for b,c in bc:
        t = a[b]
        temp -= b*t
        a[c] += t
        temp += c*t
        print(temp)

def suma(a):
    x = 0
    for i in a.items():
        x += i[0] * i[1]
    return x

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(j)for j in input().split()]
s=sum(a)
import collections
c=collections.Counter(a)
q=int(input())
for i in range(q):
    x,y=map(int,input().split())
    s+=c[x]*(y-x)
    c[y]+=c[x]
    c[x]=0
    print(s)
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
Q=int(input())
cnt=Counter(A)
for _ in range(Q):
    b,c=map(int,input().split())
    if b in cnt:
        s-=cnt[b]*b
        s+=cnt[b]*c
        print(s)
        if c in cnt:
            cnt[c]+=cnt[b]
            cnt[b]=0
        else:
            cnt[c]=cnt[b]
    else:
        print(s)

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    B, C = [], []
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))
        B.append(b)
        C.append(c)

    for i in range(q):
        summ = 0
        for a in A:
            if a == B[i]:
                summ += C[i]
            else:
                summ += a
        print(summ)

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)]
print(bc)
sum = 0
for i in range(q):
    for j in range(n):
        if a[j] == bc[i][0]:
            a[j] = bc[i][1]
    for k in range(n):
        sum += a[k]
    print(sum)
    sum = 0

Submission status: Wrong Answer

Solution code:
from collections import Counter


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

count = Counter(a)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = {}
for x in a:
  try:
    if b[x]:
      b[x] += 1
  except:
    b[x] = 1
#print(b)
ans = sum(a)
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  try:
    if b[x]:
      bx = b[x]
      b[x] = 0
  except:
    #print(b)
    print(ans)
    continue
  try:
    if b[y]:
      b[y] += bx
  except:
    b[y] = bx
  #print(b)
  ans -= bx * x
  ans += bx * y
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    num = [0] * 100005
    A = list(map(int,input().split()))
    S = sum(A)
    for a in A:
        num[a] += 1
    
    Q = int(input())
    for _ in range(Q):
        c,d = map(int,input().split())
        x = num[c]
        S -= x * c
        num[d] += x
        S += x * d
        print(S)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')

import numpy as np



def main():
    N = I()
    A_list = LI()
    Q = I()
    act_list = LLIN(Q)
    np_array = np.array(A_list)
    for z in act_list:
        # A_list = [z[1] if i == z[0] else i for i in A_list]
        np.place(np_array, np_array == z[0], z[1])
        print(sum(A_list))

if __name__ == '__main__':
    main()