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

num_of_numbers = int(input())
numbers = np.array(list(map(int, input().split())))
num_of_operation = int(input())
operations = []
for i in range(num_of_operation):
    B, C = map(int, input().split())
    numbers = np.where(numbers == B, C, numbers)
    print(sum(numbers))

Submission status: Time Limit Exceeded

Solution code:
# import sys
# input = sys.stdin.readline
# import re
# import itertools
import collections

def main():
    n = int(input())
    s = input_list()
    sc = collections.Counter(s)
    q = int(input())
    ans = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        if b in sc:
            sc.pop(b)
        a = 0
        for k, v in sc.items():
            a += k*v
        ans.append(a)
    for a in ans:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

def input_list():
    return list(map(int, input().split()))

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A_in = input()
    
Q = int(input())

for i in range(Q):
    B, C = map(str, input().split())
    
    A_in = A_in.replace(B, C)
    
    A_val = []
    for i in A_in.split():
        A_val.append(int(i))
    
    print(sum(A_val))

Submission status: Time Limit Exceeded

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

print()
for i in range(Q):
    if BC[i][0] not in A:
        pass
    else:
        for j in range(len(A)):
            if A[j]==BC[i][0]:
                A[j]=BC[i][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
a = li()
a.sort()

s = sum(a)

q = ii()

from bisect import bisect_left, bisect_right
def bisect_find(a, x):
    i = bisect_left(a, x)
    if i == len(a):
        return -1
    elif a[i] != x:
        return -1
    return i

def bisect_r_find(a, x):
    i = bisect_right(a, x)
    # if i == len(a):
    #     return -1
    return i

for i in range(q):
    b,c = mi()


    # 左を探す
    l = bisect_find(a,b)
    # 右を探す
    if l != -1:
        r = bisect_r_find(a[l:],b) + l
        # 個数をカウント
        count = r-l
        # 前回の合計との差を求める
        s = s + (c-b)*count

        # 配列を上書き
        a[l:r] = [c]*count
        a.sort()
    # print('-------------ans----------------',s)
    print(s)

Submission status: Time Limit Exceeded

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

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):
  b, c = map(int, input().split())
  B.append(b)
  C.append(c)
cnt = 0

for i in range(Q):
  for j in range(len(A)):
    if A[j] == B[i]:
      A[j] = C[i]
  for j in range(len(A)):
    cnt += A[j]
  print(cnt)
  cnt = 0

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = list()
sum_a = 0
output = 0
cnt = 0
flag = False
for i in range(q):
  bc.append(list(map(int,input().split())))
a.sort()
for i in range(q):
  for j in range(len(a)):
    if a[j] == bc[i][0]:
      a[j] = bc[i][1]
  print(sum(a))
  a.sort()

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)

l = [0] * (10**5 + 1)
c = collections.Counter(A)
for key in c:
    l[key] = c[key]

for i in range(Q):
    if B[i] in c:
        c[C[i]] += c[B[i]]
        c.pop(B[i])
    ans = 0
    for item in c:
        ans += item * c[item]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = input()
a = np.array([*map(int, input().split())])
q = int(input())
b = [[*map(int,input().split())] for _ in range(q)]
for i,j in b:
    a = np.where(a==i, j, a)
    print(sum(a))

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

Q=int(input())
for i in range(Q):
    b, c = map(int, input().split())
    flag=my_index(A,b)
    while flag is not False:
        A[flag]=c
        flag=my_index(A,b)
    print(sum(A))

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

for i in range(q):
    a, b = map(int, input().split())
    A = np.where(A == a, b, A)
    print(A.sum())

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1

    return None

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())
A.sort()
j = 0
for i in range(Q):
    while binary_search(A,B[i]) != None:
        id = binary_search(A,B[i])
        if A[id] == B[i]:
            A[id] = C[i]
            A.sort()
    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 i in range(q)]
    
    for j in range(q):
        rep = [bc[j][1] if i == bc[j][0] else i for i in a]
        a = rep
        print(sum(a))

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):
    x,y=map(int,input().split())
    if x not in l:
        print(sum(l))
    else:
        lrep=[y if i==x else i for i in l ]
        print(sum(lrep))
        l=lrep

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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

    MAX = 10 ** 5 + 10
    k = [0] * MAX
    # v = [0] * MAX

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

    num = np.arange(MAX)

    arr_k = np.array(k)

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

    # print(k[:10])

    # for i in range(Q):
    #     ans = 0
    #     b, c = map(int, input().split())
    #     if k[b] > 0:
    #         k[c] += k[b]
    #         k[b] = 0
    #     for i in range(MAX):
    #         ans += i * k[i]
    #     print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
qlist=[]
Q=int(input())
i=0

while(i<Q) :
  val=input()
  qlist.append(val)
  i+=1

j=0
while(j<Q):
  a,b=qlist[j].split()
  S=S.replace(str(a),str(b))
  list=map(int,S.split())
  print(sum(list))
  j+=1

Submission status: Time Limit Exceeded

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

def replace_sum(b,c,a):
 #   ans = 0
  #  a.replace(b,c)
    #ans = sum(a)
    #print(ans)
    for i in range(len(a)):
        if a[i] == b:
            a[i] = c
    return a



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

for i in range(q):
    #replace(b[i],c[i],a)
#    a.replace(b, c)
 #   ans = sum(a)
  #  print(ans)
    #sum = 0
   # for j in a:
   #     re = j.replace(str(2),str(3))
    #    #a.append(re)
     #   print(j,end = " ")
      #  print(re)
       # sum += int(j)
    a = replace_sum(b[i],c[i],a)
    #sum = int(sum(a))

    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = list(map(int,input().split()))
Q = int(input())
Bn = [0] * Q
Cn = [0] * Q
for c in range(Q):
    Bn[c], Cn[c] = map(int, input().split())

for count in range(Q):
    for index,an in enumerate(An):
        if an == Bn[count]:
           An[index] = Cn[count]
    print(sum(An))

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:

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

result = a
sum = sum(result)
l = []
for i in range(q):
  b, c = map(int, input().split())

  count = 0
  # result = [c if j == b else j for j in result]

  # l.append(sum(result))
  for j in range(len(result)):
    if result[j] == b:
      count = count+ 1
      result[j] = c
  sum += count * (c-b)
  print(sum)
#   l.append(sum)

# for i in l:
#   print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())
BC = []
flag = 1

for i in range(Q):
  BC.append(list(map(int, input().split())))

for i in range(Q):
  if flag == 1:
    A.sort()
    flag = 0
  
  if BC[i][0] < A[0] or A[N-1] < BC[i][0]:
    print(sum(A))
    continue
  
  for j in range(N):
    if A[j] == BC[i][0]:
      A[j] = BC[i][1]
      flag = 1
    elif A[j] > BC[i][0]:
      break

  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):
    B,C = map(int,input().split())
    b.append(B)
    c.append(C)

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:
import sys

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

def main():
    input = sys.stdin.readline
    n = int(input())
    a = list(map(str, input().strip().split()))
    q = int(input())
    bc = [map(str, input().split()) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]

    for i in range(q):
        aa = [n.replace(b[i],c[i]) for n in a]
        a = aa
        print(sum(list(map(int, aa))))
        

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
A=list(map(int,(input().split())))
npArray = np.array(A)
npArray=npArray.copy()
ans=np.sum(npArray)
Q=int(input())
B=[]
C=[]
for _ in range(Q):
  L, R = map(int,(input().split()))
  B.append(L)
  C.append(R)

for i in range(Q):
  B_count=np.count_nonzero(npArray == B[i])
  sa=(C[i]-B[i])*B_count
  upNpArray = np.where(npArray == B[i], C[i], npArray)
  ans= ans+sa
  print(ans)
  npArray=upNpArray.copy()

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())
  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(int(x) for x in input().split())
Q=int(input())
X=[list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
	sum=0
	if X[i][0] in A:
		Aaf=[X[i][1] if j==X[i][0] else j for j in A]
		for k in range(N):
			sum+=Aaf[k]
		A=Aaf
	else:
		for l in range(N):
			sum+=A[l]
	print(sum)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]
    
    if B in given_list:
        given_list = sorted(given_list)
        for i in range(N):
          if given_list[i] == B:
              given_list[i] = C
          elif int(given_list[i]) > int(C):
              break
        given_list = [int(i) for i in given_list]  
        print(sum(given_list)) #####          
    else:
        given_list = [int(i) for i in given_list]  
        print(sum(given_list)) #####
    
    given_list = [str(i) for i in given_list]

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

def input():
    return sys.stdin.readline().strip()

def main():
    n = int(input())
    a = list(map(np.int64, input().split()))
    q = int(input())
    ans = []
    for i in range(q):
        b, c = map(int, input().split())
        a = list(map(lambda x: c if x == b else x, a))
        ans.append(sum(a))

    for j in ans:
        print(j)

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
MOD = 10 ** 9 + 7


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

    for B, C in BC:
        A = np.where(A == B, C, A)
        print(np.sum(A))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A =  input().split()
Q = int(input())
X2=[]
for i in range(Q):
  B,C = input().split()
  for item in A:
    X = item.replace(B,C,1)
    X2.append(X)
  A = []
  A = X2
  X2=[]
  Z = list(map(int,A))
  print(sum(Z))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split(' ')))
q=int(input())
for _ in range(q):
    s=0
    b,c = map(int,input().split(' '))
    a=[c if x==b else x for x in a]
    for i in a:
        s+=i
    print(s)

Submission status: Time Limit Exceeded

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

q = int(input())

s = 0
result = []

r = 1
while r <= q:
  e = list(map(int, input().split()))
  i = 0
  while i < n:
     if(p[i] == e[0]):
        p[i] = e[1]
     s = sum(p)
     i += 1

  r += 1
  result.append(str(s))


s_n = '\n'.join(result)
print(s_n)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = sorted(int(x) for x in input().split())
Q = int(input())
for _ in range(Q):
    BC = input().split()
    B, C = int(BC[0]), int(BC[1])
    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 = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for i in range(len(change)):
    ans = 0
    if change[i][0] in dic:
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    for i, j in enumerate(dic):
        ans += j*dic[j]
    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, input().split())) for _ in range(Q)]

for i in range(Q):
    p, n = BC[i][0], BC[i][1]
    A = [x if x != p else n for x in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
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]
    print(int(sum(answers)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = list(map(int, input().split(' ')))
Q = int(input())
b, c= 1, 2
for i in range(Q):
  b, c = map(int, input().split(' '))
  if b in set(nums):
    for j, num in enumerate(nums):
      if num == b:
        nums[j] = c

  print(sum(nums))

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

m = 100001
res = np.arange(m,dtype=int)
newA = np.zeros((m),dtype=int)


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

for i in range(Q):
    newA[C[i]] += newA[B[i]]
    newA[B[i]] = 0
    print(np.dot(res,newA))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*q
for i in range(q):
    b,c=map(int,input().split())
    l[i]=(b,c)
book={}
for i in a:
    if i in book:
        book[i]+=1
    else:
        book[i]=1

for i in range(q):
    ans=0
    if l[i][0] in book:
        if l[i][1] in book:
            book[l[i][1]]+=book[l[i][0]]
            book.pop(l[i][0])
        else:
            book[l[i][1]]=book[l[i][0]]
            book.pop(l[i][0])
    for j in book:
        ans+=book[j]*j
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, a = int(input()), list(map(int, input().split()))
for i in range(int(input())):
    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:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
  b, c = map(int, input().split())
  for j in range(n):
    if a[j] == b:
      a[j] = c
  ans.append(sum(a))
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())
for _ 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())
b = []
c = []
for i in range(q):
    b_i, c_i = map(int, input().split())
    b.append(b_i)
    c.append(c_i)

res = {}
for a_i in a:
    if not a_i in res:
        res[a_i] = 0
    res[a_i] += 1

for b_i, c_i in zip(b, c):
    num = res[b_i] if b_i in res else 0
    res[b_i] = 0
    if not c_i in res:
        res[c_i] = 0
    res[c_i] += num

    ans = 0
    for key, val in res.items():
        ans += key * val
    print(ans)

Submission status: Time Limit Exceeded

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

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

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 j in range(N):
    if A[j]==B:
      A[j]=C
  print(sum(A))

Submission status: Time Limit Exceeded

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


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

    while(1):
        try:
            A[A.index(B)] = C
        except ValueError:
            break

    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
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 m == b else m for m in a]
  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 j in range(N):
    if A[j]==B:
      A[j]=C
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
ans = []
count = collections.Counter(a)
for i in range(q):
  total = 0
  num = count[bc[i][0]]
  count[bc[i][0]] = 0
  count[bc[i][1]] += num
  for key, value in zip(count.keys(), count.values()):
    total += int(key) * int(value)
  ans.append(total)

for out in ans:
  print(out)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]
            del num_count[B]
            _sum = 0
            for k, v in num_count.items():
                _sum += k * v
            print(_sum)
        else:
            print(_sum)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
li=list(map(int,input().split()))
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  li=list(map(lambda x: x if x!=b else c,li))
  print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-06-21 21:56:58 +0900
# LastModified: 2020-06-21 22:01:41 +0900
#


import copy

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for bc in BC:
    A = np.where(A==bc[0],bc[1],A)
    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 n, j in enumerate(a):
		if j == b:
			a[n] = c
 
	print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
    x,y=map(int,input().split())
    lrep=[y if i==x else i for i in l ]
    print(sum(lrep))
    l=lrep

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

import numpy as np
for _ in range(Q):
    B,C=map(int,input().split())
    A[C]+=A[B]
    A[B]=0
    print(sum(np.array(list(A.keys())) * np.array(list(A.values()))))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq_c = input()
Q = int(input())
for i in range(Q):
    bi, ci = input().split()
    seq_c = seq_c.replace(bi,ci)
    print(sum(list(map(int,seq_c.split()))))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = []
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 defaultdict

n = int(input())
d = defaultdict(int)
a = list(map(int, input().split()))
for key in range(n):
  d[key] = a[key]
  
q = int(input())

for i in range(q):
  b,c = map(int, input().split())
  # print(b,c)
  keys = [k for k, v in d.items() if v == b]
  for j in keys:
    d[j] = c
  print(sum(d.values()))

Submission status: Time Limit Exceeded

Solution code:
n    = int(input())
a_s  = list(map(int, input().split()))
q    = int(input())
bc_s = [ list(map(int, input().split())) for _ in range(q) ]

s = sum(a_s)
m = {}

for b, c in bc_s:
    nb = a_s.count(b) + m.get(b, 0)
    s += nb * (c - b)
    print(s)
    m[c] = nb + m.get(c, 0)

Submission status: Time Limit Exceeded

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

num = [A.count(i) for i in range(1, 100001)]
for change in changes:
    num[change[1] - 1] += num[change[0] - 1]
    num[change[0] - 1] = 0
    s = 0
    for i, c in enumerate(num):
        s += (i + 1) * c
    print(s)

Submission status: Time Limit Exceeded

Solution code:
#入力
N = int(input())
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

for j in range(Q):
  for k in range(len(Alist)):
    if Alist[k] == Blist[j]:
      Alist[k] = Clist[j]
  print(sum(Alist))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B =[]
C =[]
BIGA= []
for i in range(Q):
	b,c = map(int,input().split())
	B.append(b)
	C.append(c)
for k in range(Q):
	for n, i in enumerate(A):
		if i == B[k]:
			A[n] = C[k]
	print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
suretu = list(map(int, input().split()))
Q = int(input())
ans = [0 for i in range(Q)]
for i in range(Q):
    x, y = map(int, input().split())
    for j in range(len(suretu)):
        if x == suretu[j]:
            suretu[j] = y
    ans[i] = sum(suretu)
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
s=str(input())
q = int(input())
x = [list(map(int,input().split())) for i in range(q)]
ans=s
for i in range(q):
    ans = ans.replace(str(x[i][0]),str(x[i][1]))
    print(sum(map(int,ans.split())))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = np.array(sorted(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())
    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())

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:
N = int(input())
A = list(map(str, input().split()))
A.sort()
num = int(input())
change_list = [list(map(str, input().split())) for _ in range(num)]

for change in change_list:
    A = [s.replace(change[0], change[1]) for s in A]
    print(sum(list(map(int, 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):
  a.sort()
  b, c = map(int, input().split())
  if b in a:
    d = a.count(b)
    e = a.index(b)
    for i in range(d):
      a[e+i] = c
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [list(map(int,input().split())) for i in range(Q)]
for i in B:
    A.sort()
    for j in range(len(A)):
        if A[j] > i[0]:
            break
        if A[j] == i[0]:
            A[j] = i[1]
    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)

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]))
            count_list[index1]+=count_list[index0]
            number_list.pop(index0)
            count_list.pop(index0)
        else:
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])
            number_list.pop(index0)
            count_list.pop(index0)

    sum=0
    for i in range(len(count_list)):
        sum+=number_list[i]* count_list[i]

    print(str(sum))

Submission status: Time Limit Exceeded

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

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

for i in range(q):
    b, c = map(int,input().split())
    if b in m.keys():
        if c in m.keys():
            m[c] = m[c] + m[b]
        else:
            m[c] = m[b]
        m.pop(b)
    ans = 0
    for k in m:
        ans += m[k] * k
    print(ans)