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:
#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    CC = A==B
    NC = CC==False
    A = A*(NC) + C*CC
    asum += np.count_nonzero(CC) *(C-B)
    print(asum)
    # 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 = [0]*100000

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

for i in range(q):
    b,c = map(int,input().split())
    x = cnt[b-1]
    cnt[b-1] = 0
    cnt[c-1] = cnt[c-1] + x
    s += (c-b)*x
    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())

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())
    x = cnt[b-1]
    cnt[b-1] = 0
    cnt[c-1] = cnt[c-1] + x
    ans = 0
    for s in range(100000):
        ans += (s+1)*cnt[s]
        
    print(ans)

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 = []
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
  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)
for i in range(Q):
  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())
ls = input()
q = int(input())
for _ in range(q):
    b, q = map(str, input().split())
    ls = ls.replace(b, q)
    print(sum(map(int, ls.split())))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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


    for b, c in BC:
        A = [a if a != b else c for a in A]
        print(sum(A))


if __name__ == "__main__":
    main()

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
  else:
    num_a[b[i]] = a.count(b[i])
num_b = []
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
  else:
    num_b.append(num_a[b[i]])
    num_a[c[i]] += num_a[b[i]]
    num_a[b[i]] = 0
sum = sum(a)
for i in range(Q):
  sum = sum - b[i]*num_b[i] +c[i]*num_b[i]
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
Q = int(input())
for _ in range(Q):
  B,C = input().split()
  A = A.replace(B,C)
  S = [int(a) for a in list(A.split())]
  print(sum(S))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
Q = int(input())
for _ in range(Q):
  B,C = input().split()
  A = A.replace(B,C)
  S = 0
  listA = list(A.split())
  for a in listA:
    S += int(a)
  print(S)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_set = set(A)
Q = int(input())
dis_list = []
count = {}
temp_sum = 0
for a in A_set:
    count[a] = A.count(a)
    temp_sum += a * count[a]
for i in range(Q):
    s = list(map(int, input().split()))
    dis_list.append(s)
for j in dis_list:
    if j[0] in count:
        temp_sum += (j[1] - j[0])* count[j[0]]
        if j[1]in count:
            count[j[1]] += count[j[0]]
        else:
            count[j[1]] = count[j[0]]
        count[j[0]] = 0
    print(temp_sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*a, = map(int, input().split())
q = int(input())
d = [0]*(10**5+1)
for i in a:
    d[i] += 1
for _ in [0]*q:
    b, c = map(int, input().split())
    d[c] += d[b]
    d[b] = 0
    print(sum(i*j for i, j in enumerate(d)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    ans = sum(a)
    q = int(input())
    d = dict((x,a.count(x)) for x in set(a))
    # Counterを敢えて使わないでやってみる
    # d = Counter(a)
    for _ in range(q):
        b, c = map(int, input().split())
        # cの値がない場合の対処
        d.setdefault(c, 0)
        # bの値がない場合の対処
        d[c] = d.get(c) + d.get(b, 0)
        # 差分はbの個数 * (c-b) 
        diff = d.get(b, 0) * (c-b)
        d[b] = 0
        ans += diff
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*a, = map(int, input().split())
q = int(input())
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
for _ in [0]*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
    print(sum(i*j for i, j in d.items()))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
L=[0]*(100001)
#何回その数字が出てきたかのリスト
ans=0
for a in A:
  L[a]+=1
  #aが出てきた数（2回出てきたら2になる）
  ans=sum(A)
Q=int(input())
for i in range(Q):
  B,C=map(int, input().split())
  #Bの数だけ（C-B)足す、BはCに置き換わるのでLB＝０になおす。
  L[C]+=L[B]
  ans+=L[B]*(C-B)
  L[B]=0
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
L=[0]*(100001)
#何回その数字が出てきたかのリスト
ans=0
for a in A:
  L[a]+=1
  #aが出てきた数（2回出てきたら2になる）
  ans=sum(A)
Q=int(input())
for i in range(Q):
  B,C=map(int, input().split())
  #Bの数だけ（C-B)足す、BはCに置き換わるのでLB＝０になおす。
  key=L[B]
  L[C]+=key
  ans+=key*(C-B)
  L[B]=0
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
arr = list(map(int,input().split()))
cnt = defaultdict(int)

for val in arr:
    cnt[val] += 1
    sums = sum(arr)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
arrange = input()
Q = int(input())


for i in range(Q):
    task = input().split()
    arrange = arrange.replace(task[0], task[1])
    answer = sum([int(i) for i in arrange.split()])
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
N = int(input())
a=list(map(int, input().split()))
Q = int(input())
b=[]
c=[]
for i in range(Q):
  b1,c1=map(int, input().split())
  b.append(b1)
  c.append(c1)
def index(lst, e):
    i = bisect_left(lst, e)
    if i == len(lst):
        raise ValueError
    if lst[i] != e:
        raise ValueError
    return i
for i in range(Q):
  first=0
  last = 0
  a.sort()
  if b[i] not in a:
    print(sum(a))
  else:
    last = a.count(b[i])
    first = index(a,b[i])
    del a[first:first+last]
    list_c = [c[i]]*last
    a = a+list_c
    print(sum(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())

num_dict = dict()
for i in a:
    if i in num_dict.keys():
        num_dict[i] += 1
    else:
        num_dict[i] = 1

for i in range(q):
    ans = 0
    if b[i] in num_dict:
        if c[i] in num_dict:
            num_dict[c[i]] += num_dict.pop(b[i])
        else:
            num_dict[c[i]] = num_dict.pop(b[i])
    for k, v in num_dict.items():
        ans += k*v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
N = int(input())
a=list(map(int, input().split()))
Q = int(input())
b=[]
c=[]
def index(lst, e):
    # Step 1. bisect_left して
    i = bisect_left(lst, e)
    
    # Step 2. 変な値を ValueError で弾いて
    if i == len(lst):
        raise ValueError
    if lst[i] != e:
        raise ValueError
    
    # Step 3. 値を返す。
    return i
for i in range(Q):
  b1,c1=map(int, input().split())
  b.append(b1)
  c.append(c1)

a.sort()
for i in range(Q):
  first=0
  last = 0
  a.sort()
  if b[i] not in a:
    print(sum(a))
  else:
    last = a.count(b[i])
    first = index(a,b[i])
    del a[first:first+last]
    list_c = [c[i]]*last
    a = a+list_c
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    A = [c if a == b else a for a in A]
    print(np.sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    A = [c if a == b else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)
B = np.array(B)
C = np.array(C)
for i in range(q):
    A = [C[i] if a == B[i] else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dis_list = []
for i in range(Q):
    s = list(map(int, input().split()))
    dis_list.append(s)
for j in range(len(dis_list)):
    for k in range(len(A)):
        if A[k] == dis_list[j][0]:
            A[k] = dis_list[j][1]
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(q):
    b,c = map(int,input().split())
    if b in a:
        a[c] = a[b] + a[c]
        del a[b]
    y = [x*a.get(x) for x in a]
    s.append(sum(y))

for k in range(q):
    print(s[k])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [list(map(int,input().split())) for _ in range(Q)]
    dict = {}

    for i in range(1,10**5+1):
    #for i in range(1,6):
        dict[i] = 0

    for i in range(N):
        dict[A[i]] += 1

    for i in range(Q):
        dict[B[i][1]] += dict[B[i][0]]
        dict[B[i][0]] = 0
        #print(dict)
        sum = 0
        for i in range(1,10**5+1):
        #for i in range(1,6):
            if dict[i] != 0:
                sum += dict[i]*i
        print(sum)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [list(map(int,input().split())) for _ in range(Q)]
    dict = {}

    for i in range(1,10**5+1):
    #for i in range(1,6):
        dict[i] = 0

    for i in range(N):
        dict[A[i]] += 1

    for i in range(Q):
        dict[B[i][1]] += dict[B[i][0]]
        dict[B[i][0]] = 0
        #print(dict)
        sum = 0
        for i in range(1,10**5+1):
        #for i in range(1,6):
            sum += dict[i]*i
        print(sum)

main()

Submission status: Time Limit Exceeded

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

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

for i in range(q) : 
    ans += (bc[i][1] - bc[i][0]) * many[bc[i][0] - 1]
    print(ans)
    many[bc[i][1]-1] += many[bc[i][0]-1]
    many[bc[i][0]-1] =0

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) 
A = list(int(i) for i in input().split())  
Q = int(input()) 
B_C = [[int(i) for i in input().split()] for i in range(Q)] 
for j in range(Q):
  #print(j)
  A = [B_C[j][1] if i == B_C[j][0] else i for i in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

goukei_AAA = 0
for j in AAA:
  goukei_AAA = goukei_AAA + j

for i in range(0, Q):
    BC = list(map(int, input().split()))
    if not BC[0] in AAA:
        print(goukei_AAA)
        pass
    else:
        how_many_changes = AAA.count(BC[0])

        AAA = [BC[1] if m == BC[0] else m for m in AAA]
        goukei_AAA = goukei_AAA + (how_many_changes * (BC[1] - BC[0]))
        print(goukei_AAA)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
b_list = [list(map(int, input().split())) for _ in range(q)]
 
j = 0
while j < q:
  a_list = [b_list[j][1] if i == b_list[j][0] else i for i in a_list]
  s = sum(a_list)
  print(s)
  j += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
array = [list(map(int, input().split())) for _ in range(Q)]
#variable
count = []
number = []
S = []
n = 0

A.sort()
while True:
    number.append(A[n])
    count.append(A.count(number[len(number)-1]))
    n = n + count[len(count)-1]
    if n >= N or count[0] == N:
        break
    
while len(S)  < Q :
    n = 0
    number = [ array[len(S)][1]if a == array[len(S)][0] else a for a in number]
    for (a, b) in zip(number, count):
        n = n + a*b
    S.append(n)
for i in S[0:]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
array = [list(map(int, input().split())) for _ in range(Q)]
#variable
count = []
number = []
S = []
n = 0

A.sort()
while True:
    number.append(A[n])
    count.append(A.count(number[len(number)-1]))
    n = n + count[len(count)-1]
    if n >= N or count[0] == N:
        break
    
while len(S)  < Q :
    n = 0
    for i in range(len(number)):
        #if number[i] == array[len(S)][0]:
        number = [ array[len(S)][1]if a == array[len(S)][0] else a for a in number]
        n = n + number[i] * count[i]
    S.append(n)
for i in S[0:]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
ans = sum(A)
nums = [A.count(num) for num in range(10 ** 5 + 1)]

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

Submission status: Time Limit Exceeded

Solution code:
import copy

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("{}".format(b))
#print("{}".format(c))

for i in range(q):
    _a = [c[i] if j == b[i] else j for j in a]
    #print("_a = {}".format(_a))
    a = copy.copy(_a)
    print("{}".format(sum(a)))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

n = int(input())

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

dic = defaultdict(int)
for num in nums:
    dic[num] += 1 


answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    dic[c] += dic[b]
    dic[b] = 0

    answer = 0
    for i, v in dic.items():
        answer += i * v

    answers.append(answer)

for answer in answers:
    print(answer)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    B,C = map(int,input().split())
    A = [C if a == B else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        a = [c if i == b else i for i in a]
        ans = sum(a)
        print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
array = [list(map(int, input().split())) for _ in range(Q)]
#variable
count = []
number = []
S = []
n = 0

A.sort()
while True:
    number.append(A[n])
    count.append(A.count(number[len(number)-1]))
    n = n + count[len(count)-1]
    if n >= N or count[0] == N:
        break
    
while len(S)  < Q :
    n = 0
    for i in range(len(number)):
        if number[i] == array[len(S)][0]:
            number[i] = array[len(S)][1]
        n = n + number[i] * count[i]
    S.append(n)
for i in S[0:]:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
nb=int(input())
liste = [int(x) for x in input().split()]
liste2= [0]*((10**5)+1)
for loop in range(len(liste)):
  liste2[liste[loop]] +=1
nba = int(input())
somme = sum(liste)
for loop in range(nba):
  n,r = map(int,input().split())
  roger = liste2[n]
  for loop in range(len(liste)):
    if liste[loop]==n:
      liste[loop] = r
  diff = r-n
  somme += diff * roger
  liste2[r] += liste2[n]
  liste2[n] = 0
  print(somme)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 21 23:57:41 2020

@author: Administrator
"""
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
while(Q):
    B,C=map(int,input().split())
    A.sort()
    lo = 0
    hi = N-1
    while(lo<hi):
        mid = lo + (hi-lo)//2
        if A[mid]>=B:
            hi = mid
        else:
            lo = mid + 1
    #print(lo)
    while(lo<N and A[lo]==B):
        A[lo]=C
        lo += 1
    #print(A)
    print(sum(A))
    Q -= 1

Submission status: Time Limit Exceeded

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

d = cl.Counter(a)
v, c = zip(*d.most_common())
v = list(v)
c = list(c)
sum = 0
for m in range(n):
    sum += a[m]
for i in range(q):
    w = bc[i][0]
    e = bc[i][1]
    if w in v:
        sum += (e-w) * c[v.index(w)]
        if e in v:
            c[v.index(e)] += c[v.index(w)]
            c[v.index(w)] = 0
        else:
            v[v.index(w)] = e
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
def sousa(target_list, before, after):
    sum = 0
    for i in range(len(target_list)):
        if target_list[i] == before:
            target_list[i] = after
        sum += target_list[i]
    return target_list, sum


def main():
    _ = input()
    target_list = list(map(int, input().split()))
    sousa_list = list()
    for i in range(int(input())):
        sousa_list.append(list(map(int, input().split())))

    for i in range(len(sousa_list)):
        target_list, summary = sousa(target_list, sousa_list[i][0], sousa_list[i][1])
        print(summary)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
vl = []
for i in range(q):
  v = list(map(int, input().split()))
  vl.append(v)
toa = sum(a)

sal = []
for i in range(len(vl)):
  ac = a.count(vl[i][0])
  dv = vl[i][1] - vl[i][0]
  toa += dv * ac
  for j in range(len(a)):
    if a[j] == vl[i][0]:
      a[j] = vl[i][1]
  sal.append(toa)

for i in sal:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
q=int(input())
before=[]
after=[]
for i in range(q):
    a,b=input().split()
    before.append(int(a))
    after.append(int(b))
    
for i in range(q):
  if before[i] in l:
    l=[after[i] if j == before[i] 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()))
q=int(input())
before=[]
after=[]
for i in range(q):
    a,b=input().split()
    before.append(int(a))
    after.append(int(b))
    
for i in range(q):
  l=[after[i] if j == before[i] else j for j in l]
  print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
a = list(map(str, input().split()))
q = int(input())
vl = []
for i in range(q):
  v = list(map(int, input().split()))
  vl.append(v)
sal = []
for i in range(len(vl)):
  ta = []
  ta = copy.deepcopy(a)
  for j in range(len(ta)):
    if ta[j] == str(vl[i][0]):
      ta[j] = str(vl[i][1])
  sal.append(ta)
  a = copy.deepcopy(ta)
ssal = []
for i in range(len(sal)):
  smsal = 0
  for j in range(len(sal[i])):
    smsal += int(sal[i][j])
  ssal.append(smsal)
for i in ssal:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC=[]
for i in range (0, Q):
    BC.append([int(i) for i in input().split()])
"""
for bc in BC:
    bc = [int(i) for i in bc]
"""
#print(BC)
sumnow = sum(A)

for bc in BC:
    count = 0
    yoso = set(A)
    if bc[0] in yoso:
        for i in range(0,len(A)):
            if A[i] == bc[0]:
                A[i] = bc[1]
                count += 1
    #  print("A:",A)
        sumnow += (bc[1]-bc[0])*count
    print (sumnow)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC=[]
for i in range (0, Q):
    BC.append([int(i) for i in input().split()])
"""
for bc in BC:
    bc = [int(i) for i in bc]
"""
#print(BC)
sum = sum(A)

for bc in BC:
    count = 0
    yoso = set(A)
    if bc[0] in yoso:
        for i in range(0,len(A)):
            if A[i] == bc[0]:
                A[i] = bc[1]
                count += 1
    #  print("A:",A)
        sum += (bc[1]-bc[0])*count
    print (sum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
L=[0]*(10**5)
for i in A:L[i-1]+=i
for i in range(Q):
    B,C=map(int,input().split())
    L[C-1]+=(L[B-1]//B)*C;L[B-1]=0
    print(sum(L))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC=[]
for i in range (0, Q):
    BC.append([int(i) for i in input().split()])
"""
for bc in BC:
    bc = [int(i) for i in bc]
"""
#print(BC)
sum = sum(A)
for bc in BC:
    count = 0
    if bc[0] == bc[1]:
        print(sum)
        continue
    else:
        for i in range(0,len(A)):
            if A[i] == bc[0]:
                A[i] = bc[1]
                count += 1
    #  print("A:",A)
        sum += (bc[1]-bc[0])*count
        print (sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC=[]
for i in range (0, Q):
    BC.append([int(i) for i in input().split()])
"""
for bc in BC:
    bc = [int(i) for i in bc]
"""
#print(BC)
sum = sum(A)
for bc in BC:
    count = 0
    for i in range(0,len(A)):
        if A[i] == bc[0]:
            A[i] = bc[1]
            count += 1
  #  print("A:",A)
    sum += (bc[1]-bc[0])*count
    print (sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC=[]
for i in range (0, Q):
    BC.append([int(i) for i in input().split()])
"""
for bc in BC:
    bc = [int(i) for i in bc]
"""
#print(BC)

for bc in BC:
    for i in range(0,len(A)):
        if A[i] == bc[0]:
            A[i] = bc[1]
  #  print("A:",A)
    print (sum(A))

Submission status: Time Limit Exceeded

Solution code:

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

sum = 0
for m in range(n):
    sum += a[m]
for i in range(q):
    w = bc[i][0]
    e = bc[i][1]
    cnt = a.count(w)
    a = [e if j==w else j for j in a]
    sum += (e-w)*cnt
    print(sum)

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(q):
    w = bc[i][0]
    e = bc[i][1]
    a = [e if j==w else j for j in a]
    sum = 0
    for k in range(n):
        sum += a[k]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
_,a=input(),map(int,input().split())
for _ in range(int(input())):
    org,rplc=input().split()
    org,rplc=int(org),int(rplc)
    
    a=[rplc if n==org else n for n in a]
    sumw=sum(i for i in a)
    print(sumw)
    sumw=0

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(int(input())):
    q , c = map(int,input().split())
    
    for i in range(x.count(q)):
        x[x.index(q)] = c
    print(sum(x))

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(int(input())):
    q , c = map(int,input().split())
    
    while x.count(q) != 0:
        x[x.index(q)] = c
    print(sum(x))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
_,a=input(),input().split()
for _ in range(int(input())):
    org,rplc=input().split()
    sum=0
    for i,n in enumerate(a):
        if n ==org:
            a[i]=rplc
        sum=sum+int(a[i])
    print(sum)
    sum=0

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
x = list(map(int,input().split()))
x = np.array(x)

for i in range(int(input())):
    q , c = map(int,input().split())
    x = np.where(x==q,c,x)
    print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
lists = list(map(int,input().split()))
Q=int(stdin.readline().rstrip())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
for i in range(Q): 
    lista = []*2
    lista =listA[i]
    Ar = [lista[1] if lista[0] == x else x for x in lists]
    print(sum(Ar))
    lists = Ar

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
#N = int(input())
lists = list(map(int,input().split()))
Q=int(stdin.readline().rstrip())
#Q = int(input())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
#print(listA[0])最初のリストが返される
for i in range(Q): 
    lista = []*2
    lista =listA[i]
    Ar= [lista[1] if lista[0] == x else x for x in lists]
    print(sum(Ar))
    lists = Ar
#print(N)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lists = list(map(int,input().split()))
Q = int(input())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
#print(listA[0])最初のリストが返される
for i in range(Q): 
    lista = []*2
    lista =listA[i]
    Ar= [lista[1] if lista[0] == x else x for x in lists]
    #print(Ar)
    print(sum(Ar))
    lists = Ar

Submission status: Time Limit Exceeded

Solution code:
from collections import *
from operator import mul

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


A_dic = Counter(A)

for _ in range(q):
    b, c = map(int, input().split())
    tmp = A_dic[b]
    A_dic[c] += tmp
    A_dic[b] = 0
    v, c = zip(*A_dic.most_common())
    ans = map(mul, v, c)
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
for i in range(q):
    b, c = map(int, input().split())
    if b in dic and c not in dic:
        dic[c] = dic[b]
        del dic[b]
    elif b in dic and c in dic:
        dic[c] += dic[b]
        del dic[b]
    sum_list = [x * y for (x, y) in dic.items()]
    print(sum(sum_list))

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 x==b else x for x in a]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import bisect
def ints(): return map(int, input().split())
def _int(): return int(input())


_ = _int()
arr = sorted(ints())
n = _int()
changes = []
for _ in range(n):
    target, desired = ints()
    changes.append((target, desired))

for target, desired in changes:
    idx = bisect.bisect_left(arr, target)
    for i, v in enumerate(arr[idx::], idx):
        if v != target:
            break
        arr[i] = desired
    arr = sorted(arr)
    print(sum(arr))

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