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:
def main():
    import collections
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    ans = sum(A_list)
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())

        #num = A_list.count(B[i])
        num_dict = collections.Counter(A_list)
        num = num_dict[B[i]]
        #print(num)
        if B[i] > C[i] and B[i] in A_list :
            ans = ans - (B[i] - C[i]) * num
        elif B[i] < C[i] and B[i] in A_list  :
            ans = ans + (C[i] - B[i]) * num
        for j in range(len(A_list)) :
            if B[i] in A_list :
                rep = A_list.index(B[i])
                A_list[rep] = C[i]
            else :
                #print("in")
                break
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

arr_B=[]
arr_C=[]

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

for i in range(Q):
  if arr_B[i] in A:
    for j in range(N):
      if arr_B[i] == A[j]:
        A[j] = arr_C[i]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    for _ in range(Q):
        b, c = map(int, input().split())
        for i in range(A.count(b)):
            A.remove(b)
            A.append(c)
        print(sum(A))
resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
rep =[]
for i in range(Q):
  rep = list(map(int,input().split()))
  A = [rep[1] if i ==rep[0] 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 = ','.join(A)
Q = int(input())
lst = []
for _ in range(Q):
    a,b = map(int,input().split())
    lst.append([a,b])

#print(A)
'''
for i in range(Q):
    A = [lst[i][1] if j == lst[i][0] else j for j in A]
    print(sum(A))
'''
def digitSum(n):
    s = str(n)
    array = list(map(int, s))
    return sum(array)

for i in range(Q):
    ans = []
    A = A.replace(str(lst[i][0]),str(lst[i][1]))
    #A = int(A)
    #print(A)
    ans =A.split(',')
    ans = list(map(int, A.split(',')))
    #print(ans)
    print(sum(ans))
    #print(digitSum(A))
    A = str(A)
    #print(A)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
data = collections.Counter(a)
count, key = list(data.values()), list(data.keys())

for i in range(q):
    b, c = map(int, input().split())
    if b in key and c in key:
        count[key.index(c)] += count[key.index(b)]
        count[key.index(b)] = 0
    elif b in key:
        key.append(c)
        count.append(count[key.index(b)])
        count[key.index(b)] = 0
    res = 0
    for j in range(len(key)):
        res += count[j] * key[j]
    print(res)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
n = int(input())
p = list(map(int,input().split()))
q = int(input())

xy = [map(int,input().split()) for _ in range(q)]
x, y = [list(i) for i in zip(*xy)]

for i in range(q):
    for j in range(len(p)):
        if p[j] == x[i]:
            p[j] = y[i]
    print(sum(p))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = 0
c = 0
ans = sum(a)
for i in range(q):
    b, c = map(int, input().split())
    b_num = a.count(b)
    ans += b_num*(c-b)
    for j in range(b_num):
        a.remove(b)
        a.append(c)
    print(ans)

Submission status: Time Limit Exceeded

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

tmp = ','.join(a)

for i in range(q):
    tmp = tmp.replace(b[i], c[i])
    ans = list(tmp.split(','))
    ans2 = 0
    for j in ans:
        ans2 += int(j)
    print(ans2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = str(input())
Q = int(input())
sumLi = []
for i in range(Q):
    B, C = map(str, input().split())
    A = A.replace(B, C)
    nA = list(map(int, A.split()))
    sumLi.append(sum(nA))
for j in range(len(sumLi)):
    print(sumLi[j])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
q = int(input())
Q = []
for i in range(q):
    b, c = [int(j) for j in input().split()]
    A = [k if k != b else c for k in A]
    Q.append(sum(A))
for i in Q:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lsn = list(map(int,input().split()))
Q = int(input())
ls2 = []
for i in range(Q):
    Bi,Ci = map(int,input().split())
    ls2.append([Bi,Ci])
index = [0]*(10**5+1)
for i in range(10**5+1):
    index[i] = lsn.count(i)
for l in ls2:
    index[l[1]] = index[l[1]]+index[l[0]]
    index[l[0]] = 0
    sum1 = 0
    for j in range(10**5+1):
        sum1 += j*index[j]
    print(sum1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

d = Counter(A)

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

    li = [k * v for k, v in d.items()]
    print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
array = list(map(int,input().strip().split()))
Q = int(input())
ka=[]
for i in range(Q):
  moment = list(map(int,input().strip().split()))
  ka.append(moment)
  
for j in range(Q):
  if j==0:
    for i in range(N):
     if array[i]==ka[j][0]:
      array[i]=ka[j][1]
    sama = sum(array)
    print(sama)
  else:
    for i in range(N):
      if array[i]==ka[j][0]:
        array[i]=ka[j][1]
        sama = sama+(ka[j][1]-ka[j][0])
    print(sama)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
an = list(map(int, sys.stdin.readline().rstrip().split()))
q = int(input())

import numpy as np
from collections import deque

sum = np.sum(an)
ans = deque()

for _ in range(q):
    b,c = map(int, input().split())
    for i in range(n):
        if b == an[i]:
            an[i]=c
            sum += c-b
    ans.append(sum)

while ans:
    print(ans.popleft())

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

Submission status: Time Limit Exceeded

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

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

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(len(A)):
    if A[j]==B:
      A[j]=C
  s=0
  for k in range(len(A)):
    s=s+A[k]
  print(s)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    count = [0 for _ in range(10**5)]
    for i in range(n):
        count[a[i] - 1] += 1

    q = int(input())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        count[c - 1] += count[b - 1]
        count[b - 1] = 0
        total = 0
        for i in range(10**5):
            if count[i] == 0:
                continue
            total += count[i] * (i + 1)
        print(total)


if 'unittest' not in globals():
    resolve()

Submission status: Time Limit Exceeded

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

for q in range(Q):
    b, c = map(int, input().split())
    for i in range(N):
        if A[i] == b:
            A[i] = c
            if b <= c:
                ans += c - b
            else:
                ans -= b - c
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())

A_list = list(map(int, input().split()))
A_array = np.array(A_list)

Q = int(input())

for N in range(Q):
    B, C = map(int, input().split())
    if B in A_array:
        A_array[A_array == B] = C
    print(np.sum(A_array))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

for search, replace in replace_map:
    if search in nums:
        nums[replace] = nums[replace] + nums[search] if replace in nums else nums[search]
        del nums[search]
    print(np.dot(list(nums.keys()), list(nums.values())))

Submission status: Time Limit Exceeded

Solution code:
import sys
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)]
i = 0

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

Submission status: Time Limit Exceeded

Solution code:
def Replacing():

    Num = int(input())

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

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        vb_0 = value_B[0]
        vb_1 = value_B[1]

        value_A = [vb_1 if j == vb_0 else j for j in value_A]

        print(sum(value_A))

if __name__ == '__main__':
    Replacing()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

B = []
C = []

for q in range(Q):
    _b, _c = list(map(int, input().split()))
    B.append(_b)
    C.append(_c)


def replace(A, b, c):
    for i in range(len(A)):
        if A[i] == b:
            A[i] = c
    return A


for i in range(Q):
    b = B[i]
    c = C[i]
    A = replace(A, b, c)
    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import *
ip = stdin.readline
N = int(ip())
arr = list(map(int,ip().split()))
for i in range(int(ip())):
    a,b = map(int,ip().split())
    for i in range(N):
        if arr[i] == a:
            arr[i] = b
    print(sum(arr))

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)

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

for i in range(Q):
    if C[i] not in a:
        a[C[i]] = 0

    if B[i] in a:
        a[C[i]] += a[B[i]]
        remove_value = a.pop(B[i])
    
    ans = 0
    for k in a:
        ans += k*a[k]
    print(ans)

Submission status: Time Limit Exceeded

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

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

#最初に全探索で位置をインデックスに登録する
DP=[[] for _ in range(10**5+1)]
for i in range(len(A)):
    DP[A[i]].append(i)#
ans=sum(A)
for i in BC:
    target=i[0]
    nex=i[1]
    for j in DP[target]:
        A[j]=nex
        DP[nex].append(j)
        ans-=target
        ans+=nex
    print(ans)
    DP[target]=[]

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 i in range(q)]

import bisect

a.sort()

su = sum(a)

for i in range(q):
    idx_l = bisect.bisect_left(a,bc[i][0])
    idx_r = bisect.bisect_right(a,bc[i][0])
    
    if idx_l == n:
        print(su)
        continue
    k = (idx_r-idx_l)
    diff = bc[i][1] - bc[i][0]
    a[idx_l:idx_r] = [ bc[i][1] for _ in range(k)]
    a.sort()
    su = su + (diff*k)
    print(su)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
als = list(map(int,input().split()))
ans = sorted(als)
q = int(input())
for i in range(q):
    b, c = map(int,input().split())
    if b in ans:
        cot = ans.count(b)
        ans =[ans[j] if ans[j] != b else c for j in range(n)]        
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
l=int(input())
a=list(map(int,input().strip().split()))
q=int(input())
for i in range(q):
  m,n=map(int,input().strip().split())
  for i in range(len(a)):
    if a[i]==m:
        a[i]=n
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Ac = {}
for a in [ int(i) for i in input().strip().split(' ')]:
    if a in Ac:
        Ac[a] += a
    else:
        Ac[a] = a

Q = int(input())
for q in range(Q):
    B, C = [ int(i) for i in input().strip().split(' ')]
    
    if B in Ac:
        if C in Ac:
            Ac[C] += Ac[B] // B * C
        else:
            Ac[C] = Ac[B] // B * C
        del Ac[B]
        
    print(sum(Ac.values()))

Submission status: Time Limit Exceeded

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

ans=[]

for i in B:
  b=i[0]
  c=i[1]
  A=[i if i!=b else c for i in A ]
  
  ans.append(sum(A))
  
for i in ans:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
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(int(sum(A)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
q = int(input())
BC = [[int(i) for i in input().split()] for _ in range(q)]
for i in range(q) :
    b = BC[i][0]
    c = BC[i][1]
    A = [c if i == b else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

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


for j in range(len(BC)):
    for k in range(len(A)):
        if A[k] == BC[j][0]:
            A[k] = BC[j][1]
    total = sum(A)
    print(total)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D_fix
# CreatedDate:  2020-06-21 22:17:03 +0900
# LastModified: 2020-06-21 22:25:13 +0900
#




def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = [0]*100001
    for s in a:
        cnt[s]+=1
    q = int(input())
    for _ in range(q):
        b, c = map(int, input().split())
        spare = cnt[b]
        cnt[b] = 0
        cnt[c] += spare
        print(sum(i*cnt[i] for i in range(len(cnt))))



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

d = [0] * (10**5+1)
for i in a:
    d[i] += 1

q = int(input())

for _ in range(q):
    b, c = map(int, input().split())
    d[c] += d[b]
    d[b] = 0
    ans = 0
    for i, v in enumerate(d):
        ans += i * v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_str = input()
for a in range(int(input())):
    b, c = input().split()
    list_str = list_str.replace(b, c)
    list = list_str.split()
    list_int = map((lambda x: int(x)), list)
    print(sum(list_int))
    list_int = []

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
lis = np.array(list(map(int, input().split())), dtype = int)
Q = int(input())

for _ in range(Q):
  B, C = map(int, input().split())
  lis[lis == B] = C
  print(sum(lis))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():
    n = int(input())
    a = [int(x) for x in stdin.readline().rstrip().split()]
    q = int(input())
    for i in range(q):
        b, c = [int(x) for x in stdin.readline().rstrip().split()]
        a = [c if x == b else x for x in a]
        print(sum(a))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array([int(k) for k in input().split()],dtype=np.uint64)
q = int(input())
r = int(np.sum(a))
for _ in range(q):
    b, c = [int(k) for k in input().split()]
    d = a == b
    s = len(a[d])
    a[d] = c
    r += (c - b) * s
    print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a =np.array([int(x) for x in input().split()])
q = int(input())
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]

sum1 = np.sum(a[a>10**5])
a = a[a<=10**5]
for i in range(q):
    a = np.sort(a)
    ans = 0
    for j in range(len(a)):
        if a[j] == b[i]:
            a[j] = c[i]
        ans += a[j]
    print(ans+sum1)

Submission status: Time Limit Exceeded

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

for i in range(q) :
    a = [bc[i][1] if j == bc[i][0] else j for j in a]
    ans[i] = sum(a)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = tuple(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
        elif len(d[b]) == 0:
            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)

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

d = defaultdict(int)

N = input()
A = list(map(int, input().split())) 
for i in A:
    d[i] += 1

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

Submission status: Time Limit Exceeded

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

Q = int(input())

same = []
for i in range(10**5+1):
    same.append([])

for i in range(N):
    same[A[i]].append(i)

result = sum(A)

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

    #print(same[B])
    #print(same[C])
    lim = len(same[B])
    result += (C-B)*lim
    print(result)


    for j in range(lim):
        same[C].append(same[B].pop())
        #print(same[B])
        #print(same[C])

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())
BC=[]
for i in range(Q):
    BC.append(list(map(int, input().split())))
BC=np.array(BC)

print()
for i in range(Q):
    if BC[i,0] not in A:
        pass
    else:
        sub_array=(A==BC[i,0])
        not_sub_array=np.logical_not(sub_array)
        sub_array=sub_array*BC[i,1]
        A=A*not_sub_array
        A=A+sub_array
    sum_A=np.sum(A)
    print(sum_A)

Submission status: Time Limit Exceeded

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

N = int(input())

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

Q = int(input())

goukei_AAA = sum(AAA)

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])
        sabun = BC[1] - BC[0]
        AAA = [BC[1] if m == BC[0] else m for m in AAA]
        goukei_AAA = goukei_AAA + (how_many_changes * sabun)
        print(goukei_AAA)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = input()
    Q = int(input())

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

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

        answer = answer + (int(C) - int(B)) * numB
        print(answer)

        A = A.replace(B, C)

if __name__ == "__main__":
    main()

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

bc = np.array([list(map(int, input().split())) for i in range(Q)])

for i in range(Q):
    A[np.where(A == bc[i][0])] = bc[i][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A = sorted(A)
for i in range(Q):
  B, C = map(int,input().split())
  flag = False
  for j in range(N):
    if A[j]==B:
      A[j]=C
      flag = True
    elif flag:
      break

  print(sum(A))

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 = {i:0 for i in range(1, 100001)}
for i in A:
    if i in num:
        num[i] += 1
    else:
        num[i] = 1

for change in changes:
    if change[1] in num:
        num[change[1]] += num[change[0]]
    else:
        num[change[1]] = num[change[0]]
    num[change[0]] = 0
    s = sum([i * c for i, c in num.items()])
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
q = int(input())
cnt_A = dict(Counter(A))
for i in range(q):
    ans = 0
    b, c = map(int, input().split())
    if b in cnt_A and cnt_A[b] > 0:
        m = cnt_A[b]
        cnt_A[b] = 0
        if c not in cnt_A:
            cnt_A[c] = m
        else:
            cnt_A[c] += m
    ans = sum([k * v for k, v in cnt_A.items()])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

Q = int(input())

B = []
C = []

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

    
for s in sums:
    print(s)

Submission status: Time Limit Exceeded

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

nums = {}
n = int(input())
li = list(map(int,input().split()))
q = int(input())
BC = []
for _ in range(q):
    b, c = map(int,input().split())
    BC.append([b, c])

for i in range(n):
    if li[i] in nums:
        nums[li[i]] += 1
    else:
        nums[li[i]] = 1

for i in range(q):
    if BC[i][0] in nums:
        if BC[i][1] in nums:
            nums[BC[i][1]] += nums[BC[i][0]]
            nums[BC[i][0]] = 0
        else:
            nums[BC[i][1]] = nums[BC[i][0]]
            nums[BC[i][0]] = 0
    s = 0
    for i in nums.keys():
        s += nums[i]*i
    print(s)

Submission status: Time Limit Exceeded

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


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

#取得完了

#sum = sum(A)

#print(sum)

for i in range (Q):
  for j in range(N):
    if A[j] == C[i]:
      break
    elif A[j] == B[i]:
      A[j] = C[i]
  A.sort()
  print(sum(A))

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):
    b,c=(map(int,input().split()))
    BC.append([b,c])
#最初に全探索で位置をインデックスに登録する
DP=[[] for _ in range(10**5+1)]
for i in range(len(A)):
    DP[A[i]].append(i)#
for i in BC:
    target=i[0]
    nex=i[1]
    for j in DP[target]:
        A[j]=nex
        DP[nex].append(j)
    print(sum(A))
    DP[target]=[]

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())
  if b in a:
  	a = [c if k==b else k for k 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 i in range(q)]
l1 = []
l2 = []
ans = sum(a)
for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    cnt = a.count(b)
    cnt2 = cnt + l2.count(b) - l1.count(b)
    for j in range(cnt):
        l1.append(b)
        l2.append(c)
    dif = c - b
    ans += dif * cnt2
    print(ans)

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 i in range(Q):
    BTemp,CTemp = map(int, input().split())
    B.append(BTemp)
    C.append(CTemp)
for BTemp,CTemp in zip(B,C):
    A = np.where(A == BTemp, CTemp, A)
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(q):
    a.sort()
    b,c = list(map(int,input().split()))
    for j in range(len(a)):
        if a[j] > b:
            break
        if a[j] == b:
            a[j] = c
            a_sum += c - b
    
    res.append(a_sum)
        
for i in res:
    
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
_=int(input())
A=np.array(list(map(int,input().split())))
Q = int(input())
for i in range(Q):
    B,C=map(int,input().split())
    A=np.where(A==B,C,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()))

a_array = np.zeros((100001), dtype = np.int)
a_array = a_array.reshape((1, 100001))
a_val = np.arange(100001)
a_val = a_val.reshape((1, 100001))
a_val = a_val.transpose()

for i in range(len(a)):
  a_array[0, a[i]] += 1

q = int(input())

ans = np.dot(a_array, a_val)[0,0]

for i in range(q):
  b, c = map(int, input().split())
  if a_array[0, b] != 0:
    a_array[0, c] += a_array[0 ,b]
    a_array[0, b] = 0
    ans = np.dot(a_array, a_val)[0,0]
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
suretu = list(input().split())
Q = int(input())
ans = []
for i in range(Q):
    kei = 0
    x, y = input().split()
    l_replace = [s.replace(x, y) for s in suretu]
    suretu = l_replace
    for k in l_replace:
        kei += int(k)
    ans.append(kei)
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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


dict_n = defaultdict(lambda:0)

for i in alist:
    dict_n[i] += 1

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


for i in range(q):
    
    b = bclist[i][0]
    c = bclist[i][1]

    dict_n[c] += dict_n[b]
    dict_n[b] = 0

    sum = 0
    for key in dict_n:
        sum += key * dict_n[key]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections

num_of_numbers = int(input())
numbers = list(map(int, input().split()))
num_of_operation = int(input())

# intで初期化するため0が返る
cnt = collections.defaultdict(int)
# numbersに含まれる各数字の出現回数を保持
for i in set(numbers):
    cnt[i] = numbers.count(i)
# 初期状態の和を算出
ans = sum(numbers)

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

    # 置換対象の数字が存在する場合
    if B in cnt:
        tmpB = cnt[B]
        tmpC = cnt[C]
        ans = ans - (B * tmpB) + (C * tmpB)

        cnt[C] = tmpC + tmpB
        cnt[B] = 0

    print(ans)