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 bisect

n=int(input())
a=[int(x) for x in input().rstrip().split()]
q=int(input())
a.sort()
for i in range(q):
  b,c=map(int,input().split())
  b_l=bisect.bisect_left(a,b)
  b_r=bisect.bisect_right(a,b)
  a=a[:b_l]+[c]*(b_r-b_l)+a[b_r:]
  print(sum(a))

Submission status: Time Limit Exceeded

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

d=[0]*(10**5 +1 )

for i in a:
    d[i-1]+=i

for _ in range(q):
    b,c=map(int,input().split())
    ans=0
    k=d[b-1]//b
    d[c-1]+=c*k
    d[b-1]=0

    print(sum(d))

Submission status: Time Limit Exceeded

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

for i in B:
    A = [str(s) for s in A]
    ans  =[d.replace(i[0],i[1]) for d in A]
    A = [int(s) for s in ans]
    print(sum(A))

Submission status: Time Limit Exceeded

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

N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
#print(N,A,Q,BC)
for i in range(Q):
    b,c = BC[i]
    #print(b,c,A)
    A = [c if a == b else a for a in A]
    #print(A)
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def MI():
    return map(int, input().split())

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

# N = int(input())
# group = 0
# tmp = 0

# while tmp < N: 
#     tmp += 26**group
#     group += 1
    
# print(group)

from collections import Counter

N = int(input())
A = LI()
Q = int(input())
A_c = Counter(A)

for _ in range(Q):
    B, C = MI()
    tmp = A_c[B]
    A_c[B] = 0
    
    if B in A_c.keys():
        A_c[C] += tmp
    else:
        A_c[C] = tmp
    
    ans = 0
    
    
    for k, v in A_c.items():
        ans += k * v

    print(ans)

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

for operation in operations:
    numbers = np.where(numbers == operation[0], operation[1], numbers)
    print(sum(numbers))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

for j in range(q):
    if info[j][0] in a_dict.keys():
        if info[j][1] in a_dict.keys():
            a_dict[info[j][1]] += a_dict[info[j][0]]
            del a_dict[info[j][0]]
        else:
            a_dict[info[j][1]] = a_dict[info[j][0]]
            del a_dict[info[j][0]]
    ans = 0
    a_key = list(a_dict.keys())
    a_value = list(a_dict.values())
    for k in range(len(a_dict)):
        ans += a_key[k] * a_value[k]

    print(ans)

Submission status: Time Limit Exceeded

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

print("\n".join([str(i) for i in j]))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = input()
inp = map(int, input().split())
dic = dict(collections.Counter(inp))
wa = 0
for i in range(len(dic)):
    wa += list(dic.keys())[i] * list(dic.values())[i]
for i in range(int(input())):
    a, b = map(int, input().split())
    se = set(dic.keys())
    if a in se:
        wa += dic[a] * (b-a)
        if b in se:
            pass
        else:
            dic[b] = 0
        dic[b] += dic[a]
        dic[a] = 0
    print(wa)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]


num = int(input())
for i in range(num):
    B, C = map(int, input().split())
    indexes = [i for i, x in enumerate(A) if x == B]
    for j in range(len(indexes)):
        A[indexes[j]] = C
    print(sum(A))

Submission status: Time Limit Exceeded

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

d=[0]*(10**6)

for i in a:
    d[i-1]+=i

for _ in range(q):
    b,c=map(int,input().split())
    ans=0
    k=d[b-1]//b
    d[c-1]+=c*k
    d[b-1]=0

    print(sum(d))

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):
  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:
#N = int(input())
#A = list(map(int, input().split()))
#a, b = map(int, input().split())
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())
for i in range(q):
    b,c=map(int,input().split())
    for l in range(n):
        if a[l]==b:
            a[l]=c
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline
read = sys.stdin.read

n = int(readline())
a = np.array(readline().split(), dtype=np.int64)
q = int(readline())
bc = np.array(read().split(), dtype=np.int64)

for i in range(q):
    a = np.where(a == bc[i*2], bc[i*2+1], a)
    print(np.sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
  a = [operation[i][1] if num == operation[i][0] else num for num in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
As = np.array(input().split()).astype(np.int64)

Q = int(input())

for q in range(Q):
  B, C = map(int, input().split())
  As[np.where(As==B)] = C
  print(As.sum())

Submission status: Time Limit Exceeded

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

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

a.sort()

for i in range(q):
    if b[i] in a:
        s = a.index(b[i])
        while s < n and a[s] == b[i]:
            a[s] = c[i]
            s += 1
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
x=int(input())

y=[int(i) for i in input().split()]

z=int(input())

for i in range(z):
    a, b= map(int , input().split())
    v=[]
    for ele in y:
        if ele == a:
            v.append(b)
        else:
            v.append(ele)

    sum1 = 0
    for ele in v:
        sum1 += ele
    y=v
    print(sum1)

Submission status: Time Limit Exceeded

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

for i,j in BC:
  for l in range(N):
    A[l]=A[l].replace(i,j)
  ans=0
  for k in A:
    ans+=int(k)
  print(ans)

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())
    newA = a
    for i in a:
        if B in newA:
            newA[a.index(B)] = C
    print(sum(newA))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
BC = [list(map(int, sys.stdin.readline().split())) for i in range(Q)]
for i in range(Q):
    B, C = BC[i]
    A[A == B] = C
    print(sum(A))

Submission status: Time Limit Exceeded

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

q = []

for i in range(Q):
    B, C = list(map(int, input().split()))
    for i in range(N):
        if A[i] > B:
            break
        if A[i] == B:
            A[i] = C

    A.sort()
    q.append(sum(A))

for i in q:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
total=0
for i in range(Q):
    B, C = map(int, input().split())
    for j in range(N):
        if A[j]==B:
            A[j]=C
        total=total+A[j]
    print(total)
    total=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())
    a = [i if i != b else c 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 = [0] * Q
C = [0] * Q
for i in range(Q):
    B[i],C[i] = map(int,input().split())
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 numpy as np

N = int(input())
arr = np.array(list(map(int, input().split())))

Q = int(input())

for i in range(Q):
    B, C = list(map(int, input().split()))
    arr[arr == B] = C
    print(arr.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())

for i in range(q):
    for j in range(n):
        if b[i]  == a[j]:
            a[j] = c[i]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

d = Counter(As)

Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    if B in d:
        d[C] += d[B]
        d[B] = 0
    
    print(sum(k*v for k, v in d.items()))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]

num = int(input())
for i in range(num):
    B, C = map(int, input().split())
    indexes = [i for i, x in enumerate(A) if x == B]
    for j in range(len(indexes)):
        A[indexes[j]] = C
    sum = 0
    for i in range(N):
        sum += A[i]
    print(sum)

Submission status: Time Limit Exceeded

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

for i in que:
    for j in range(N):
        if arr[j] == i[0]:
            arr[j] = i[1]
    print(sum(arr))

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    A = list(input().split())
    ans = ','.join(A)

    Q = int(input())

    replaces = []
    for i in range(Q):
        pp, ss = input().split()
        replaces.append([pp, ss])

    for varcher in replaces:
        ans = ans.replace(varcher[0], varcher[1])
        print(sum(map(int, ans.split(','))))


resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int,input().split()))
t = int(input())
su = 0
while t:
    t-=1
    k,r = map(int,input().split())
    p = arr.count(k)
    su = sum(arr)-sum([k]*p)+sum([r]*p)
    arr = list(filter((k).__ne__, arr))
    arr.extend([r]*p)
    print(su)

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 = [int(x) for x in input().split()]
    A = [C if a == B else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=list(map(int,input().split()))
q=int(input())
s=0
for j in range(q):
	r,s=map(int,input().split())
	d=[]
	for i in range(len(c)):
		if(c[i]==r):
			d.append(i)
	for i in d:
		c[i]=s
	print(sum(c))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N = I()
    A_list = LI()
    Q = I()
    act_list = LLIN(Q)
    for z in act_list:
        A_list
        A_list = [z[1] if i == z[0] else i for i in A_list]
        print(sum(A_list))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(q):
  s=list(map(int, input().split()))
  np.place(num, num==s[0], s[1])
  print(sum(num))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    b, c = map(int, input().split())
    a = [c if e == b else e for e in a]
    l.append(a)

for a in l:
    print(sum(a))

Submission status: Time Limit Exceeded

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


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


def str_array():
    return input().strip().split()

######################### TEMPLATE ENDS HERE ##################################

n = int(input())
arr = int_array()
b, c = [], []
n_c = int(input())
for i in range(n_c):
    tmp_b, tmp_c = int_array()
    b.append(tmp_b)
    c.append(tmp_c)

for i in range(n_c):
    for j in range(n):
        if arr[j] == b[i]:
            arr[j] = c[i]
    print(sum(arr))
    # print(arr)

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:
# Replacing 
def replace(arr, q):
    for cur, new in q:
        for i, num in enumerate(arr):
            if num == cur:
                arr[i] = new
        print(sum(arr))

n = int(input())
ar = list(map(int, input().rstrip().split()))
q = int(input())
qar = []
for i in range(q):
    x = list(map(int, input().rstrip().split()))
    qar.append(x)
replace(ar, qar)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def get_sum(count):
    return sum([key * count[key] for key in count.keys()])
n = int(input())
a = list(map(int, input().split()))
q = int(input())
count = Counter(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    count[c] += count[b]
    count[b] = 0
    print(get_sum(count))

Submission status: Time Limit Exceeded

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

D = dict()
for a in A:
    if a not in D.keys():
        D[a] = 0
    D[a] += 1

for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    
    if B in D.keys():
        if C not in D.keys():
            D[C] = 0
        D[C] += D[B]
        del D[B]

    ans = 0
    for k, v in D.items():
        ans += k * v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
a = list(map(int, input().split()))
q = int(input())
s = []
S = sum(a)
def find(b, a, c): #配列aからbを探し出して,bをcに変換
  counter = 0
  for i in range(len(a)):
    if a[i] == b:
      a[i] = c
      counter += 1
  return counter
for i in range(q):
  b, c = map(int,input().split())
  count = find(b,a,c)
  S += count * (c - b)
  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())

BC = []
for _ in range(Q):
    b, c = map(int, input().split())
    BC.append([b, c])

C = Counter(A)
for bc in BC:
    b = bc[0]
    c = bc[1]
    if b in C:
        if c in C:
            C[c] += C[b]
            C[b] = 0
        else:
            C[c] = C[b]
            C[b] = 0
    print(sum([k*v for k, v in C.items()]))

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 x in BC:
    for y in range(N):
        if A[y] == x[0]:
            A[y] = x[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
m = list(map(int, input().strip().split(' ')))
s = int(input())

for i in range(s):
	a = list(map(int, input().strip().split(' ')))
	for j in range(len(m)):
		if m[j]==a[0]:
			m[j]=a[1]
	print(sum(m))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
al = [int(i) for i in input().split()]
q = int(input())

counter = collections.Counter(al)

for _ in range(q):
  b, c = [int(i) for i in input().split()]
  count = counter[b]
  counter[b] = 0
  counter[c] += count
  total = 0
  for (v, c) in counter.most_common():
    total += v * c
  print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
a = list(map(int, input().split()))
q = int(input())
s = []
S = sum(a)
def find(b, a, c): #配列aからbを探し出して,bをcに変換
  counter = 0
  for i in range(len(a)):
    if a[i] == b:
      a[i] = c
  return a
for i in range(q):
  b, c = map(int,input().split())
  a = find(b,a, 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())
  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())

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
  b, c = map(int, input().split(' '))
  sum_num = 0
  for j in range(len(nums)):
    if nums[j] == b:
      nums[j] = c
    sum_num += nums[j]
  print(sum_num)

Submission status: Time Limit Exceeded

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

d={}

for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1

for _ in range(q):
    b,c=map(int,input().split())
    ans=0
    if c in d:
        if b in d:
            d[c]+=d[b]
    else:
        if b in d:
            d[c]=d[b]

    if b in d:
        d[b]=0

    for k,v in d.items():
        ans+=k*v

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = map(int, input().split())
Q = int(input())

counter = Counter(A)
for i in range(Q):
  b, c = map(int, input().split())
  count_b = counter[b]
  counter[b]-= count_b
  counter[c] += count_b
  print(sum([k*v for k, v in counter.items()]))

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

for i in range(q):
    ans = 0
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
        ans += a[j]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=list(map(int,input().split()))
q=int(input())
for i in range(q):
    x,y=(map(int,input().split()))
    while(1):
        if x not in arr:
            break
        else:
            po=arr.index(x)
            arr[po]=y
    print(sum(arr))

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    a_li = list(map(int, input().split()))
    q = int(input())

    ans_li = []

    for _ in range(q):
        b, c = map(int, input().split())
        a_li = [c if i == b else i for i in a_li]
        ans_li.append(sum(a_li))

    for s in ans_li:
        print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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



A = list(A)
for _ in range(Q):
	# replace every element whose value is B with C
	B, C = map(int, input().split())

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

Submission status: Time Limit Exceeded

Solution code:
def solve():
    for i in range(len(arr)):
        if arr[i] == b:
            arr[i] = c
    return sum(arr)

if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    q = int(input())
    for i in range(q):
        b, c, = map(int, input().split())
        print(solve())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int,input().split()))
t = int(input())
while t:
    t-=1
    k,r = map(int,input().split())
    for i in range(n):
        if arr[i] == k:
            arr[i]=r
    print(sum(arr))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
s=sum(a)
data=[0]*10**5
for i in a:
  data[i-1]+=1
for i in range(q):
  b,c=map(int,input().split())
  s+=(c-b)*data[b-1]
  print(s)
  data[c-1]+=data[b-1]
  data[b-1]=0

Submission status: Accepted

Solution code:
n = int(input())
array = list(map(int,input().split()))
q = int(input())
sums = sum(array)
cnt = [0]*(10**5+1)
for i in array:
    cnt[i] += 1
for j in range(q):
    b,c = map(int,input().split())
    sums += cnt[b]*(c-b)
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(sums)

Submission status: Accepted

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

total = sum(a)
s = collections.Counter(a)


for x in range(q):
    if bc[x][0] in s and bc[x][1] in s:
        s[bc[x][1]] += s[bc[x][0]]
        total += (bc[x][1]-bc[x][0]) * s[bc[x][0]]
        del s[bc[x][0]]
    elif bc[x][0] in s and bc[x][1] not in s:
        s[bc[x][1]] = s[bc[x][0]]
        total += (bc[x][1]-bc[x][0]) * s[bc[x][0]]
        del s[bc[x][0]]
    print(total)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
from collections import Counter
def solve():
    N = int(input())
    a = list(map(int,input().split()))
    Q = int(input())
    query = [tuple(map(int,inp.split())) for inp in stdin.read().splitlines()]
    cnt = Counter(a)
    s = sum(a)
    res = []
    for b,c in query:
        if cnt[b] != 0:
            cnt[c] += cnt[b]
            s += (c-b)*cnt[b]
            res.append(s)
            del cnt[b]
        else:
            res.append(s)
    print('\n'.join(map(str,res)))






if __name__ == '__main__':
    solve()