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:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
process = []
proc_append = process.append
for i in range(Q):
	proc_append(tuple(input().split()))
	process[i] = tuple(map(int,process[i]))

for b,c in process:
	A.sort()
	if b in set(A):
		for i in range(N):
			if b==A[i]:
				begin = i
				break
		for i in range(begin,N):
			if b!=A[i]:
				end = i-1
				break
			if i==N-1:
				end = i
		for i in range(begin,end+1):
			A[i] = c
	print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
ch = [[int(c) for c in input().split()] for _ in range(Q)]
for s in ch:
    for i in range(len(A)):
        if A[i]==s[0]:A[i]=s[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
ch = [[int(c) for c in input().split()] for _ in range(Q)]
for s in ch:
    for i, a in enumerate(A):
        if a==s[0]:A[i]=s[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0]*N
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())


S = [0]*Q
for i in range(Q):
    for j in range(N):
        if A[j] == B[i]:
            A[j] = C[i]
    for k in range(N):
        S[i] += A[k] 

for i in range(Q):
    print(S[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect
import numpy as np
n = int(input())
a = list(map(int, input().split()))
q = int(input())
a.sort()
A = list(set(a))
CA = [0]*len(A)
for i,x in enumerate(A):
  l = bisect.bisect_left(a,x)
  r = bisect.bisect_right(a,x)
  CA[i] = r-l

def trans(A,b,c):
  for i,x in enumerate(A):
    if x == b:
      A[i] = c
  return A

A = np.array(A)
CA = np.array(CA) 
for i in range(q):
  b,c = list(map(int, input().split()))
  A = trans(A,b,c)
  ans = A * CA
  print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
a = [int(x) for x in input().split()]
q =int(input())
n= sum(a)


for i in range(q):
    n = sum(a)
    b,c=map(int,input().split())
    print(n + a.count(b)*(c-b))
    a.sort()
    while b in a:
        a[a.index(b)]=c

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

d=[0]*100001
for i in a:
    d[i]+=1

for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans=0
    for i in range(len(d)):
        ans+=i*d[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
n = int(input())
a = list(map(int, input().split()))
q = int(input())
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100000
sum = 0
ans = [0]*q

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

for j in a:
    sum += j

for k in range(q):
    diff = req[k][1] - req[k][0]
    diff = diff * count[req[k][0] - 1]
    count[req[k][1] - 1] += count[req[k][0] - 1]
    count[req[k][0] - 1] = 0
    sum += diff
    ans[k] = sum

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

Submission status: Time Limit Exceeded

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

A = {}
for i in range(n):
  A = dict(Counter(A) + Counter({a[i]:1}))

def sumA(A):
  ans = 0
  for x in A.keys():
    ans += x * A[x]
  return ans

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    A = dict(Counter(A) + Counter({c:A[b]}))
    A = dict(Counter(A) - Counter({b:A[b]}))
  print(sumA(A))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(a)

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

    if(C not in c):
        c[C] = 0

    c[C] += c[B]
    del c[B]

    sum = 0
    for key, cnt in c.items():
        sum += key*cnt

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(a)

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

    if(C not in c):
        c[C] = 0

    c[C] += c[B]
    c[B] = 0

    sum = 0
    for key, cnt in c.items():
        sum += key*cnt

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
data = stdin.readlines()

n = int(data[0])
a = [int(s) for s in data[1].split()]
q = int(data[2])

i = 1
l = []
while i <= q:
    l.append(int(data[i+2].split()[0]))
    l.append(int(data[i+2].split()[1]))
    i += 1

i = 0
while i <= q-1:
    m = 0
    while m <= n-1:
        if a[m] ==  l[2*i]:
            a[m] = l[2*i+1]
        m += 1
    print(sum(a))
    i += 1

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

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

Submission status: Time Limit Exceeded

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

n = int(input())
A = list(map(int,input().split()))
q = int(input())
M=[list(map(int,input().split())) for _ in range(q)]
Sum = sum(A)
d={}
for i in range(n):
    if A[i] not in d:
        d[A[i]]=1
    else:
        d[A[i]]+=1
for i in M:
    a,b=i[0],i[1]
    if a not in d:
        L = list(d.items())
        print(sum([x*y for x,y in L]))
        continue
    elif b in d:
        d[b]+=d[a]
    else:
        d[b]=d[a]
    d.pop(a)
    L = list(d.items())
    print(sum([x*y for x,y in L]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int,input().split()))
Q = int(input())
S =sum(A_list)
for i in range(Q):
    B,C = map(int,input().split())
    count = 0
    for j in A_list:
        if j == B:
            A_list[A_list.index(j)] = C
            count += 1
    S = S + count * (C - B)
    print(S)

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

for i in range(q):
    for j in range(n):
        if bc[i][0] not in a:
            continue
        if a[j] == bc[i][0]:
            a[j] = bc[i][1]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]

ans = 0
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
    ans = 0
    ans = sum(i*j for i, j in A.items())
    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 i in range(q)]

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = [tuple(map(int, input().split())) for i in range(Q)]
A = np.asarray(A)
for b, c in bc:
    A = np.where(A == b, c, A)
    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

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()
Sum = sum(A)

for i in range(Q):
    L = bisect.bisect_left(A, B[i])
    R = bisect.bisect_right(A, B[i])
    num = R - L
    Sum += num * (C[i] - B[i])

    for j in range(L, R):
        A[j] = C[i]

    A.sort()
    print(Sum)

Submission status: Time Limit Exceeded

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

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

d = dict()
for num in S:
    d.setdefault(num, 0)
    d[num] += 1

for _ in range(Q):
    b, c = map(int, input().split())
    d.setdefault(c, 0)
    d.setdefault(b, 0)
    d[c] += d[b]
    d[b] = 0
    ans = sum(s*t for s, t in d.items())
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n=int(input())
a=map(int,input().split())
q=int(input())
b=[[int(i) for i in input().split()] for l in range(q)]

c=collections.Counter(a)

for x in b:
    c[x[0]],c[x[1]]=0,c[x[0]]+c[x[1]]
    print(sum([j*k for j,k in c.items()]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = Counter([int(i) for i in input().split()])
q = int(input())
lst = [[int(i) for i in input().split()] for j in range(q)]

for i in lst:
    a[i[1]] += a[i[0]]
    del a[i[0]]
    print(sum(map(lambda item: item[0]*item[1], a.items())))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from numba import jit
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# --------------------------
@jit('i8(i8[:])')
def change(arr):
    b, c = map(int, input().split())
    arr = np.where(arr == b, c, arr)
    print(np.sum(arr))

    return arr


# -------------------------
n = int(input())
a = list(map(int, input().split()))
'''
aa = ''.join(a)
aaa = str(aa)
#print(type(aa))
# print(a)
'''
aa = np.array(a)
q = int(input())


for i in range(q):
    aa = change(aa)

    # print(aa)
    #aaa.replace(str(b), str(c))
    # print(aaa)

#ab = map(int, aa)
# print(np.sum(aa))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
a_set = set(a)
a_dict = dict([(i, a.count(i))for i in a_set])
a_sum = sum(a)

for i in range(q):
    b,c = map(int,input().split())
    if not b in a_dict:
        print(a_sum)
        continue
    a_sum = a_sum - a_dict[b]*b + a_dict[b]*c
    if c in a_dict:
        a_dict[c] = a_dict[c] + a_dict[b]
    else:
        a_dict[c] = a_dict[b]
    a_dict[b] = 0
    print(a_sum)

Submission status: Time Limit Exceeded

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

for i in range(q):
    b,c = map(int,input().split())
    co = 0
    for j in range(n):
        co = co + (a[j]==b)
        a[j] = c*(a[j]==b)+a[j]*(a[j]!=b)
    a_sum = a_sum - b*co + c*co
    print(a_sum)

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*(a[j]==b)+a[j]*(a[j]!=b) for j in range(n)]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
t=int(input())
a=sorted(a)
for i in range(t):
    d,c=map(int,input().split())
    while(d in a):
     index = a.index(d)
     a[index]=c  
    print(sum(a))

Submission status: Time Limit Exceeded

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

base = sum(A)

Q = int(input())

for i in range(Q):
  B, C = map(int, input().split())
  # Aに現れるBの個数xがわかればよい
  x = A.count(B)
  base += x*(C-B)
  print(base)
  A = [a if a != B else C for a in A]

Submission status: Time Limit Exceeded

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()
nums = [0]*10**6
for i in range(n):
  nums[a[i]]+=1
q=ni()
for i in range(q):
  b,c=nm()
  nums[c]+=nums[b]
  nums[b]=0
  ans=0
  for j in range(len(nums)):
    ans+=nums[j]*j

  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
def sumA(A):
  ans = 0
  for x in A.keys():
    ans += x * A[x]
  return ans

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
t=int(input())
for i in range(t):
    d,c=map(int,input().split())
    while(d in a):
     index = a.index(d)
     a[index]=c  
    print(sum(a))

Submission status: Time Limit Exceeded

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

array = dict()
for i in data:
  if i in array:
    array[i] += 1
  else:
    array[i] = 1
    
for i in range(Q):
  quary = [int(i) for i in input().split()]
  if quary[0] not in array:
    pass
  elif quary[1] in array:
    array[quary[1]] += array[quary[0]]
    del array[quary[0]]
  else:
    array[quary[1]] = array[quary[0]]
    del array[quary[0]]
  
  total = 0
  for i, j in array.items():
    total += i*j
  print(total)

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(len(b)):
    for j in range(len(a)):
        if a[j]==b[i]:
            a[j]=c[i]
    

    print(sum(a))

Submission status: Time Limit Exceeded

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

total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    numb = num[B]
    num[B] -= numb
    num[C] += numb
    print(total + (C - B) * numb)
    total=total + (C - B) * numb

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())
  if a.count(b):
    l = a.index(b)
    r = n - a[::-1].index(b)
    for i in range(l, r):
      if a[i] == b:
        a[i] = 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))

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

s = sum(a)

for b, c in bc:
    s += (c - b)*num[b]
    num[c] += num[b]
    num[b] = 0

    print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = [int(x) for x in input().split()]
Q = int(input())
d = []
somme = []
e = []
a=b=0
for i in range(Q):
    a,b = [int(x) for x in input().split()]
    if a in seq:
        e = [ b if x == a else x for x in seq][:]
    else:
        e = seq[:]
    somme.append(sum(e))
    seq = e[:]
for _ in somme:
    print(_)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

seq = [int(x) for x in input().split()]
Q = int(input())
d = []
somme = []
e = []
a=b=0
for i in range(Q):
    a,b = [int(x) for x in input().split()]
    if a in seq:
        e = [ b if x == a else x for x in seq][:]
    else:
        e = seq[:]
    somme.append(sum(e))
    seq.clear()
    seq = e[:]
    e.clear()
for i in somme:
    print(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    temp = [C if j == B else j for j in A_list]
    print(sum(temp))
    A_list = temp.copy()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort()
s = set(a)
c = collections.Counter(a)
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    total = 0
    for key, val in c.items():
#         print(key, val)
        total += key * val
    print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for _ in range(int(input())):
    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()))
n=sum(A)
for _ in range(int(input())):
    B,C=map(int,input().split())
    n=n-A.count(B)*(B-C)
    A=[C if i==B else i for i in A]
    print(n)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = list()
C = list()
for i in range(Q):
  b, c = map(int, input().split())
  B.append(b)
  C.append(c)
sorted(A)
A_sum = sum(A)

break_flg = False
for i in range(Q):
  count = 0
  break_flg = False
  bi = B[i]
  ci = C[i]
  for j in range(len(A)):
    ai = A[j]
    if bi == ai:
      A[j] = C[i]
      break_flg = True
      count += 1
    
    if (bi != ai or j == len(A)-1) and break_flg == True:
      A_sum += (count * (ci - bi))
      print(A_sum)
      break
    elif break_flg == False and j == len(A)-1:
      print(A_sum)

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    max_num = sum(a_list)
    ans = [0]*q
    for i in range(q):
        b,c = map(int, input().split())
        count = 0
        for j in range(n):
            if b==a_list[j]:
                count += 1
                a_list[j] = c
        
        max_num += (c - b)*count
        ans[i] = max_num

    for i in ans:
        print(i)
   
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    ans = [0]*q
    for i in range(q):
        b,c = map(int, input().split())
        for j in range(n):
            if b == a_list[j]:
                a_list[j] = c
        ans[i] = sum(a_list)
        
    for i in ans:
        print(i)
   
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = [int(x) for x in input().split()]
Q = int(input())
somme = []
for i in range(Q):
  l = [int(x) for x in input().split()]
  a = l[0]
  b = l[1]
  if a in seq:
   	e = [ b if x == a else x for x in seq]
  else:
    e = seq[:]
  somme.append(sum(e))
  seq.clear()
  seq = e[:]
  e.clear()
for i in somme:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = [int(x) for x in input().split()]
Q = int(input())
somme = []
for i in range(Q):
  l = [int(x) for x in input().split()]
  a = l[0]
  b = l[1]
  if a in seq:
   	e = [ b if x == a else x for x in seq]
  else:
    e = seq[:]
  somme.append(sum(e))
  seq.clear()
  seq = e[:]
  e.clear()
for i in somme:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
BC = [list(map(int,input().split())) for _ in range(Q)]
numA = []
for i in range(1, 10**5+1):
  numA.append(A.count(i))
ans = sum(A)
for l in BC:
  ans = ans + ((l[1] - l[0]) * numA[l[0]-1])
  print(ans)
  numA[l[1]-1] = numA[l[0]-1] + numA[l[1]-1]
  numA[l[0]-1] = 0

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

seq = [int(x) for x in input().split()]
Q = int(input())
somme = []
for i in range(Q):
    l = [int(x) for x in input().split()]
    a = l[0]
    b = l[1]
    if a in seq:
        e = [ b if x == a else x for x in seq]
    else:
        e = seq[:]
    somme.append(sum(e))
    seq.clear()
    seq = e[:]
    e.clear()
for i in somme:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort()
s = set(a)
c = collections.Counter(a)
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    if ca in c:
        c[ca] += c[cf]
#         del sd[cf]
        del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
#         del sd[cf]
        del c[cf]
    total = 0
    for key, val in c.items():
#        print(key, val)
        total += key * val
    print(total)

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

for i in range(o):
    summ=0
    a,b=map(int,input().split())
    for x in range(n):
        if l[x]==a:
            l[x]=b 
        
        summ+=l[x]
    print(summ)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
count = Counter(a)
q = int(input())
b_c = [list(map(int, input().split())) for i in range(q)]
for i in b_c:
  count[i[1]] += count[i[0]]
  count[i[0]] = 0
  ans = 0
  for k, v in count.items():
    ans += k * v
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
BC = [[int(x) for x in input().split()]for i 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 = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
BC = [[int(x) for x in input().split()]for i in range(Q)]

Sum = sum(A)
for i in range(Q):
  b = BC[i][0]
  c = BC[i][1]
  Sum += (c-b)*A.count(b)
  print(Sum)
  A = [c if i == b else i for i in A]

Submission status: Time Limit Exceeded

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

sum_l=[]
for i in range(C):
  D = list(map(int, input().split()))
  
  for h in range(A):
    if B[h] == D[0]:
      B[h]=D[1]
    
  print(sum(B))

Submission status: Time Limit Exceeded

Solution code:
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
n = int(input())
a = list(map(int, input().split()))
q = int(input())
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100000
sum = 0

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

for j in a:
    sum += j

for k in range(q):
    diff = req[k][1] - req[k][0]
    diff = diff * count[req[k][0] - 1]
    count[req[k][1] - 1] += count[req[k][0] - 1]
    count[req[k][0] - 1] = 0
    sum += diff
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
input()
A = [int(a) for a in input().split()]
d = Counter(A)
sum_ = sum(A)
for q in range(int(input())):
    B,C = map(int,input().split())
    sum_ += ((C - B) * d[B])
    d[C] += d[B]
    del d[B]
    sum2 = 0
    for k,v in d.items():
        sum2 += k*v
    print(sum2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
for q in range(int(input())):
    B,C = input().split()
    A = list(map(lambda x: x.replace(B,C),A))
    print(sum(map(lambda x: int(x),A)))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

for _ in range(q):
    cf, ca = map(int, input().split())
    na[na == cf] = ca
    print(na.sum())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
initial=list(map(int,input().split(" ")))
q=int(input())
for _ in range(q):
    x,y=map(int,input().split(" "))
    for i in range(len(initial)):
        if initial[i]==x:
            initial[i]=y
        else:
            continue
    print(sum(initial))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
a = list(input().split())
A = [int(a[i]) for i in range(N)]
Q = int(input())
Query = [input().split() for i in range(Q)]
SUM = sum(A)
num = [A.count((i+1)) for i in range(100000)]

for i in range(Q):
    B = int(Query[i][0])
    C = int(Query[i][1])
    SUM += (C-B) * num[B-1]
    print(SUM)
    num[C-1] += num[B-1]
    num[B-1] = 0

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
array = []
for i in range(Q):
    m,n = [int(x) for x in input().split()]

    for j in range(len(A)):
        if m == A[j]:
            A[j] = n
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
array = []
for i in range(Q):
    m,n = [int(x) for x in input().split()]

    for j in range(len(A)):
        if m == A[j]:
            A[j] = n
    array.append(sum(A))
for i in array:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(input().split())
Q = int(input())
for i in range(Q):
    b,c = input().split()
    A = list(map(lambda x:x.replace(b,c),A))
    B = A.copy()
    print(sum(map(int, B)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
array = []
for i in range(Q):
    m,n = [int(x) for x in input().split()]
    array.append([m,n])
operation = []
for i in range(len(array)):
    for j in range(len(A)):
        if array[i][0] == A[j]:
            A[j] = array[i][1]
    print(sum(A))

Submission status: Time Limit Exceeded

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


for i in range(Q):
    b, c = map(int, input().split())
    for j, name in enumerate(An):
        if An[j] == b:
            An[j] = c
    
    print(sum(An))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = 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):
    for j, name in enumerate(An):
        if An[j] == b[i]:
            An[j] = c[i]
    
    print(sum(An))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = 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):
    for j, name in enumerate(An):
        if An[j] == b[i]:
            An[j] = c[i]
    
    print(sum(An))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

counter = collections.Counter(a)
s = sum(a[:])
change = []
count = []
for i in range(q):
    s += (c[i]-b[i])*counter[b[i]]
    for j in range(len(change)):
        if b[i] == change[j]:
            s += (c[i]-b[i])*count[j]
    change.append(c[i])
    count.append(counter[b[i]])
    print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 21:04:33 2020

@author: naoki
"""

N = int(input()) # input the row A length "N" 
A = list(map(int,input().split())) # input the row of A 
Q = int(input()) # input the number of trying operation 
BC = [list(map(int,input().split())) for i in range(Q)]
S = 0 
for i in A:
    S += i# Sum of all conponents of the row "A"
for B,C in BC:
    count = 0 # renewing count 
    count = A.count(B) # counting the number of "B"
    A = [C if i == B else i for i in A]   # changing B to C  
    S = S + (C - B) * count 
    print(S)