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()))
Q = int(input())

ans = []

B = [0] * Q
C = [0] * Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
    A = [C[i] if l == B[i] else l for l in A]
    ans.append(sum(A))

for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    N = int(input())
    a = [0] + list(map(int,input().split()))
    d = [0] + [a.count(i+1) for i in range(10**5)]
    Q = int(input())
    s = sum(a)
    for _ in range(Q):
        B,C = map(int,input().split())
        if d[B] != 0:
            tmp = d[B]
            d[B] = 0
            d[C] += tmp
            s = s - tmp*B + tmp*C
        print(s)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum1 = 0
  for j in range(len(list)):
    if list[j] == s:
      list[j] = t
    elif list[j] > s:
      break;
  sum1 = sum(list)
  list.sort()
  print(sum1)

Submission status: Time Limit Exceeded

Solution code:
import collections

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)

# print(N,A,Q,B,C)


a = []

a = dict(collections.Counter(A))

# print(a)


# d1 = {'k1': 1, 'k2': 2}
# d2 = {'k1': 100, 'k3': 3, 'k4': 4}

# d1.update(d2)
# print(d1)
# # {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

# これを用いれば良い。

for i in range(Q):
    # 変更後
    after = {B[i]: 0, C[i]: a.setdefault(B[i], 0) + a.setdefault(C[i], 0)}
    # print(a, after)
    a.update(after)

    s = 0
    for k in a:
        s += k * a[k]
    print(s)

# print(a)

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:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum1 = 0
  for j in range(len(list)):
    if list[j] == s:
      list[j] = t
  sum1 = sum(list)
  print(sum1)

Submission status: Time Limit Exceeded

Solution code:
def func(a,b,c):
	for j in range(len(a)):
		if a[j]==b:
			a[j]=c
		else:
			pass

n = int(input())
a = list(map(int,input().split()))
q = int(input())
for k in range(q):
	b,c = map(int,input().split())
	func(a,b,c)
	ans = sum(a)
	print(ans)

Submission status: Time Limit Exceeded

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

a = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    a[c] += a[b]
    del a[b]
    print(sum(a.elements()))

Submission status: Time Limit Exceeded

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

a = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    a[c] += a[b]
    del a[b]
    ans = [x * y for x, y in a.items()]
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
nb=int(input())
liste = [int(x) for x in input().split()]
nba = int(input())
somme = sum(liste)
for loop in range(nba):
  n,r = map(int,input().split())
  roger = liste.count(n)
  for loop in range(roger):
    liste.append(r)
  diff = r-n
  somme += diff * roger
  print(somme)

Submission status: Time Limit Exceeded

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

a = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    a[c] += a[b]
    a[b] = 0
    ans = [x * y for x, y in a.items()]
    print(sum(ans))

Submission status: Time Limit Exceeded

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

for i in range(q):
    for j in range(n):
        if a[j] == s[i][0]:
           a[j] = s[i][1]
    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):
  b, c = map(int, input().split())
  for i in range(n):
    x = a[i]
    if b == x:
      a[i] = c
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys

def L(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI():return list(map(int,sys.stdin.readline().rstrip().split()))
def LS():return list(sys.stdin.readline().rstrip().split())

import re
def solver(N,A,Q,BC):

    for b,c in BC:
        #print(A,':',b,'->',c)
        bef = '[\s]*'+str(b)
        aft = ' '+str(c)
        #print("bef,aft=",bef,aft)
        A = re.sub(bef, aft, A)
        #A = A.replace(' '+str(b), ' '+str(c))
        #print(A)
        print(sum(map(int,A.split())))


def main():
    N = I()
    #A = LI()
    A = L()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    #print("N,Q=",N,Q)
    #print("A=",A)
    #print("BC=",BC)

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
D=dict()
for i in A:
  if i in D:
    D[i]+=1
  else:
    D[i]=1
for _ in range(Q):
  b,c=map(int,input().split())
  if b in D:
    if c in D:
      D[c]+=D[b]
    else:
      D[c]=D[b]
    del D[b]
  r=0
  for i,j in D.items():
    r+=i*j
  print(r)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    B, C = list(map(int, input().split()))
    
    count= A.count(B)
    for n in range(count):
        index = A.index(B)
        A[index] = C
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
a_int = [int(x) for x in a]
a_sum = sum(a_int)
a_num = []
for i in range(100001):
    a_num.append(a_int.count(i))

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

    a_sum += (c - b) * n
    print(a_sum)

    a_num[b] = 0
    a_num[c] += n

Submission status: Time Limit Exceeded

Solution code:
nb=int(input())
liste = [int(x) for x in input().split()]
nba = int(input())
for loop in range(nba):
  n,r = map(int,input().split())
  for loop in range(len(liste)):
    if liste[loop]==n:
      liste[loop] = r
  print(sum(liste))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())+1
a = list(map(int, input().split()))
cnt = []

sum = 0
for i in range(100005):
    cnt.append(a.count(i))
    sum += i * cnt[i]

q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())+1
a = list(map(int, input().split()))
cnt = []

sum = 0
for i in range(100005):
    cnt.append(a.count(i))
    sum += i * cnt[i]

q = int(input())
ans = []

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

for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = 0
b = [0] * q
c = [0] * q

for i in range(n):
    ans += a[i]
    
for i in range(q):
    b, c = map(int, input().split())
    for j in range(n):
        if a[j] == b:
            a[j] = c
            ans  += c-b
    print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(q):
  for j in range(n):
    if A[j]==BC[i][0]:
      A[j]=BC[i][1]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
a_int = [int(x) for x in a]
a_sum = sum(a_int)
a_num = []
for i in range(100001):
    a_num.append(a_int.count(i))

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

for j in range(len(tmp)):
    b_tmp = tmp[j][0]
    c_tmp = tmp[j][1]

    n = a_num[b_tmp]
    
    a_sum += (c_tmp - b_tmp) * n
    print(a_sum)

    a_num[b_tmp] = 0
    a_num[c_tmp] += n

Submission status: Time Limit Exceeded

Solution code:
n = int(input())+1
a = list(map(int, input().split()))
cnt = []

for i in range(100005):
    cnt.append(0)
sum = 0
for i in range(100005):
    cnt[i] = a.count(i)
    sum += i * cnt[i]

q = int(input())
ans = []

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

for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = 0
b = [0] * q
c = [0] * q

    
for i in range(q):
    ans = 0
    b, c = map(int, input().split())
    for j in range(n):
        if a[j] == b:
            a[j] = c
        ans += a[j]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split(' ')
Q = int(input())
bc = [input().split() for _ in range(Q)]
A = [int(i) for i in A]
for i in range(Q):
    bc[i] = [int(j) for j in bc[i]]

ans = sum(A)
counter = 0
# print('-input end-')
# print(ans)
# print('=sum=')

for i in range(Q):
    # print('+++++++')
    # print(A)
    # print('+++++++')
    for j in range(N):
        if A[j] == bc[i][0]:
            A[j] = bc[i][1]
            counter = counter + 1
            # print('dif : ' + str(bc[i][1] - bc[i][0]))
            # print('counter : ' + str(counter))
    ans += counter*(bc[i][1] - bc[i][0])
    print(ans)
    counter = 0

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]


#print(N)
#print(A)
#print(Q)
#print(x)
#print(y)

#取得完了

#sum = sum(A)

#print(sum)

for i in range (Q):
  for j in range(N):
    if A[j] == B[i]:
      A[j] = C[i]
  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):
  b,c=list(map(int,input().split()))
  a=[c if i == b else i for i in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
def main():

    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    bc =[list(map(int, input().split())) for _ in range(q)]
    temp = [0] * (10**5 + 1)
    
    for i in a:
        temp[i] += i
    
    for i in range(q):
        b,c = bc[i][0],bc[i][1]
        temp[c] += (temp[b]//b) * c
        temp[b] = 0
        print(sum(temp))
 
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy

N = int(input())

target_list = list(map(int, input().split()))
check = [Counter(target_list)]
result_list = [sum(target_list)]


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check[index]:
        result = copy.deepcopy(check[index])
        change = result.pop(B)
        diff = change * (C - B)
        if C in result:
            result[C] = result[C] + change
        else:
            result[C] = change

    else:
        result = copy.deepcopy(check[index])
        diff = 0
    check.append(result)
    print(result_list[index] + diff)
    result_list.append(result_list[index] + diff)

Submission status: Time Limit Exceeded

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

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

    for k in range(N):
        if asses_list[k] == B:
            asses_list[k] = C
    total = 0
    for s in asses_list:
        s_int = int(s)
        total += s_int

    print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
q = int(input())
b = []
c = []
# diff = []
for i in range(q):
    _ = list(map(int, input().split()))
    c.append(_[1])
    b.append(_[0])

for i in range(q):
    for j in range(n):
        if b[i] == a[j]:
            a[j] = c[i]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = 0
b = [0] * q
c = [0] * q

for i in range(q):
    b[i], c[i] = map(int, input().split())
    
for i in range(q):
    ans = 0
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
        ans += a[j]
    print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    B, C = map(int, input().split())
    if B in A.keys():
        if C in A.keys():
            A[C] += A[B]
        else:
            A[C] = A[B]
        A[B] = 0
    ans = 0
    for k, v in A.items():
        ans += k*v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = 0
b = [0] * q
c = [0] * q

for i in range(q):
    b[i], c[i] = map(int, input().split())
    
for i in range(q):
    ans = 0
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
            
    for k in range(n):
        ans = ans + a[k]
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy

N = int(input())

target_list = map(int, input().split())
check = [Counter(target_list)]

for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check[index]:
        result = copy.deepcopy(check[index])
        if C in result:
            result[C] = result[C] + result.pop(B)
        else:
            result[C] = result.pop(B)
    else:
        result = copy.deepcopy(check[index])
    check.append(result)
    print(sum([a * b for a, b in result.items()]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = list(in_nn())
    Q = in_n()
    t = in_na()
    BC = list(zip(t, t))

    d = defaultdict(int)
    for i in range(N):
        d[A[i]] += 1

    for i in range(Q):
        b, c = BC[i]
        d[c] += d[b]
        d.pop(b)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum = 0
  for j in range(len(list)):
    if list[j] == s:
      list[j] = t
    sum += list[j]
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = map(int, input().split())
Q = int(input())
ops = []
for _ in range(Q):
  b, c = map(int, input().split())
  ops.append((b,c))

tot = []
cnt = Counter(A)
for b, c in ops:
  #print(cnt)
  #print(b, c)
  cnt[c] += cnt[b]
  cnt[b] = 0
  s = sum(k*v for k, v in cnt.items())
  print(s)

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 i in range(q)]
num = [0]*(10**5+1)
for i in a:
  num[i] = a.count(i)
sum_a = sum(a)

for i in range(q):
  dif = b[i][1]-b[i][0]
  #print(dif)
  sum_a += dif*num[b[i][0]]
  num[b[i][1]] += num[b[i][0]]
  num[b[i][0]] = 0
  print(sum_a)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
A = [int(x) for x in input().split()]#yoko
q = int(input())

ans = [0] * q

ans1 = sum(A)

A = str(A)

for i in range(q):

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

    if str(b) in A:
        ans1 -= b * A.count(str(b))
        ans1 += c * A.count(str(b))
        A = A.replace(str(b),str(c))
        #A = int(A)

    ans[i] = ans1



for i in range(q):
    print(ans[i])

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

for i in bc:
    str_a = " ".join(map(str, a))
    str_a = str_a.replace(str(i[0]), str(i[1]))
    a = list(map(int, str_a.split()))
    print(sum(a))

Submission status: Time Limit Exceeded

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

s = sum(A)
for i in range(0, Q):
    if B[i] == C[i]:
        print(sum(A))
        continue
    else:
        count = A.count(B[i])
        A = [j for j in A if j != B[i]]
        for _ in range(count):
            A.append(C[i])
        print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype = int)
Q = int(input())
lis = [0]* 10 ** 5
S = sum(A)

for i in range(10 ** 5):
  lis[i] = np.count_nonzero(A == i + 1)
  
for _ in range(Q):
  B, C = map(int, input().split())
  S += (C - B) * lis[B - 1]
  print(S)
  lis[C - 1] += lis[B - 1]
  lis[B - 1] = 0

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:
def run(n, seq, num, trials):

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

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

S=0
#まずはじめの合計求める

num=[0]
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num.append(cnt)
    if cnt!=0:
        S+=cnt*i
        

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    change=num[B]*(C-B)
    num[C]+=num[B]
    num[B]-=num[B]
    S+=change
    print(S)

Submission status: Time Limit Exceeded

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

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(10**5+1)
for i in range(10**5+1):
  numlist[i]=listA.count(i)
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  if numlist[B]!=0:
    sum=sum+(C-B)*numlist[B]
    # print(C)
    numlist[C]+=numlist[B]
    numlist[B]=0
    # print(numlist)
  print(sum)

Submission status: Time Limit Exceeded

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

s = sum(A)
for i in range(0, Q):
    count = A.count(B[i])
    A = [j for j in A if j != B[i]]
    for _ in range(count):
        A.append(C[i])
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

S=sum(A)
#まずはじめの合計求める

num=[0]
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num.append(cnt)

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    change=num[B]*(C-B)
    num[C]+=num[B]
    num[B]-=num[B]
    S+=change
    print(S)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
D=dict()
for i in A:
  if i in D:
    D[i]+=1
  else:
    D[i]=1
for i in range(Q):
  b,c=map(int,input().split())
  if b in D:
    if c in D:
      D[c]+=D[b]
    else:
      D[c]=D[b]
    D[b]=0
  r=0
  for i,j in D.items():
    r+=i*j
  print(r)

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)]
temp = [0] * (10**5 + 1)

for i in a:
    temp[i] += i

for i in range(q):
    b,c = bc[i][0],bc[i][1]
    temp[c] += (temp[b]//b) * c
    temp[b] = 0
    print(sum(temp))

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    count_list = [0] * 100000
    for i in list(set(seq)):
        count_list[i-1] = seq.count(i)
    
    _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 math

n = int(input())
aL = list(map(int, input().split(" ")))
q = int(input())
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

sums = [0 for i in range(10**5 + 1)]

for a in aL:
    sums[a] += 1

for b, c in bcL:
    sums[c] += sums[b]
    sums[b] = 0
    _sum = 0
    for i, s in enumerate(sums):
        _sum += i * s
    print(_sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split(' ')
Q = int(input())
bc = [input().split() for _ in range(Q)]
A = [int(i) for i in A]
for i in range(Q):
    bc[i] = [int(j) for j in bc[i]]
# print(bc)

for i in range(Q):
    for j in range(len(A)):
        if A[j] == bc[i][0]:
            A[j] = bc[i][1]
    ans = sum(A)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    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)
    
    _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())
A = list(map(int, input().split()))
Q = int(input())
lis = [0]* 10 ** 5
S = sum(A)

for i in range(10 ** 5):
  lis[i] = A.count(i + 1)
  
for _ in range(Q):
  B, C = map(int, input().split())
  S = S + (C - B) * lis[B - 1]
  print(S)
  lis[C - 1] += lis[B - 1]
  lis[B - 1] = 0

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]

list = [0] * 100001
sum = 0
for i in range(100001):
    list[i] = A.count(i)

for j in range(N):
    sum += A[j] 

for k in range(Q):
    sum += list[B[k]] * (C[k] -B[k])
    list[C[k]] += list[B[k]]
    list[B[k]] = 0
    print(sum)

Submission status: Time Limit Exceeded

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

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(10**5+1)
for i in range(10**5+1):
  numlist[i]=listA.count(i)
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  sum=sum+(C-B)*numlist[B]
  # print(C)
  numlist[C]+=numlist[B]
  numlist[B]=0
  # print(numlist)
  print(sum)

Submission status: Time Limit Exceeded

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

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

S=sum(A)
#まずはじめの合計求める

num=[0]*(10**5+1)
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num[i]=cnt

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    change=num[B]*(C-B)
    num[C]+=num[B]
    num[B]-=num[B]
    S+=change
    print(S)

Submission status: Time Limit Exceeded

Solution code:
#D
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
mp = defaultdict(int)
for i in a:
    mp[i] += 1
q = int(input())
for _ in range(q):
    #print(mp)
    s = 0   
    b,c=map(int,input().split())
    if c in mp:
        mp[c] = mp[c]+mp[b]
        mp.pop(b)
    elif b in mp:
        mp[c] = mp.pop(b)
        
    for i in mp:
        s += (mp[i])*i
    print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# 日本語を入力できるようにするために上記一文が必要



N = int(input() )
num_list = list( map(int, input().split() ) )
Q = int(input() )
ans_list = []

for i in range(Q):
        b, c = map(int, input().split() )
        num_list = [c if i == b else i for i in num_list]
        ans_list.append(sum(num_list))


for x in ans_list:
        print(x)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]

list = [0] * 100001
sum = 0
for i in range(100001):
    list[i] = A.count(i)

for i in range(len(A)):
    sum += A[i] 

for i in range(Q):
    sum = list[B[i]] * (C[i] -B[i]) + sum
    list[C[i]] = list[C[i]] + list[B[i]]
    list[B[i]] = 0
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
aL = list(map(int, input().split(" ")))
q = int(input())
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

d = {}
for i in range(n):
    if aL[i] in d:
        d[aL[i]] += 1
    else:
        d[aL[i]] = 1

for b, c in bcL:
    if b in d.keys():
        if c in d:
            d[c] += d[b]
        else:
            d[c] = d[b]
        del d[b]

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
q = int(input())
hako = np.array([0]*(10**6))
ind = np.array(list(range(10**6)))
s = []

for i in a:
    hako[i] += 1

for i in range(q):
    b, c = [int(i) for i in input().split()]
#     S += (ind[c] - ind[b])*hako[b]
    hako[c] += hako[b]
    hako[b] = 0
    S = np.sum(hako*ind)
    s.append(S)
for i in range(q):
    print(s[i])

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())
    A = [C if B == i else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

    answer = sum(A)
    setA = set(A)
    numNumberList = [0] * 100001

    for Ai in setA:
        numNumberList[Ai] = A.count(Ai)    

    for i in range(Q):
        B, C = list(map(int, sys.stdin.readline().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

count = [0]*(10**5+1)
for i in range(len(A)):
  count[A[i]] += 1

A = list(set(A))
for i in range(Q):
  B, C = map(int, input().split())
  count[C] += count[B]
  count[B] = 0
  ans = 0
  for i in range(10**5+1):
    ans += count[i] * i
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
for t in range(q):
	b, c = map(int, input().split())
	for x in range(len(arr)):
		if arr[x] == b:
			arr[x] = c
	sum = 0
	for y in range(len(arr)):
		sum = sum + arr[y]
	print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,list(input().split())))
Q = int(input())
s = []
for i in range(Q):
    B,C = map(int,(input().split()))
    r = 0
    for j in range(N):
        if A[j] == B:
            A[j] = C
        r += A[j]
    s.append(r)
for i in range(Q):
    print(s[i])