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:
n = int(input(""))
s = list(map(int,input("").split()))
q = int(input(""))
for i in range(q):
    b_i, c_i = map(int,input("").split())
    for i in range(len(s)) :
        if s[i] == b_i:
           s[i] = c_i

    print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
la = list(map(int, input().split()))
q = int(input())
ls = sum(la)
lc = []
lc_cnt = []
for i in range(q):
  b, c = map(int, input().split())
  cnt = la.count(b)
  if lc.count(b) > 0:
    j = 0
    for k in lc:
      cnt += lc_cnt[j] if k == b else 0
      j += 1
  ls += (c-b)*cnt
  
  lc.append(c)
  lc_cnt.append(cnt)
  print(ls)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = input()

Q = int(input())

sumlist = []

for i in range(Q):
    B, C = input().split()
    A = A.replace(B,C)
    A_copy = list(map(int,A.split()))
    sumlist.append(sum(A_copy))
    

for k in range(len(sumlist)):
    print(sumlist[k])

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    alis = list(map(int, input().split()))
    adic = {}
    for num in alis:
        if num in adic:
            adic[num] += 1
        else:
            adic[num] = 1
    
    anslis = []

    q = int(input())
    bclis = []
    for i in range(q):
        b, c = map(int,input().split())
        if b in adic:
            kazu = adic[b]
            adic[b] = 0
            if c in adic:
                adic[c] += kazu
            else:
                adic[c] = kazu
        ans = 0
        for key in adic:
            ans += adic[key] * key
        anslis.append(ans)
    for num in anslis:
        print(num)
    



    

 
    
    
if __name__ == "__main__":
    main()

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()))
a.sort()
a.append(0)
q=int(input())
for i in range(q):
    bi,ci=map(int,input().split())
    t=0
    for j in range(n):
        if t==1:
            break
        else:
            d=j
            while a[d]==bi:
                a[d]=ci
                d+=1
                t=1
    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 =[i[1] if j ==i[0] else j for j in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
List = list(map(str, input().split()))
Q = int(input())
x = [0] * Q
y = [0] * Q
for i in range(Q):
    x[i], y[i] = map(str, input().split())
    y[i]=int(y[i])

wa=0
for i in List:
    wa+=int(i)

for i in range(Q):
    c = collections.Counter(List)
    num=c['{}'.format(x[i])]
    wa+=num*(y[i]-int(x[i]))
    List=[s.replace('{}'.format(x[i]), '{}'.format(str(y[i]))) for s in List]
    print(wa)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy
 
import sys
import string
def get_int(): return int(sys.stdin.readline().strip())
def get_ints(): return map(int, sys.stdin.readline().strip().split()) 
def get_list(): return list(map(int, sys.stdin.readline().strip().split())) 
def get_string(): return sys.stdin.readline().strip()
def out(data): return sys.stdout.write(str(data))
def out_list(arr): return sys.stdout.write(' '.join(str(e) for e in arr))

MAX = 10**9
MOD = 10**9 + 7

n = get_int()
arr = get_list()
q = get_int()
sum_ = sum(arr)

for _ in range(q):
	a, b = get_ints()
	if a in arr:
		sum_ += arr.count(a) * (b - a)
		arr = [b if x == a else x for x in arr]
	out(sum_)
	out('\n')

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

total = sum(A)

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    q=int(input())
    qu=[]
    pr=[]
    for i in range(q):
        qu.append(list(map(int,input().split(' '))))
    for i in range(q):
        for j in range(n):
            if a[j]==qu[i][0]:
                a[j]=qu[i][1]
        pr.append(sum(a))
    for i in pr:
        print(i)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


for q in range(Q):
    b, c = map(int, input().split())
    ind = [i for i, x in enumerate(A) if x==b]
    for i in ind :
        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())
b = []
c = []
l = [list(map(int, input().split())) for i in range(q)]
for i in range(len(l)):
    b.append(l[i][0])
    c.append(l[i][1])
for i in range(q):
    if b[i] in a:
        for j in range(len(a)):
            if a[j] == b[i]:
                a[j] = c[i]
        print(sum(a))
    else:
        print(sum(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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', '')

import numpy as np
from collections import Counter


def main():
    N = I()
    A = LI()
    Q = I()
    act_list = LLIN(Q)
    cnt = Counter(A)
    # print(cnt)
    for z in act_list:
        ans = 0
        b_num = cnt[z[0]]
        cnt[z[0]] = 0
        cnt[z[1]] = cnt[z[1]] + b_num
        # print(cnt)
        # A_list = [z[1] if i == z[0] else i for i in A_list]
        # np.place(np_array, np_array == z[0], z[1])
        for k, v in cnt.items():
            ans = ans + k*v
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
ope=[]
for i in range(Q):
  ope.append(list(map(int,input().split())))
ope=np.array(ope)
A=np.array(A)

for b,c in ope:
  A[A==b]=c
  print(np.sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
al = list(map(int, input().split()))
q = int(input())
bl = []
cl = []
for i in range(q):
    b,c = map(int, input().split())
    bl.append(b)
    cl.append(c)

shoki = sum(al)
for i in range(q):
    b = bl[i]
    c = cl[i]
    kaisu = al.count(b)
    al = [c if i == b else i for i in al]
    tasu = kaisu * (c - b)
    shoki += tasu
    print(shoki)

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 = [c if v==b else v for v in li]
    print(sum(li))

Submission status: Time Limit Exceeded

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

#for i in range(Q):
  #for j in range(N):
    #if(A_list[j] == list_list[i][0]):
      #A_list[j] = list_list[i][1]
  #print(sum(A_list))
  
  
  
for i in range(Q):
  sum_ = sum(A_list)
  #変更の個数
  cosuu = A_list.count(list_list[i][0])
  sa = list_list[i][1] - list_list[i][0]
  henkou = sa*cosuu
  print(sum_ + henkou)
  for j in range(cosuu):
    A_list.remove(list_list[i][0])
    A_list.append(list_list[i][1])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
  B,C=map(int,input().split())
  a=L.count(B)
  for j in range(a):
    L.remove(B)
    L.append(C)
  print(sum(L))

Submission status: Time Limit Exceeded

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

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

    A = A_replace
    S[i] = sum(A_replace)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
n=int(input())
a=list(map(int,input().split()))
a=Counter(a)
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  if a[c]:
    a[c]+=a[b]
  else:a[c]=a[b]
  a[b]=0
  print(sum(sorted(a.elements())))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A =list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    B,C = (int(x) for x in input().split())
    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())

    for i in range(A.count(B)):
        A[A.index(B)] = C

    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
S = [0]*Q

for i, bc in enumerate(BC):
  b = bc[0]
  c = bc[1]
  s = 0
  for j, v in enumerate(a):
    if v == b:
      s += c
      a[j] = c
    else:
      s += v
  S[i] = s

[print(i) for i in S]

Submission status: Time Limit Exceeded

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

from collections import Counter
a = Counter(A)
x = 0

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

    s = 0
    for i,v in a.items():
        if v != 0:
            s += i*v
    print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
nums = list(map(int, input().split()))
q = int(input())
vals = [tuple(map(int, input().split())) for _ in range(q)]

c = Counter(nums)

for b, k in vals:
    o = c[b]
    del c[b]
    c[k] += o
    total = sum([x*y for x, y in c.items()])
    print(total)

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

# print("end")
for k in range(q):
    
    rep = [c[k] if i == b[k] else i for i in a]
    # print(rep)
    a = rep
    res = sum(rep)
    print(res)

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

# print(n,a,q,B)

# from collections import Counter

# _a = Counter()

for (b, c) in B:
    a = [c if x == b else x for x in a]
    print(sum(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
  b, c = map(str, input().split())
  num = ",".join(a)
  num = num.replace(b, c)
  a = num.split(",")
  ans = map((lambda x:int(x)), a)
  print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

Q = int(input())

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

    B.append(tmp_b)
    C.append(tmp_c)

## -- input -- ##

## -- process -- ##

for b, c in zip(B, C):
    A = [c if a == b else a for a 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, 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:
n = int(input())
a = list(map(int, input().split()))

q = int(input())

from collections import Counter

ca = Counter(a)

def sum_dict(dic):
    return sum([num*cnt for num,cnt in dic.items()])

for _ in range(q):
    b,c = list(map(int, input().split()))
    
    if b in ca:
        cnt = ca.pop(b)
        ca[c] += cnt
    
    print(sum_dict(ca))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(Q):
    b, c = map(int, input().split())
    a[a == b] = c
    print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=sum(A)
print(ans)

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    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())
ans = []
for q in range(Q):
    b, c = map(int, input().split())
    a = np.where(a==b, c, a)
    ans.append(sum(a))
for q in range(Q):
    print(ans[q])

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = input().split()
Q = int(input())
BCs = []
for a in range(Q):
    BCs.append(input().split())

for b in range(Q):
    for c in range(N):
        if As[c] == BCs[b][0]:
            As[c] = BCs[b][1]
    R = 0
    for d in As:
        R += int(d)
    print(R)

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())
    wa=0
    for j in range(len(A)):
        if A[j]==b:
            A[j]=c
        wa+=A[j]
    print(wa)

Submission status: Time Limit Exceeded

Solution code:
def tot(x):
	tot= 0
	for d in x:
		tot+= d
	return tot

n = int(input())
start= list(map(int, input().split()))
q= int(input())
for _ in range(q):
	b,c= map(int, input().split())
	for index in range(len(start)):
		if start[index]==b:
			start[index]=c
	print(tot(start))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

for i in range (Q):
    for j in range (N):
        if A[j]==grid[i][0]:
            A[j]=int(grid[i][1])

    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

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

for search, replace in replace_map:
    A = np.where(A==search, replace, 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()))
Q=int(input())
ope=[]
for i in range(Q):
  ope.append(list(map(int,input().split())))

A=np.array(A)
for b,c in ope:
  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())
bc = [list(map(int,input().split())) for _ in range(q)]

# sum = sum(a)

for i in bc:
    b = i[0]
    c = i[1]
    a = [c if i==b else i for i in a]
    # print(a)
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import numpy as np
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    a = np.array(list(IL()),dtype='int64')
    q = I()
    s = deque()
    for rep in range(q):
        b,c = IL()
        a[a==b] = c
        s.append(sum(a))
    for _ in range(q):
        print(s.popleft())
    return

if __name__=='__main__':
    Main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

ttl = sum(A)
cnt = Counter(A)

Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    
    if b in cnt:
        occ = cnt.pop(b)
        ttl = ttl - b * occ
        cnt = cnt + Counter({c: occ})
        ttl = ttl + c * occ
    
    print(ttl)

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())
alist = [int(s) for s in input().split(" ")]
adict = {}
for a in alist:
  if a in adict:
    adict[a] += 1
  else:
    adict[a] = 1
q = int(input())
bclist = []
for _ in range(q):
  b, c = [int(s) for s in input().split(" ")]
  bclist.append([b, c])

for b, c in bclist:
  if c in adict and b in adict:
    adict[c] += adict[b]
    del adict[b]
  elif b in adict:
    adict[c] = adict[b]
    del adict[b]
  ans = 0
  for k, v in adict.items():
    ans += k * v
  print(ans)

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):
    A.sort()
    b,c = BC[i]
    #print(b,c,A)
    for j in range(N):
        if A[j] > b:
            break
        else:
            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())
  A = [C if i == B else i for i in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list_A = [int(item) for item in input().split()]
Q = int(input())
list_bc = []
for i in range(Q):
    list_bc.append([int(item) for item in input().split()])

for i in range(Q):
    B = list_bc[i][0]
    C = list_bc[i][1]

    list_A = [C if item == B else item for item in list_A]
    print(sum(list_A))

Submission status: Time Limit Exceeded

Solution code:

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

result = a

l = []

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

  l.append(sum(result))

for i in l:
  print(i)

Submission status: Time Limit Exceeded

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

count_list = [0 for i in range(10**5 + 1)]
for a, i in Counter(A_list).items():
    count_list[a] = i

for b, c in BC_list:
    count_list[c] += count_list[b]
    count_list[b] = 0
    _sum = 0
    for i, a in enumerate(count_list):
        _sum += i * a
    print(_sum)

Submission status: Time Limit Exceeded

Solution code:
a = input()
sequence_integer = [str(idx) for idx in input().split()]
num = int(input())
data = []
for idx in range(num):
  data.append([str(idx) for idx in input().split()])
for idx, jdx in data:
  sequence_integer = [s.replace(idx, jdx) for s in sequence_integer] 
  result = sum([int(idx) for idx in sequence_integer])
  print(result)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

dct = {}
for k in range(N):
  n = nums[k]
  if n in dct:
    dct[n] += 1
  else:
    dct[n] = 1

for _ in range(Q):
  B, C =(int(s) for s in input().rstrip().split())
  if B in dct:
    if C in dct:
      dct[C] += dct.pop(B)
    else:
      dct[C] = dct.pop(B)

  sum = 0
  for k, v in dct.items():
    sum += k * v
  
  print(sum)

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

for i in range(Q):
    B = [BC[i][1] if l == BC[i][0] else l for l in A]
    print(sum(B))
    A = B

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

from  collections import Counter

dict_a = Counter(a)

def answer(q, dict_a):
  for i in range(q):
    dict_a[operation[i][1]] += dict_a[operation[i][0]]
    dict_a[operation[i][0]] = 0
    s = 0
    for (k, v) in dict_a.items():
      s += k*v
    print(s)

answer(q, dict_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 B in A:
        A[A.index(B)] = C

    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a_li = list(map(int,input().split()))
a_counter = Counter(a_li)
q = int(input())
for i in range(q):
    ans = 0
    a,b = map(int,input().split())
    dum = a_counter[a]
    a_counter[a] = 0
    a_counter[b] += dum
    for j in a_counter:
        ans += j * a_counter[j]
    print(ans)

Submission status: Time Limit Exceeded

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


for s in S:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy

tmp=input()
list=input().split(" ")
#list = map((lambda e: int(e)), list)
#array=numpy.zeros(len(list),dtype=">i4")
#for i in range(len(list)):
#  array[i]=int(list[i])

a=int(input())

for i in range(a):
  b=input().split(" ")
  list = [s.replace(b[0],b[1]) for s in list]
  list2=map((lambda e: int(e)),list)
  print(sum(list2))
  #if int(b[0]) in array:
  #  pos=numpy.where(array==int(b[0]))[0]
  #  for j in pos:
  #    array[j]=int(b[1])
  #  sum=numpy.sum(array)
  #  print(sum)
  #else:
  #  sum=numpy.sum(array)
  #  print(sum)

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())
  while b in li:
    o=li.index(b)
    li[o]=c
  print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
Q = int(input())
for i in range(Q):
    B, C = input().split()
    A = A.replace(B, C)
    print(sum([int(n) for n in A.split()]))

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):
    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=input()
q=int(input())
for _ in range(q):
    b,c = input().split(' ')
    a = a.replace(b,c)
    print(sum(map(int,a.split(' '))))

Submission status: Time Limit Exceeded

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

aDict = dict()
for aElement in A:
    if aElement not in aDict:
        aDict[aElement] = 0
    aDict[aElement] += 1

for _ in range(Q):
    b, c = map(int, input().split())
    if c not in aDict:
        aDict[c] = 0
    aDict[c] += aDict.pop(b, 0)
    print(sum([k * v for k, v in aDict.items()]))

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 b==j 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()))
mp = {}
for i in range(n):
    mp[a[i]] = a[i]
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 sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
q = ni()
c = [na() for _ in range(q)]

for i in c:
  a = [i[1] if i[0] == l else l for l in a]
  print(sum(a))