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:
import collections
n = int(input())
a_lst = list(map(int, input().split()))

cs = collections.Counter(a_lst)
res = sum(a_lst)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if b in cs:
    res = res - b * cs[b] + c * cs[b]
    if c in cs:
      cs[c] += cs[b]
      cs[b] = 0
    else:
      cs[c] = cs[b]
      cs[b] = 0      
    print(res)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions
import functools
import copy
from collections import deque
from functools import reduce
from decimal import Decimal


def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    bc = set()
    for _ in range(q):
        bc.add(tuple(map(int, input().split())))

    for element in bc:
        for i in range(len(a)):
            if a[i] == element[0]:
                a[i] = element[1]
        print(sum(a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
import math
import fractions
import functools
import copy
from collections import deque
from functools import reduce
from decimal import Decimal


def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    bc = []
    for _ in range(q):
        bc.append(list(map(int, input().split())))
    print(bc)

    for element in bc:
        for i in range(len(a)):
            if a[i] == element[0]:
                a[i] = element[1]
        print(sum(a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
import bisect

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    # A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    # b = np.copy(A)

    # print(sum(A))
    print(N)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
import bisect

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    # A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    # b = np.copy(A)

    # print(sum(A))
    print(A)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
import bisect

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    # A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    # b = np.copy(A)

    print(sum(A))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

readline = sys.stdin.readline

N = int(input())
A = np.array(readline().split(), np.int64)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

print(N)

"""
for q in range(Q):
    A = [BC[q][1] if x==BC[q][0] else x for x in A]
    print(sum(A))
    """

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
count = [0 for i in range(110000)]
sum_ = sum(A)
for i in range(N):
  count[A[i]] += 1

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
count = [0 for i in range(110000)]
sum_ = sum(A)
for i in range(N):
  count[A[i]] += 1

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
bc = [[0 for i in range(2)] for j in range(Q)]
for i in range(Q):
  bc[i] = list(map(int,input().split()))

count = [0 for i in range(110000)]
for i in range(N):
  count[A[i]] += 1

sum_ = sum(A)
for i in range(Q):
  b,c = bc[i]
  n = count[b]
  sum_ -= b*n
  sum_ += c*n
  count[c] += n
  print(sum_)

Submission status: Wrong Answer

Solution code:
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())
ls = []
lsans=[]
D = A
ans = 0
for z in range(N):
    ans += A[z]
    #print(ans)
for j in range(Q):
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    if j >= 1:
        if B[j]==C[j-1]:
            #print("yes")
            ans -= (B[j]-C[j])*(len(ls[j])+n)
    else:
        n = len(ls[j])
        ans -= (B[j]-C[j])*len(ls[j])

    print(ans)

Submission status: Wrong Answer

Solution code:
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())
ls = []
lsans=[]
D = A
ans = 0
for z in range(N):
    ans += A[z]
for j in range(Q):
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    if j > 1:
        if B[j]==B[j-1]:
            ans -= (B[j]-C[j])*(len(ls[j])+n)
        else:
            n = len(ls[j])
            ans -= (B[j]-C[j])*len(ls[j])
        print(ans)

Submission status: Wrong Answer

Solution code:
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())
ls = []
lsans=[]
#ls.append([i for i,x in enumerate(A) if x == 2])
D = A
for j in range(Q):
    #print(Q)
    #print(D)
    ans = 0
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    #print(B[j],"B[j]")
    #print("j=",j,[a for a,x in enumerate(A) if x == B[j]])
    #print(A)
    if len(ls[j]) != 0:
        for i in range(len(ls[j])):
            #print("i",i)
            #print(len(ls[j]))
            #print(A)
            D[ls[j][i]] = C[j]
            #print(D)
        for z in range(N):
            ans += D[z]
            D = A
                #print("ans",ans)
            #lsans = []
        lsans.append(ans)
        #print("j",j)
            #print(lsans)
for i in range(len(lsans)):
    print (lsans[i])

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  print(B, C)
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print(sum(A))

Submission status: Wrong Answer

Solution code:
n=int(input(""))
ain=input("").split(" ")
a={1:0,}
c=[]
for i in range(n):
    c+=[int(ain[i])]
s=sum(c)
for i in range(n):
    if (c[i] in a):
        a[c[i]]+=1
    else:
        a[c[i]]=1
q=int(input(""))
c=[]

for i in range(q):
    c=input("").split(" ")
    bb=int(c[0])
    cc=int(c[1])
    if (bb in a and a[bb]!=0):
        s+=(cc-bb)*a[bb]
        if (cc in a):
            a[cc]+=a[bb]
        else:
            a[cc]=a[bb]
                              
    print(s)

Submission status: Wrong Answer

Solution code:
n=int(input(""))
ain=input("").split(" ")
a={1:0,}
c=[]
for i in range(n):
    c+=[int(ain[i])]
s=sum(c)
for i in range(n):
    if (c[i] in a):
        a[c[i]]+=1
    else:
        a[c[i]]=1
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    bb=int(c[i][0])
    cc=int(c[i][1])
    if (bb in a and a[bb]!=0):
        s+=(cc-bb)*a[bb]
        if (cc in a):
            a[cc]+=a[bb]
        else:
            a[cc]=a[bb]
                              
    print(s)

Submission status: Wrong Answer

Solution code:
n=int(input(""))
ain=input("").split(" ")
a={1:0,}
c=[]
for i in range(n):
    c+=[int(ain[i])]
s=sum(c)
for i in range(n):
    if (c[i] in a):
        a[c[i]]+=1
    else:
        a[c[i]]=1
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    listb+=[int(c[i][0])]
    listc+=[int(c[i][1])]
    if (listb[i] in a and a[listb[i]]!=0):
        s+=(listc[i]-listb[i])*a[listb[i]]
        if (listc[i] in a):
            a[listc[i]]+=a[listb[i]]
        else:
            a[listc[i]]=a[listb[i]]
    
    print(s)

Submission status: Wrong Answer

Solution code:
 
n=int(input(""))
a=[]
n-=1
for i in range(11):
    if (n==0):
        break
    a+=[n%26+1]
    n=int(n/26)
b=[]
for i in range(len(a)):
    b+=[a[len(a)-i-1]]

s=""
for i in b:
    if (i==1):
        s+="a"
    elif(i==2):
        s+="b"
    elif(i==3):
        s+="c"
    elif(i==4):
        s+="d"
    elif(i==5):
        s+="e"
    elif(i==6):
        s+="f"
    elif(i==7):
        s+="g"
    elif(i==8):
        s+="h"
    elif(i==9):
        s+="i"
    elif(i==10):
        s+="j"
    elif(i==11):
        s+="k"
    elif(i==12):
        s+="l"
    elif(i==13):
        s+="m"
    elif(i==14):
        s+="n"
    elif(i==15):
        s+="o"
    elif(i==16):
        s+="p"
    elif(i==17):
        s+="q"
    elif(i==18):
        s+="r"
    elif(i==19):
        s+="s"
    elif(i==20):
        s+="t"
    elif(i==21):
        s+="u"
    elif(i==22):
        s+="v"
    elif(i==23):
        s+="w"
    elif(i==24):
        s+="x"
    elif(i==25):
        s+="y"
    elif(i==26):
        s+="z"
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int,input().strip().split()))
s = sum(num)
num_dict = {}
for i in num:
  if i in num_dict:
    num_dict[i] += 1
  else:
    num_dict[i] = 1
q = int(input())
for i in range(q):
  b,c = map(int,input().strip().split())
  if b in num_dict:
    num_dict[c] = num_dict[b]
    del num_dict[b]
    if b > c:
      s = s - (b-c)*num_dict[c]
      print(s)
    else:
      s = s + (c-b)*num_dict[c]
      print(s)
  else:
    print(s)

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
D = collections.Counter(A)
Q = int(input())
#print(D)
ans = sum(A)
for i in range(Q):
    B,C = map(int,input().split())
    #print(D[B],type(D[B]))
    ans += (C-B)*D[B]
    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
numlista=list(map(int,input().split()))
#aをリスト化する
stra=str()
for i in numlista:
  stra+=str(i)
#aの合計を求める
suma=0
for i in range(n):
  suma+=numlista[i]
#
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  k=c-b
  o=stra.count(str(b))
  suma+=k*o
  print(suma)

Submission status: Wrong Answer

Solution code:
n=int(input()) #get input N
a=list(map(int, input().split())) #get input A
q=int(input()) #get input Q
b=0 #for increment in while loop
c=0 #temporary answer

while b<q: #loop to get input for B and C

    x,y=input().split(" ")
    for i in a:
       if x==i:
           i=y #change element into C

    for i in a:
        c+=i #add all element in list

    print (c)
    b+=1
    c=0

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
sum_num = sum(A)
for before, after in BC:
    if before in A.keys():
        cnt = A[before]
        del A[before]
        sum_num -= before * cnt
        add = Counter({after: cnt})
        A = A + add
        sum_num += after * cnt
    print(sum_num)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
for before, after in BC:
    if before in A.values():
        cnt = A[before]
        del A[before]
        add = Counter({after: cnt})
        A = A + add
    print(sum([value * cnt for value, cnt in A.most_common()]))

Submission status: Wrong Answer

Solution code:
import collections

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

s = sum(A)
c = collections.Counter(A)

for _ in range(Q):
    B, C = map(int, input().split())
    s += (C-B)*c[B]
    c[C] += c[B]
    c[B] += c[C]
    print(s)

Submission status: Wrong Answer

Solution code:
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

# Problem A
# alpha = input()
# if alpha.isupper():
#     print("A")
# else:
#     print("a")

# Problem B
# input_scan()
# [n, k] = [int(x) for x in next_line().split(" ")]
# fruits = sorted([int(x) for x in next_line().split(" ")])
# print(sum(fruits[:k]))

# Problem C
# num = int(input())

# dictionary = "abcdefghijklmnopqrstuvwxyz"

# def convert_to_alpha(num):
#     if num == 0:
#         return ""
#     quot = num // 26
#     rem = num % 26
#     if rem == 0:
#         return "z" + convert_to_alpha(quot - 1)
#     else:
#         return dictionary[rem - 1] + convert_to_alpha(quot)

# name = ""
# print("".join(reversed(convert_to_alpha(num))))

# Problem D
input_scan()

next_line()
from collections import Counter
nums = [int(x) for x in next_line().split(" ")]
total_sum = sum(nums)
nums = Counter(nums)


q = int(next_line())
for i in range(q):
    [b, c] = [int(x) for x in next_line().split(" ")]
    diff = c - b
    total_sum += nums[b] * diff
    occ = nums[b]
    if c in nums:
        nums[c] += occ
    else:
        nums[c] = occ

    append_output(str(total_sum))

print_output()

Submission status: Wrong Answer

Solution code:
n=int(input())
l1=list(map(int,input().split()))
p=sum(l1)
q=int(input())
for _ in range(q):
    a,b=map(int,input().split())
    p=p+b-a
    k=p
    print(k)

Submission status: Wrong Answer

Solution code:
n=int(input())
l1=list(map(int,input().split()))
p=sum(l1)
q=int(input())
for _ in range(q):
    a,b=map(int,input().split())
    print(p+b-a)

Submission status: Wrong Answer

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

for i in li:
    b, c = map(int, input().split())
    li = [c if i == b else i for i in li]
    gokei.append(sum(li))
    q -= 1
    if q == 0:
        [print(i) for i in gokei]
        break

Submission status: Wrong Answer

Solution code:
K = int(input())
S = len(input())

M = 10**9 + 7

F = [1]
IF = [1]
for i in range(1, K + S + 1):
    F.append((F[-1] * i) % M)
    IF.append((IF[-1] * pow(i, M - 2, M)) % M)

def nCr(n, r):
    return (F[n] * IF[r] * IF[n-r]) % M

ans = 0
for i in range(K + 1):
    ans = (ans + nCr(i + S - 1, S - 1) * pow(25, i, M) * pow(26, K - i, M)) % M
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
a_dict = {}
a_sum = 0
for a in a_list:
    a_sum += a
    if a in a_dict:
        a_dict[a] += 1
    else:
        a_dict[a] = 1

q = int(input())

for i in range(q):
    b, c = map(int,input().split())
    convert_num = a_dict.get(b)
    diff = 0
    if convert_num:
        diff = convert_num * (c - b)
        if a_dict.get(c):
            a_dict[c] = a_dict[c] + a_dict[b] 
            a_dict.pop(b)
        else:
            a_dict[c] = a_dict[b]
    a_sum += diff
    print(a_sum)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
AC = Counter(A)
S = sum(A)
Q = int(input())
for _ in range(Q):
  B,C = map(int, input().split())
  if B not in AC:
    pass
  else:
    count = AC[B]
    S = S + (C-B)*count
    if C in AC:
      AC[C] += count
    else:
      AC[C] = count
  print(S)

Submission status: Wrong Answer

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

    for i in range(Q):
        B, C = tuple(BC[i])
        for j in range(N):
            if A[j] == B:
                A[j] = C
            if A[j] > B:
                break
        A = sorted(A)
        print(sum(A))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
for i in range(1, n+1):
    count = 0
    for j in (1, i+1):
        if i % j == 0:
            count += 1
            
    ans = i * count
print(ans)

Submission status: Wrong Answer

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

    for i in range(Q):
        B, C = BC[i][0], BC[i][1]
        for j in range(len(A)):
            if A[j] == B:
                A[j] = C
        print(sum(A))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = 1
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)

Submission status: Wrong Answer

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    dic[B] = 1
    dic[C] = 0
    sums += dic[B] * (C - B)
    print(sums)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
a = [0]*10**7
x = math.sqrt(10)
b = math.floor(x)
l = 0
for i in range(1,n+1):
  x = math.sqrt(i)
  b = math.floor(x)
  for j in range(1,b+1):
    if(i%j==0 and i//j == j):
      a[i-1] +=1
    elif(i%j==0):
      a[i-1] +=2
for k in range(n):
  l = l + a[k]*(k+1)
print(l)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,n+1):
  dp[i]=a.count(i)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(1):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(min(q,20)):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(min(q,1000)):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(min(q,20000)):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(min(q,50000)):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
print(int(N*(N-1)/2))

Submission status: Wrong Answer

Solution code:
# Problem D - Replacing

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

# initialization
nums = [0] * (10**5 + 1)
cur_sum = sum(a_nums)
for a in a_nums:
    nums[a] += 1

# count
for i in range(Q):
    b, c = map(int, input().split())
    b_count = nums[b]
    cur_sum = cur_sum - b_count*b + b_count*c
    print(cur_sum)
    nums[c] += b_count

Submission status: Wrong Answer

Solution code:
input()
arr = [int(num) for num in input().strip().split(' ')]
q = int(input())

queries = []

for i in range(q):
    query = [ int(num) for num in input().strip().split(' ')]
    queries.append(query)

nums = dict()
summ = 0

for number in arr:
    summ += number
    if number not in nums:
        nums[number] =  1
    else:
        nums[number] += 1

for query in queries:
    curr, new = query
    if curr not in nums: continue
    count = nums[curr]
    diff = (new-curr)*count
    summ += diff
    del nums[curr]
    if new not in nums:
        nums[new] = count
    else:
        nums[new] += count
    print(summ)

Submission status: Wrong Answer

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
n = int(input())
a = list(map(int, input().split()))
q = int(input())
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100000
sum = 0
ans = [0]*q

for i in range(100000):
    count[i] = a.count(i+1)

for j in a:
    sum += j

for k in range(q):
    diff = req[k][1] - req[k][0]
    diff = diff * count[req[k][0] - 1]
    count[req[k][1] - 1] += count[req[k][0] - 1]
    count[req[k][0] - 1] = 0
    sum += diff
    ans[k] = sum

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
p = [list(map(int, input().split())) for _ in range(q)]
from collections import Counter
c = Counter(a)
for i in range(q):
    if p[i][0] in c:
        c[p[i][1]] += c[p[i][0]]
        c[p[i][0]] = 0
        print(c)
    ans = 0
    for i in c:
        ans += c[i] * 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())

sumA = sum(A)
cnt_num = np.zeros(10**5 + 1)

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

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

A = {}
for i in range(n):
  A = dict(Counter(A) + Counter({a[i]:1}))

def sumA(A):
  ans = 0
  for x in A.keys():
    ans += x * A[x]
  return ans

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    A = dict(Counter(A) + Counter({c:A[b]}))
    A = dict(Counter(A) - Counter({b:A[b]}))
    print(sumA(A))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

A = {}
for i in range(n):
  A = dict(Counter(A) + Counter({a[i]:1}))

ans = 0
for x in A.keys():
  ans += x * A[x]

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    ans -= b * A[b]
    ans += c * A[b]
    A = dict(Counter(A) + Counter({c:A[b]}))
    A = dict(Counter(A) - Counter({b:A[b]}))
    print(ans)

Submission status: Wrong Answer

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

base = sum(A)
changed = {}

Q = int(input())

for i in range(Q):
  B, C = map(int, input().split())
  # Aに現れるBの個数xがわかればよい
  x = A.count(B)
  for key, value in changed.items():
    if value == B:
      # print(f'changed {key} to {value} earlier')
      x += A.count(key)
  base += x*(C-B)
  print(base)
  changed[B] = C

Submission status: Wrong Answer

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

A = {}
for i in range(n):
  if a[i] in A.keys():
    A[a[i]] += 1
  else:
    A[a[i]] = 1

ans = 0
for x in A.keys():
  ans += x * A[x]

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    div = (A[b] * c) - (A[b] * b)
    ans += div
    if c in A.keys():
      A[c] += A[b]
      A[b] = 0
    else:
      A[c] = A[b]
      A[b] = 0  
    print(ans)

Submission status: Wrong Answer

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()
nums = [0]*10**6
for i in range(n):
  nums[a[i]]+=1
q=ni()
ans=0
for i in range(len(nums)):
  ans+=nums[i]*i

for i in range(q):
  b,c=nm()
  ans-=nums[b]*b
  ans+=nums[c]*c
  nums[c]+=nums[b]
  nums[b]=0
  print(ans)

Submission status: Wrong Answer

Solution code:
import os
import sys
from io import BytesIO, IOBase
from math import gcd
from collections import Counter as cc

def main():
    n=int(input())
    l=list(map(int,input().split()))
    sm=sum(l)
    cnt=cc(l)
    q=int(input())
    for _ in range(q):
        p,r=map(int,input().split())
        sm-=(p*cnt[p])
        cnt[r]+=cnt[p]
        sm+=(r*cnt[r])
        cnt[p]=0
        print(sm)
        


    


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
n=sum(A)
M=[]
P=[]
for _ in range(int(input())):
    B,C=map(int,input().split())
    if P.count(B)>0:
        n=n-P.count(B)*M[P.index(B)]*(B-C)
    else:
        n=n-A.count(B)*(B-C)
    if A.count(B)>0:
        M.append(A.count(B))
        P.append(C)
    print(n)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
D = {}
res = 0
for a in A:
    res += a
    if a in D.keys():
        D[a] += 1
    else:
        D[a] = 1

q = int(input())


for cq in range(q):
    b, c = map(int, input().split())
    if b in D.keys():
        res += (c - b) * D[b]
        if c in D.keys():
            D[c] += D[b]
        else:
            D[c] = D[b]
    print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
cnt = [0] * int(1e5 + 10)
sum=0
for i in a_list:
 cnt[i] +=1
 sum+=i
  
q=int(input())
ans=0
 
for j in range(q):
 x,y=map(int,input().split())
 sum+=(y-x)*cnt[x]
 cnt[y]+=cnt[x]
 cnt[y]=0
 print(sum)

Submission status: Wrong Answer

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
cnt = [0] * int(1e5 + 10)
sum=0
for i in a_list:
 cnt[i] +=1
 sum+=i
  
q=int(input())
ans=0
 
for j in range(q):
 x,y=map(int,input().split())
 sum+=(y-x)*cnt[x]
 cnt[x]+=cnt[y]
 cnt[y]=0
 print(sum)