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:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
B = [0]*Q
C = [0]*Q

sum = np.sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    time = 0
    A = np.sort(A)
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
            time += 1
        elif A[k] > B[i]:
            break
        
    sum += (C[i]-B[i])*time

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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

x=[0]*(10**5)
for i in range(n):
    x[i]=a.count(i+1)*(i+1)


for _ in range(q):
    b,c=map(int,input().split())
    if x[b-1]!=0:
        x[c-1]+=x[b-1]//b*c
        x[b-1]=0
        print(sum(x))
    else:
        print(sum(x))

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

L=len(A)
for i in range(Q):
    for j in range(L):
        if A[j]==B[i]:
            A[j]=C[i]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    for i in range(Q):
        B, C = list(map(int,input().split()))
        D = [j for j, x in enumerate(A) if x == B]
        for k in D:
            A[k] = C
        print(sum(A))
 
 
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

N=int(input())
A=map(int,input().split())
Q=int(input())
numbers=[]
for _ in range(Q):
    B,C=map(int,input().split())
    numbers.append([B,C])
answers=[0 for _ in range(100001)]
for a in A:
    answers[a]+=a
for number in numbers:
    suuji=answers[number[0]]//number[0]
    answers[number[0]]=0
    answers[number[1]]+=suuji*number[1]

    answer=np.array(answers)
    answer=np.nonzero(answer)
    suuji=0
    for a in answer[0]:
        suuji+=answers[a]
    print(suuji)

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
res = sum(As)
dis = {i:As.count(i) for i in set(As)}
Q=int(input().rstrip('\n'))
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in dis:
    res += dis[B]*(C-B)
    if C in dis:
      dis[C] += dis[B]
    else:
      dis[C] = dis[B]
    dis[B]=0
  print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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):
  search = s[i][0]
  A.sort()
  b=bisect.bisect_left(A,search)
  c=bisect.bisect_right(A,search)
  for j in range(b,c):
    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())

b = []
c = []
for i in range(q):
    x, y = map(int, input().split())
    b.append(x)
    c.append(y)

for j in range(q):
    a = [c[j] if i == b[j] 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()))
S=sum(A)
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]
    S=sum(A)
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    b,c = map(int,input().split())
    a = [c if j == b else j for j in a]
    s.append(sum(a))

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

A.sort()

dict_a = dict()

for a in A:
    if not (a in dict_a):
        dict_a[a] = 1
    else:
        dict_a[a] += 1


Q = int(input())

B = []
C = []

sums = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)

for b, c in zip(B, C):
    if (b in dict_a):
        num = dict_a[b]
    
        if not (c in dict_a):
            dict_a[c] = num
        else:
            dict_a[c] += num

        dict_a.pop(b)

    keys = dict_a.keys()
    vals = dict_a.values()

    sumval = 0
    for k, v in zip(keys, vals):
        sumval += k * v

    print(sumval)

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())
B, C = [], []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for b, c in zip(B, C):
    A = np.where(A==b, c, A)
    print(A.sum())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
  n = int(input())
  a = np.array(list(map(int, input().split())))
  q = int(input())
  
  for _ in range(q):
    b, c = map(int, input().split())
    a = [c if i == b else i for i in a]
    print(sum(a))
  
main()

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())
import collections
A=collections.Counter(a)
for i in range(Q):
	if B[i] in A:
		f=A.pop(B[i])
		if C[i] in A:
			g=A[C[i]]
			h=f+g
		else:
			h=f
		A[C[i]]=h
	S=0
	for k,v in A.items():
		S=S+k*v
	print(S)

Submission status: Time Limit Exceeded

Solution code:
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)

ans = sum(A)
d = dict()
for i, a in enumerate(A):
    if a not in d:
        d[a] = ()
    d[a] += (i,)

for b, c in zip(B, C):
    if b not in d:
        print(ans)
        continue
    ans += len(d[b]) * (c-b)
    if c not in d:
        d[c] = ()
    d[c] = d[c] + d[b]
    d[b] = ()
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def getCalc(AAA, BBB):
    sum = 0
    for i in range(len(AAA)):
        sum += AAA[i] * BBB[i]
    return sum


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

AAA = []
BBB = []

for i in range(10 ** 5 + 1):
    count = A.count(i)
    if (count > 0):
        AAA.append(i)
        BBB.append(count)

total = getCalc(AAA, BBB)

for i in range(Q):
    if (B[i] in AAA):
        index = AAA.index(B[i])

        if(C[i] in AAA):
            Cindex = AAA.index(C[i])
            AAA[index] = C[i]
            BBB[index] += BBB[Cindex]
            AAA.pop(Cindex)
            BBB.pop(Cindex)
        else:
            AAA[index] = C[i]

        total = getCalc(AAA, BBB)

    print(total)

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 = []

X = np.array([0]*(10**5))
Y = np.array([i  for i in range(1, (10**5)+1)])

for j in range(N):
    X[A[j]-1] += 1

for i in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    print(X@Y)

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

Submission status: Time Limit Exceeded

Solution code:

from collections import Counter

def resolve():
  N = int(input())
  countA = Counter([int(x) for x in input().split(" ")])
  Q = int(input())

  sum_A = 0
  for a in countA.keys():
    sum_A += a*countA[a]

  for _ in range(Q):
    B, C = map(int, input().split(" "))
    if B in countA.keys():
      countA[C] += countA.pop(B)
      sum_A = 0
      for a in countA.keys():
        sum_A += a*countA[a]
      print(sum_A)
    else:
      print(sum_A)


if __name__ == "__main__":
  resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
#sys.exit()
#sorted( ,reverse=True)
N = int(input())
#S,T = map(int, input().split())
A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
Q = int(input())
#cnt = 0
#cnt += 1

ans = sum(A)

for _ in range(Q):
    B,C = map(int, input().split())
    for i in range(N):
        if A[i] == B:
            A[i] = C
            ans += C-B
    #print(A)
    #cnt = A.count(B)
    #print(cnt)
    #ans += cnt*(C-B)
    print(ans)

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
s=[0]*q
for i in range(q):
	b[i],c[i]=map(int,input().split())
f={}
for i in range(n):
	f.setdefault(a[i],[])
for i in range(n):
	f[a[i]].append(i)
#print(f)
for i in range(q):
	if b[i] in f:
		if c[i] in f:
			f[c[i]]=f[c[i]]+f[b[i]]
			f.pop(b[i])
		else:
			f.setdefault(c[i],[])
			f[c[i]]=f[c[i]]+f[b[i]]
			f.pop(b[i])
	#print(f)
	for g,h in f.items():
		#print(len(h)*g)
		s[i]=s[i]+len(h)*g
	print(s[i])

Submission status: Time Limit Exceeded

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


def change(b, c):
    if b in A:
        for i in range(N):
            if A[i] == b:
                A[i] = c
    ans = sum(A)
    print(ans)

for i in range(Q):
    change(B[i], C[i])

Submission status: Time Limit Exceeded

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

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

    if B in given_list:
        for i in range(N):
          if given_list[i] == B:
              given_list[i] = C
        print(sum(given_list)) #####       
    else:  
        print(sum(given_list)) #####

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
B,C = [], []

for _ in range(q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)


for b, c in zip(B,C):
    ans = 0
    for i, a in enumerate(A):
        if a == b:
            ans += c
            A[i] = c
        else:
            ans += a
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)
 
sum=0
for i in range(len(count_list)):
    sum+=number_list[i]* count_list[i]

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            sum+=(number_list[index1]-number_list[index0])* count_list[index0]
            count_list[index1]+=count_list[index0]
        else:
            sum+=(int(act[1])-number_list[index0])* count_list[index0]
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])

        count_list[index0]=0
 
 
    print(str(sum))

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)]
ans = sum(A)
for i in B:
    num = 0
    A.sort()
    for j in range(len(A)):
        if A[j] > i[0]:
            break
        if A[j] == i[0]:
            num +=1
            A[j] = i[1]
    ans = ans + num *(i[1]-i[0])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
AA = {}
for a in A:
	if a in AA:
		AA[a] += 1
	else:
		AA[a] = 1

Q = int(input())
ans = []
for i in range(Q):
	b, c = list(map(int, input().split()))
	if b in AA:
		if c not in AA:
			AA[c] = 0
		AA[c] += AA[b]
		del AA[b]

	t = 0
	for k, v in AA.items():
		t += k * v
	ans += [t]
[print(a) for a in ans]

Submission status: Time Limit Exceeded

Solution code:
import bisect
import collections
import copy
import heapq
import itertools
import math
import numpy
import string
import sys
def I(): return numpy.int64(sys.stdin.readline().rstrip())
def LI(): return list(map(numpy.int64, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


N = I()
A = LI()
Q = I()

for s in range(Q):
    BC = LI()
    for t in range(N):
        if A[t] == BC[0]:
            A[t] = BC[1]
    print(numpy.sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()
q = int(input())
for i in range(q):
    b, c = map(str, input().split())
    A = a.replace(b, c)    
    ans = 0
    s = "0"
    for j in list(A):
        if j != " ":
            s += j
        else :
            ans += int(s[1:])
            s = "0"
    ans += int(s[1:])
    print(ans)
    a = A

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
  return [i for i, _x in enumerate(l) if _x == x]

for i in range(Q):
  BC = list(map(int, input().split()))
  B_i = BC[0]
  C_i = BC[1]
  B_ind = my_index_multi(A, B_i)
  for j in B_ind:
    A[j] = C_i
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

q = int(input())
sum_s = sum(a_list)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    for i in range(Q):
        B, C = list(map(int,input().split()))
        D = [j for j, x in enumerate(A) if x == B]
        for k in D:
            A[k] = C
        print(sum(A))



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for q in range(Q):
    B, C = 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:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

a = sorted(a)

for i in range(q):
    flag = 0
    for j in range(len(a)):
        if a[j] == bc[i][0]:
            a[j] = bc[i][1]
            flag += 1
        elif flag > 1:
            break

        # print(a)
    
    print(sum(a))

Submission status: Time Limit Exceeded

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

for num, rep in zip(b, c):
    a = [rep if ele == num else ele for ele in a]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_lst = list(map(int, input().split()))
q = int(input())
b_lst = []
c_lst = []
cnt = []
ans = []
# ans = a_lst.copy()
for i in range(q):
    x, y = map(int, input().split())

    b_lst.append(x)
    c_lst.append(y)

for i in range(q):

    for j in range(len(a_lst)):
        if a_lst[j] == b_lst[i]:
            a_lst[j] = c_lst[i]
    cnt.append(sum(a_lst))
    print(cnt[i])

# print(cnt)
# for i in range()
# print(b_lst)

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 = (int(x) for x in input().split())

    idx_list = [i for i, x in enumerate(A) if x == B]
    for idx in idx_list:
        A[idx]=C
    
    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)]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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

x=[0]*(10**5)
for i in range(n):
    x[i]=a.count(i+1)*(i+1)


for _ in range(q):
    b,c=map(int,input().split())
    if x[b-1]!=0:
        x[c-1]+=x[b-1]//b*c
        x[b-1]=0
        print(sum(x))
    else:
        print(sum(x))

Submission status: Time Limit Exceeded

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


for j in range(Q):
    b,c = B[j],C[j]
    for i in range(N):
        if A[i]==b:
            A[i] = c
        S[j] = S[j]+A[i]
    print(S[j])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    B, C = map(str, input().split())
    A = list(map(lambda x: int(str(x).replace(B, C)), A))
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B = [0]*Q
C = [0]*Q
A = np.array(A)
l = A

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

for i in range(Q):
    A[A==B[i]]=C[i]
    print(np.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)]
for i in range(Q):
  A = [BC[i][1] if j == BC[i][0] else j for j in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A.sort()
    A = np.array(A)
    ans = sum(A)
    from bisect import bisect_left,bisect_right

    for i in range(q):
        b,c = map(int, input().split())
        start = bisect_left(A,b)
        end = bisect_right(A,b)
        A[start:end] = c
        A.sort()
        ans += (c-b)* (end-start)
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    b,c = map(int,input().split())
    l = [c if j == b 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())
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:
def run(n, seq, num, trials):
    '''
    '''
    for i in range(num):
        b, c = trials[i]
        seq = [e if e != b else c for e in seq]
        print(sum(seq))


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()))
A.sort()
Q = int(input())
B = []
C = []

for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != B[i] and s != h :
        if A[h] < B[i]:
            s = h
            h = int((s+e)/2)
        elif A[h] > B[i]:
            e = h
            h = int((s+e)/2)
    for j in range(s, e):
        if A[j] == B[i]:
            A[j] = C[i]
    A.sort()
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
nums = [int(x) for x in input().split()]
m = int(input())

for i in range(m):
      nb,nc = [int(x) for x in input().split()]
      for j in range(len(nums)):
            if nums[j] == nb:
                  nums[j] = nc
      print(sum(nums))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
k = int(input())
for i in range(k):
	a,b = map(int,input().split())
	for nj in li:
		if nj==a:
			inde = li.index(nj)
			li.pop(inde)
			li.insert(inde,b)
	print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
dA = {}
for a in A:
  dA[a] = 0
for a in A:
  dA[a] += 1
Q = int(input())
for q in range(Q):
  B,C = map(int,input().split())
  if B in dA:
    if C in dA:
      dA[C] += dA.pop(B)
    else:
      dA[C] = dA.pop(B)
  s = 0
  for a in dA:
    s += a * dA[a]
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = input().split()
Q = int(input())
a = collections.Counter(A)
ans2 = []
for i in range(Q):
    ans = ""
    B, C = map(str, input().split())
    if B in a and C in a:
        a[C] += a[B]
        del a[B]
    elif B in a and C not in a:
        a[C] = a[B]
        del a[B]
    for key, value in a.items():
        ans += "{}*{}+".format(key, value)
    ans2.append(eval(ans[:-1]))
[print(a) for a in ans2]

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ll = []
for i in range(q):
	l = list(map(int, input().split()))
	ll.append(l)
    
for i in ll:
  if i[0] in a:
    a = [i[1] if j == i[0] else j for j in a]
    print(sum(a))
  else:
    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
  b = b_c[i][0]
  c = b_c[i][1]
  b_count = a.count(b)
  a = [i for i in a if i != b]
  for i in range(b_count):
    a.append(c)
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = {}

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

for i in range(N):
  a = A[i]
  if not a in X:
    X[a] = 1
  else:
    X[a] += 1

Q = int(input())
B = []
C = []

ANS = []
for i in range(Q):
  b, c = map(int, input().split())
  if b in X:
    if c in X:
      X[c] += X[b]
    else:
      X[c] = X[b]
    X[b] = 0
  ans = 0
  for i in X.keys():
    ans += i * X[i]
  ANS.append(ans)

for i in range(len(ANS)):
  print(ANS[i])

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
l=list(map(int,input().split()))
b=int(input())
for i in range(b):
  l1=list(map(int,input().split()))
  for i in range(len(l)):
    if l[i] ==l1[0]:
      l[i]=l1[1]
  print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
Q = int(input())
for i in range(1,Q+1):
  x, y = map(str, input().split())
  A = [s.replace(x, y) for s in A]
  print(sum(map(int, A)))

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)
N = ii()
A = [el for el in mips()]
Q = ii()
def ch(el,B,C):
    if el == B:
        return C
    return el
for i in range(Q):
    B,C = mips()
    A = [ch(el,B,C) for el in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
A = list(map(int,input().split()))
A.sort()
Q = int(input())
s = [list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
  search = s[i][0]
  b=bisect.bisect_left(A,search)
  c=bisect.bisect_right(A,search)
  for j in range(b,c):
    A[j] = s[i][1]
  print(sum(A))

Submission status: Time Limit Exceeded

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

d = {}
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    if b in d.keys() and c in d.keys():
        d[c] += d[b]
        d.pop(b)
    elif b in d.keys():
        d[c] = d[b]
        d.pop(b)
    ans = 0
    for k, v in d.items():
        ans += int(k) * v
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
a = list(map(int, input().split()))
q = int(input())
C = []
Co = []
s = []
S = sum(a)
def find(b, a, c): #配列aからbを探し出す
  counter = 0
  for i in range(len(a)):
    if a[i] == b:
      counter += 1
  return counter
def finde(c, b, co) : # Cのなかにbがいくつ入っているか
  counter = 0
  for i in range(len(c)):
    if c[i] == b :
      counter += co[i]
  return counter 

for i in range(q):
  b, c = map(int,input().split())
  C.append(c)
  count = find(b,a,c) #aのなかにbはcount個ある
  Co.append(count) # C[i]はaのなかにCo[i]個含まれている
  ss = finde(C, b, Co) # Cのなかにbが何個含まれているか数える
  S += (count+ss) * (c - b)
  print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

def retList(target, A):
  split = target.split()  
  return [int(str(s).replace(split[0], split[1])) for s in A]

N = int(input())
#print(N)

a = input().split()
A = [int(b.strip()) for b in a]
#print(A)

Q =  int(input())
#print(Q)

B = sys.stdin.readlines()
C = [b.strip() for b in B]
#print(C)

for i in range(Q):
  target = C[i]
  if i == 0:
    ret = retList(target, A)
  else:
    ret = retList(target, ret)
  print(sum(ret))

Submission status: Time Limit Exceeded

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


N = int(input())
A_list = list(input().split())
Q = int(input())
num_list = [list(input().split()) for _ in range(Q)]

for i in num_list:
    B = i[0]
    C = i[1]
    A_list = [int(str(A).replace(B, C)) for A in A_list]
    A_result = sum(A_list)
    print(A_result)

Submission status: Time Limit Exceeded

Solution code:
def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
      
      
n = int(input())
a = [int(i) for i in input().split(' ')]
q = int(input())
b = []
c = []
for _ in range(q):
  temp = input().split(' ')
  b.append(int(temp[0]))
  c.append(int(temp[1]))

for i, j in zip(b, c):
  if i in a:
    while True:
      k = my_index(a, i)
      if k is False:
        break
      a[k] = j
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)
 
sum=0
for i in range(len(count_list)):
    sum+=number_list[i]* count_list[i]

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            sum+=(number_list[index1]-number_list[index0])* count_list[index0]
            count_list[index1]+=count_list[index0]
        else:
            sum+=(int(act[1])-number_list[index0])* count_list[index0]
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])

        number_list.pop(index0)
        count_list.pop(index0)
 
 
    print(str(sum))

Submission status: Time Limit Exceeded

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0
    
for i in range(N):
    lst[A[i]] += 1

Q = int(input())

ans = []
for i in range(Q):
    B, C = map(int, input().split())
    if B in lst:
        if C in lst:
            lst[C] += lst[B]
            lst.pop(B)
        else:
            lst[C] = lst[B]
            lst.pop(B)
            
        ans_tmp = 0
        for i in lst:
            ans_tmp += i*lst[i]

        ans.append(ans_tmp)
        
    elif i != 0:
        ans.append(ans[-1])
    else:
        ans_tmp = 0
        for i in lst:
            ans_tmp += i*lst[i]
        ans.append(ans_tmp)

for i in ans:
    print(i)