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: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
S=sum(A)
Q=int(input())
for i in range(Q):
    B,C=map(int,input().split())
    S=S+(C-B)*A.count(B)
    A = [C if i ==B  else i for i in A]
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 21:04:33 2020

@author: naoki
"""

N = int(input()) # input the row A length "N" 
A = list(map(int,input().split())) # input the row of A 
Q = int(input()) # input the number of trying operation 
BC = [list(map(int,input().split())) for i in range(Q)]

for B,C in BC:
    S = 0 # Sum of all conponents of the row "A"
    A = [C if i == B else i for i in A]    
    for j in A:
        S+=j
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 21:04:33 2020

@author: naoki
"""

N = int(input()) # input the row A length "N" 
A = list(map(int,input().split())) # input the row of A 
Q = int(input()) # input the number of trying operation 
BC = [list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
    S = 0 # Sum of all conponents of the row "A"
    for j in range(N):
        if A[j] == BC[i][0]:
            A[j] = BC[i][1]
        S +=A[j]
    print(S)

Submission status: Time Limit Exceeded

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

dict_A = {}

for a in A:
  if a in dict_A:
    dict_A[a] += 1
  else:
    dict_A[a] = 1
    
for B, C in B_to_C:
  if B in dict_A:
    if C in dict_A:
      dict_A[C] += dict_A[B]
      del dict_A[B]
    else:
      dict_A[C] = dict_A[B]
      del dict_A[B]
      
  sums = 0
  for k, v in dict_A.items():
    sums += k*v
  print(sums)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_A = input().split()
q = int(input())
SUM = sum(map(int, list_A))
for i in range(q):
  b, c = input().split()
  dif = 0
  cnt = 0
  if b in list_A:
    flag = True
    while flag:
      try:
        Index = list_A.index(b)
        list_A[Index] = c
        cnt += 1
      except ValueError:
        flag = False
  else:
    pass
  dif = cnt*(int(c) - int(b))
  SUM += dif
  print(SUM)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_A = input().split()
q = int(input())
SUM = sum(map(int, list_A))
for i in range(q):
  b, c = input().split()
  dif = 0
  if b in list_A:
    num = list_A.count(b)
    list_A = [c if x == b else x for x in list_A]
    dif = num*(int(c) - int(b))
  else:
    pass
  SUM += dif
  print(SUM)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_A = input().split()
q = int(input())
for i in range(q):
  SUM = 0
  b, c = input().split()
  if b in list_A:
    list_A = [c if x == b else x for x in list_A]
  else:
    pass
  SUM += sum(map(int, list_A))
  print(SUM)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
Q = int(input())
b_c = [input().split() for i in range(Q)]

for i in range(Q):
    sum = 0
    A = [j.replace(b_c[i][0], b_c[i][1]) for j in A]
    for j in range(len(A)):
        sum += int(A[j])
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

x = np.zeros(100_001, dtype=int)
v = np.arange(100_001)

n = int(input())
for a in map(int, input().split()):
  x[a] += 1
q = int(input())
for _ in range(q):
  b, c = map(int, input().split())
  x[c] += x[b]
  x[b] = 0
  print(x @ v)

Submission status: Time Limit Exceeded

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

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

q = int(input())
result = []
for i in range(q):
    b, c = list(map(int, input().split()))
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    result.append(str(sum))
print('\n'.join(result))

Submission status: Time Limit Exceeded

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

count = {}
for a in A:
    if count.get(a):
        count[a] += 1
    else:
        count[a] = 1

q = int(input())
result = []
for i in range(q):
    b, c = list(map(int, input().split()))
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    result.append(str(sum))
print('\n'.join(result))

Submission status: Time Limit Exceeded

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

count = {}
for a in A:
    if count.get(a):
        count[a] += 1
    else:
        count[a] = 1

q = int(input())
B = []
C = []
for i in range(q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)
for i in range(q):
    b = B[i]
    c = C[i]
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
a = input()
A = list(map(int,input().split()))
N = int(input())
dic = collections.Counter(A)
for i in range(N):
    ans = 0
    b,c = map(int,input().split())
    p = dic[b]
    dic[c] += p
    dic[b] = 0
    k = list(dic.keys())
    v = list(dic.values())
    for i in range(len(k)):
        ans += k[i]*v[i]
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def binarySearch(array, key):
    l = 0
    r = N - 1
    pos = -1
    while l <= r and pos == -1:
        m = (l + r) // 2
        m_val = array[m]
        if m_val == key:
            pos = m
        elif m_val > key:
            r = m - 1
        else:
            l = m + 1
    return pos

def upperIdx(array, pos):
    if pos == N - 1:
        return pos
    else:
        while(array[pos] == array[pos + 1]):
            if pos >= N - 2:
                break
            else:
                pos += 1
        if array[pos] == array[N - 1]:
            pos += 1 
        return pos

def lowerIdx(array, pos):
    if pos == 0:
        return pos
    else:
        while(array[pos] == array[pos - 1]):
            if pos <= 1:
                break
            else:
                pos -= 1
        if array[pos] == array[0]:
            pos -= 1
        return pos

for i in range(Q):
    A = sorted(A)
    Bi, Ci = map(int, input().split())
    position = binarySearch(A, Bi)
    if position == -1:
        print(sum(A))
    else:
        lower = lowerIdx(A, position)
        upper = upperIdx(A, position)
        C = [Ci] * (upper - lower + 1)
        A[lower:upper + 1] = C
        print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
  bc = list(map(int, input().split()))
  res = bc[0]
  for bit in range(n):
    if a[bit] == res:
      a[bit] = bc[1]
  print(sum(a))

Submission status: Time Limit Exceeded

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

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

    for j in a:
        if j == b:
            a[count] = c
        count += 1
    a_list = [_ for _ in a]
    ans.append(a_list)

for answer in ans:
    print(sum(answer))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
q = int(input())
for i in range(q):
  a,b = map(int,input().split())
  for j in range(n):
    if A[j] == a:
      A[j] = b
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
# 入力
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]


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

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

for bc in BC:
    b, c = bc
    array[c] += array[b]
    array[b] = 0

    total = 0
    for i in range(10 ** 5 + 1):
        total += i * array[i]
    print(total)

Submission status: Time Limit Exceeded

Solution code:
# 入力
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]


def replacement(orig, b, c):
    if orig == b:
        return c
    else:
        return orig


for bc in BC:
    b, c = bc
    A = [replacement(a, b, c) for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
se=list(map(int,input().split()))
q=input()
for i in range((int(q))):
  inp=input().split()
  se=[int(inp[1]) if i==int(inp[0]) else i for i in se]
  
  print(sum(se))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
p=list(map(int, input().split()))
Q=int(input())
for i in range(Q):
  N,K=(int(x) for x in input().split())
  p=[K if k == N else k for k in p]
  print(sum(p))

Submission status: Time Limit Exceeded

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

num = {}
for i in A:
    if i in num:
        num[i] += 1
    else:
        num[i] = 1
for i in range(Q):
    p, n = BC[i][0], BC[i][1]
    if p in num:
        if n in num:
            num[n] += num[p]
        else:
            num[n] = num[p]
        num[p] = 0
    ans = 0
    for k, v in num.items(): ans += k * v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(input())
a = list(map(int, input().split()))
'''
aa = ''.join(a)
aaa = str(aa)
#print(type(aa))
# print(a)
'''
aa = np.array(a)
q = int(input())

for i in range(q):
    b, c = map(int, input().split())
    aa = np.where(aa == b, c, aa) 
    print(np.sum(aa))
    #aaa.replace(str(b), str(c))
    #print(aaa)

#ab = map(int, aa)
#print(np.sum(aa))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(input())
number_count=[0]*100001
for i in range(100001):
    number_count[i]=number.count(i)

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(input())
number_count=[0]*100001
for i in range(100001):
    number_count[i]=number.count(i)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

l.sort()

m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    l = [b if j == a else j for j in l]

    print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    l = [b if j == a else j for j in l]

    print(sum(l))

Submission status: Time Limit Exceeded

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

ans = [0]*(100000+1)
for a in A:
    ans[a] += a

for i in range(Q):
    b, c = map(int, input().split())
    ans[c] += (ans[b]//b) * c
    ans[b] = 0
    print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
bc = []
for i in range(0, q):
  bc.append(list(map(int, input().split())))
for j in bc:
  if j[0] in arr:
    count = arr.count(j[0])
    for _ in range(0, count):
      ind = arr.index(j[0])
      arr[ind] = j[1]
  print(sum(arr))

Submission status: Time Limit Exceeded

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

ans = [0]*1000000
for a in A:
    ans[a] += a

for i in range(Q):
    b, c = map(int, input().split())
    ans[c] += (ans[b]//b) * c
    ans[b] = 0
    print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    for j in range(len(l)):
        if(l[j] == a):
            l[j] = b
    print(sum(l))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    sum = 0
    ope = list(map(int,input().split()))
    B = [i for i, x in enumerate(A) if x == ope[0]]
    for j in B:
        A[j] = ope[1]

    for j in A:
        sum += j
    X.append(sum)

for i in X:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
q = int(input())
count = 0
s = []

for i in range(q):
	count = 0
	b, c = map(int, input().split())
	for a in l:
		if a == b:
			l[count] = c
		count += 1
	s.append(sum(l))

[print(i) for i in s]

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
h = list(map(int,input().split()))
Q = int(input())
a = [[int(i) for i in input().split()] for _ in range(Q)]
c = 0
ans = []
for i in range(0, Q):
    h = [a[i][1] if j == a[i][0] else j for j in h] 
    c = sum(h)
    ans.append(c)
    print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
a_sum = []
for _ in range(q):
    b,c = map(int, input().split())
    for i in sorted(a_list, reverse=True):
        if i == b:
            a_list.remove(b)
            a_list.append(c)
    a_sum.append(sum(a_list))


for i in a_sum:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
h = list(map(int,input().split()))
Q = int(input())
a = [[int(i) for i in input().split()] for _ in range(Q)]
c = 0
ans = []
for i in range(0, Q):
    h = [a[i][1] if j == a[i][0] else j for j in h] 
    c = sum(h)
    ans.append(c)
for i in range(0, Q):
     print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
a_sum = []
for _ in range(q):
    b,c = map(int, input().split())
    for i in sorted(a_list, reverse=True): #ここのインデックスを逆にするのがヤマ
        if i == b:
            a_list.remove(b)
            a_list.append(c) #ここで追加するとインデックスがずれるため
        #print(a_list)
    a_sum.append(sum(a_list))


for i in a_sum:
    print(i)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    sum = 0
    ope = list(map(int,input().split()))
    for j in range(N):
        if A[j] == ope[0]:
            A[j] = ope[1]
        sum += A[j]
    X.append(sum)

for i in X:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for q in range(Q):
    newB, newC = map(int,input().split())
    B.append(newB)
    C.append(newC)

a_dict = {}
a_dict = defaultdict(int)
for t in A:
    count = A.count(t)
    a_dict[t] = count

all_sum = sum(A)
for i in range(Q):
    diff = (C[i] - B[i]) * a_dict[B[i]]
    all_sum += diff
    a_dict[C[i]] += a_dict[B[i]]
    print(all_sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for q in range(Q):
    newB, newC = map(int,input().split())
    B.append(newB)
    C.append(newC)

all_sum = sum(A)
rep = []
for i in range(Q):
    count = A.count(B[i])
    count_rep = rep.count(B[i])
    diff = (C[i] - B[i]) * (count + count_rep)
    all_sum += diff
    for t in range(count):
        rep.append(C[i])
    print(all_sum)

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    あなたは、 N 個の正整数 A1 , A2 , ⋯ , AN からなる数列 A を持っています。 
    あなたは、これから以下の操作を Q 回、続けて行います。 
    i 回目の操作では、値が Bi である要素すべてを Ci に置き換えます。 すべての 
    i ( 1 ≤ i ≤ Q) に対して、 i 回目の操作が行われた後の数列 A のすべての要素の和、 Si を求めてください。

    [制約]
    入力は全て整数
    1 ≤ N ,Q ,Ai ,Bi ,Ci ≤ 10^5 
    Bi ≠ Ci

    [入力例]
    4
    1 2 3 4
    3
    1 2
    3 4
    2 4
    
    [出力例]
    11
    12
    16
    '''

    #  TODO 下全部 LTE で不正解

    # count_dict = {i+1 : seq.count(i+1) for i in range(100000)}
    # count_list = [seq.count(i+1) for i in range(100000)]
    # count_list = [0 if i+1 not in seq else seq.count(i + 1) for i in range(100000)]
    # count_list = [count_list[i-1] if i not in seq or count_list[i-1] != 0 else seq.count(i) for i in seq]

    count_list = [0] * 100000
    for i in set(seq):
        count_list[i-1] = count_list[i-1] if count_list[i-1] != 0 else seq.count(i)
    
    # count_list = []
    # for i in range(100000):
    #     count_list.append(0 if i+1 not in seq else seq.count(i+1))

    _sum = sum((k+1)*v for k,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b-1]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b-1] = 0
        count_list[c-1] += _val

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: Time Limit Exceeded

Solution code:
import time
import random
def run(n, seq, num, trials):
    '''
    あなたは、 N 個の正整数 A1 , A2 , ⋯ , AN からなる数列 A を持っています。 
    あなたは、これから以下の操作を Q 回、続けて行います。 
    i 回目の操作では、値が Bi である要素すべてを Ci に置き換えます。 すべての 
    i ( 1 ≤ i ≤ Q) に対して、 i 回目の操作が行われた後の数列 A のすべての要素の和、 Si を求めてください。

    [制約]
    入力は全て整数
    1 ≤ N ,Q ,Ai ,Bi ,Ci ≤ 10^5 
    Bi ≠ Ci

    [入力例]
    4
    1 2 3 4
    3
    1 2
    3 4
    2 4
    
    [出力例]
    11
    12
    16
    '''

    #  TODO 下全部 LTE で不正解

    # count_dict = {i+1 : seq.count(i+1) for i in range(100000)}
    # count_list = [seq.count(i+1) for i in range(100000)]
    # count_list = [0 if i+1 not in seq else seq.count(i + 1) for i in range(100000)]
    # count_list = [count_list[i-1] if i not in seq or count_list[i-1] != 0 else seq.count(i) for i in seq]

    count_list = [0] * 100000
    # for i in seq:
    #     count_list[i-1] = count_list[i-1] if count_list[i-1] != 0 else seq.count(i)

    # seq = []
    # for i in range(10000):
    #     seq.append(random.randint(1, 100000))
    
    for i in set(seq):
        count_list[i-1] = seq.count(i)
    
    # count_list = []
    # for i in range(100000):
    #     count_list.append(0 if i+1 not in seq else seq.count(i+1))

    # s_time = time.time()
    _sum = sum((i+1)*v for i,v in enumerate(count_list))
    # print(time.time() - s_time)
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b-1]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b-1] = 0
        count_list[c-1] += _val

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: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
list_a = list(map(int, input().split()))

list_a.sort()

counts = []

for i in range(0, 100000):
    if (i+1) in list_a:
        counts.append(list_a.count(i+1))
    else:
        counts.append(0)

q = int(input())
ans = sum(list_a)

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

    ans += counts[b-1] * (c - b)

    counts[c-1] += counts[b-1]
    counts[b-1] = 0

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  input = sys.stdin.readline
  N=int(input())
  L=list(map(int,input().split()))
  Q=int(input())
  for i in range(Q):
    A,B=map(int,input().split())
    R=[B for K in L if K==A]
    L=[i for i in L if i!=A]
    L.extend(R)
    print(sum(L))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def run(n, seq, num, trials):
    count_list = np.zeros(100000, dtype=int)
    seq = np.array(seq)
    for i in seq:
        if count_list[i-1] > 0:
            continue
        count_list[i-1] = sum(np.where(seq == i,1,0))
    
    _sum = sum((k+1)*v for k,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b-1]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b-1] = 0
        count_list[c-1] += _val

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: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
  A,B=map(int,input().split())
  R=[B for K in L if K==A]
  L=[i for i in L if i!=A]
  L.extend(R)
  print(sum(L))

Submission status: Time Limit Exceeded

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

num = []
for i in range(100001):
  num.append(a.count(i))

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

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A_np =np.array(A,dtype=np.int64)
Q = int(input())
S =[0]*Q
for i in range(Q):
    b,c = (int(i) for i in input().split())
    A_np =np.where(A_np ==b,c,A_np)
    S[i] =np.sum(A_np)
    #print(A_np)


print(*S,sep='\n')

Submission status: Time Limit Exceeded

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

Q = int(input())
s = sum(A)
for i in range(Q):
  b, c = map(int, input().split())
  delta = c - b
  count = 0
  for i in range(len(A)):
    if A[i] == b:
      A[i] = c
      count += 1
  s += count * delta
  print(s)

Submission status: Time Limit Exceeded

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

Q = int(input())
for i in range(Q):
  b, c = map(int, input().split())
  s = 0
  A = [c if a == b else a for a in A]
  print(sum(A))

Submission status: Time Limit Exceeded

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

Q = int(input())
for i in range(Q):
  b, c = map(int, input().split())
  s = 0
  for i in range(len(A)):
    if A[i] == b:
      A[i] = c
    s += A[i]
  print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

q = int(input())

ans = sum(a)

for _ in range(q):
    b, c = map(int, input().split())
    diff = c - b
    b_num = sum(x == b for x in a)
    a = [c if i == b else i for i in a]
    ans += diff * b_num
    print(ans)

Submission status: Time Limit Exceeded

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

list_a.sort()

counts = []

for i in range(0, 100000):
    if (i+1) in list_a:
        counts.append(list_a.count(i+1))
    else:
        counts.append(0)

q = int(input())
ans = sum(list_a)

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

    ans += counts[b-1] * (c - b)

    counts[c-1] += counts[b-1]
    counts[b-1] = 0

    print(ans)

Submission status: Time Limit Exceeded

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

list_a.sort()

appeared = list(set(list_a))
counts = []

for i in range(len(appeared)):
    counts.append(list_a.count(appeared[i]))

q = int(input())
ans = 0

for i in range(len(appeared)):
    ans += appeared[i] * counts[i]

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

    if b in appeared:
        b_idx = appeared.index(b)
        b_count = counts[b_idx]
        ans += b_count * (c - b)
        print(ans)

        if c in appeared:
            c_idx = appeared.index(c)
            counts[c_idx] += counts[b_idx]
            appeared.pop(b_idx)
            counts.pop(b_idx)
        else:
            appeared.append(c)
            counts.append(counts[b_idx])
            appeared.pop(b_idx)
            counts.pop(b_idx)
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

input_num = int(input())
input_numline = list(map(int,input().split(' ')))
q = int(input())

op_list = []
for i in range(q):
    op_list.append(list(map(int,input().split(' '))))

maxnum = 100000

num_list = [0 for m in range(maxnum+1)]

for n in input_numline:
    num_list[n] += 1

for o in op_list:
    num_list[o[1]] += num_list[o[0]]
    num_list[o[0]] = 0

    ans = 0
    for j in range(maxnum+1):
        ans += j*num_list[j]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
    b, c, = map(int, input().split())
    while b in a:
        ind = a.index(b)
        a[ind] = c
    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
    b, c = map(int, input().split())
    count = list_a.count(b)
    list_a = [c if i == b else i for i in list_a]
    ans += count * (c-b)

    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
    b, c = map(int, input().split())
    list_a = [c if i == b else i for i in list_a]
    print(sum(list_a))

Submission status: Time Limit Exceeded

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())
for i in range(q):
    t=0
    for j in range(n):
        if a[j]==b[i]:
            a[j]=c[i]
        t+=a[j]
    print(t)

Submission status: Time Limit Exceeded

Solution code:
#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    if B in u:
        posB = np.where(u==B)
        asum += int(count[posB]) * (C-B)
        # print(u)
        # print(count)
        if not (C in u):
            u[posB] = C
        else :
            posC = np.where(u==C)
            count[posC] += count[posB]
            u = np.delete(u,posB)
            count = np.delete(count,posB)
    print(asum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    for i in range(N):
        if A[i] == B:
            A[i] = C
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
#Replacing
import numpy as np
N = int(input())
A = np.array(sorted(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    asum += count[u==B].sum() * (C-B)
    u = np.where(u==B,C,u)
    print(asum)

Submission status: Time Limit Exceeded

Solution code:
#Replacing
import numpy as np
N = int(input())
A = np.array(sorted(map(int, input().split())))
Q = int(input())
# u, count = np.unique(A,return_counts=True)
# asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    A = np.where(A==B,C,A)
    print(A.sum())

Submission status: Time Limit Exceeded

Solution code:
import collections as c

n = int(input())

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

q = int(input())

s = sum(a)

cnt = c.Counter(a)

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

for i 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: Time Limit Exceeded

Solution code:
import collections as c

n = int(input())

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

q = int(input())

s = sum(a)

cnt = [0]*100000

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

for i 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: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int, input().split()))
Q = int(input())
num_a =[0]*100001
num_b = []
sum_a = sum(a)
b=[]
c=[]
for i in range(Q):
  b1,c1=map(int, input().split())
  b.append(b1)
  c.append(c1)
  if b[i] not in a:
    continue
  elif num_a[b[i]]>0:
    continue
  else:
    num_a[b[i]] = a.count(b[i])
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  else:
    num_b.append(num_a[b[i]])
    num_a[c[i]] += num_a[b[i]]
    num_a[b[i]] = 0
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  print(sum_a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a=list(map(int, input().split()))
Q = int(input())
num_a =[0]*100001
b=[]
c=[]
for i in range(Q):
  b1,c1=map(int, input().split())
  b.append(b1)
  c.append(c1)
for i in range(Q):
  if b[i] not in a:
    continue
  elif num_a[b[i]]>0:
    continue
  else:
    num_a[b[i]] = a.count(b[i])
num_b = []
sum_a = sum(a)
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  else:
    num_b.append(num_a[b[i]])
    num_a[c[i]] += num_a[b[i]]
    num_a[b[i]] = 0
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  print(sum_a)