Problem:


Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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


print()
for i in range(Q):
    look_0=BC[i][0]
    look_1=BC[i][1]
    if look_0 not in A:
        pass
    else:
        sub_array=(A==look_0)
        not_sub_array=np.logical_not(sub_array)
        sub_array=sub_array*look_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:
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])

A.sort()
S = []
for i in range(Q):
    for idx in range(N):
        if A[idx] == BC[i][0]:
            A[idx] = BC[i][1]
    S.append(sum(A))

    
for item in S:
    print(item)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = str(input())
Q = int(input())
sumLi = []
i = 0
j = 0
while i < Q:
    B, C = map(str, input().split())
    A = A.replace(B, C)
    nA = list(map(int, A.split()))
    sumLi.append(sum(nA))
    i += 1
while j < Q:
    print(sumLi[j])
    j += 1

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
ans = []
for i in range(q):
  b, c = map(int, input().split())
  if b in dic and c in dic:
    dic[c] += dic[b]
    dic.pop(b)
  elif b in dic and c not in dic:
    dic[c] = dic[b]
    dic.pop(b)
  result = 0
  for k, v in dic.items():
    result += k * v
  ans.append(result)
for i in range(q):
  print(ans[i])

Submission status: Time Limit Exceeded

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

for i in range(q):
    for k in range(n):
        if int(b_c[i][0]) == a[k]:
            a[k] = int(b_c[i][1])
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def sum(n, a):
  ans = 0
  n = int(n)
  for num in range(n):
    ans += int(a[num])
  print(ans)

n = input()
a = [e for e in input().split()]
q = input()
q = int(q)

b = np.zeros((q, 2), dtype=int)

for num in range(q):
  b[num][0],b[num][1] = input().split()
  # b1,b2 = input().split()


for num in range(q):
  a = [s.replace(str(b[num][0]),str(b[num][1])) for s in a]
  sum(n,a)
  # print(int(a[0]) + int(a[1]) + int(a[2]) + int(a[3]) )

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

    idx_list = [i for i, x in enumerate(A) if x == B]
    for idx in idx_list:
        A[idx]=C
    
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
A_n =list(map(int,input().split()))
Q =int(input())
B_n ,C_n=[],[]
for i in range(Q):
    B,C =map(int,input().split())
    B_n.append(B)
    C_n.append(C)

sum0 =sum(A_n)
for i in range(Q):
    count =A_n.count(B_n[i])
    sum0 += (C_n[i]-B_n[i])*count
    A_n  =list(filter(lambda x: x != B_n[i] ,A_n))
    A_n.extend([C_n[i] for j in range(count)])
    print(sum0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

q = int(input())

#初期化

a_dic ={}


for i in a:
    if i not in a_dic:
        a_dic[i] = i
    else:
        a_dic[i] += i

#a_sorted = sorted(a)
for i in range(q):
    ans = 0
    b,c = map(int, input().split())
    #print(a_dic)
    
    if b in a_dic:
        if c not in a_dic:
            a_dic[c] = a_dic[b]/b * c 

        else:
            tmp_c = a_dic[c]
            tmp_c += a_dic[b]/b*c
            a_dic[c] = tmp_c
        a_dic.pop(b) 
    print(int(sum(a_dic.values())))

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    A = A.replace(str(lst[i][0]),str(lst[i][1]))
    ans =A.split(',')
    ans = list(map(int, A.split(',')))
    print(sum(ans))
    A = str(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 = list(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=list(map(int,input().split()))
Q=int(input())
Z=[]
B=[0]*Q
C=[0]*Q
for i in range(Q):
  Z.append(list(map(int,input().split())))
for i in range(Q):
  B[i]=Z[i][0]
  C[i]=Z[i][1]
#for j in range(1,Q+1):
for b in range(Q):
  for k,l in enumerate(A):
    if l==B[b]:
      A[k]=C[b]
  print(sum(A))
#print(C)

Submission status: Time Limit Exceeded

Solution code:
# from collections import defaultdict
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
s = sum(A)
store = dict()
for q in range(Q):
    count = 0
    B,C = [int(x) for x in input().split()]
    if B not in store:
        store[B]=A.count(B)
    if C not in store:
        store[C]=A.count(C)
    s = s - store[B]*B + store[B]*C
    store[C] += store[B]
    store[B] = 0
    print(s)
    # print(store)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
D=Counter(A)
E=list(D.keys())
F=list(D.values())
G=[0]*len(E)
for i in range(0,len(D)):
    G[i]=E[i]*F[i]
Q=int(input())
P = [list(map(int,stdin.readline().split())) for i in range(Q)]
for i in range(0,Q):
    if E.count(P[i][0])>0:
        j=E.index(P[i][0])
        if E.count(P[i][1])>0:
            k=E.index(P[i][1])
            G[k]=G[k]+E[k]*F[j]
            F[k]=F[k]+F[j]
            E.pop(j)
            F.pop(j)
            G.pop(j)
        else:
            G[j]=P[i][1]*F[j]
            E[j]=P[i][1]
    print(sum(G))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
  D[a] += 1
Q = int(input())
B = []
C = []
for _ in range(Q):
  l = list(map(int, input().split()))
  B.append(l[0])
  C.append(l[1])

for i in range(Q):
  k = D[B[i]]
  D.pop(B[i])
  D[C[i]] += k
  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())
b_c = [list(map(int, input().split())) for i in range(q)]

ans = 0

for i in range(q):
    before = b_c[i][0]
    after = b_c[i][1]
    while before in a:
        a[a.index(before)] = after
    ans = sum(a)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(input().split())
Q=int(input())
anlist=[]
BC=[input().split() for i in range(Q)]
for i in range(Q):
  if BC[i][0] in A:
    while True:
      A[A.index(BC[i][0])]=BC[i][1]
      if BC[i][0] not in A:
        break
  A=list(map(int,A))
  print(sum(A))
  A=list(map(str,A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ais = list(map(str, input().split()))
q = int(input())
bcs = [list(map(int, input().split())) for i in range(q)]
for b, c in bcs:
  new = "-".join(ais)
  change = new.replace(str(b),str(c))
  ais = change.split('-')
  sum = 0
  for i in ais:
    sum+=int(i)
  print(sum)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
import numpy as np
a2=np.array(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  idx=np.where(a2[:,0]==b)
  idx=np.append(idx,np.where(a2[:,0]==c))
  s=0
  for jj in idx:
    s+=a2[jj,1]
  a2=np.delete(a2,idx,axis=0)
  a2=np.append(a2,[[c,s]],axis=0)
  print(sum(a2[:,0]*a2[:,1]))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    b, c = input().split()
    atmp = " ".join(a)
    atmp = atmp.replace(b, c)
    a = atmp.split(" ")
    anew = [int(x) for x in a]
    ans.append(sum(anew))
    
print(*ans, sep='\n')

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())
    ans =[ans[j] if ans[j] != b else c for j in range(n)]        
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
Q=int(input())
Z=[]
B=[0]*Q
C=[0]*Q
for i in range(Q):
  Z.append(list(map(int,input().split())))
for i in range(Q):
  B[i]=Z[i][0]
  C[i]=Z[i][1]
for b in range(Q):
  for k,l in enumerate(A):
    if l==B[b]:
      A[k]=C[b]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

import collections

buk = [0] * (10**5 + 1)
for e in a:
  buk[e] += 1


# dprint(buk)

for b, c in bc:
  sum = 0
  for k in range(10 ** 5 + 1):
    if buk[k] == 0:
      continue
    if k == b:
      # dprint(b, c)
      buk[c] += buk[b]
      buk[b] = 0
      # dprint(buk)
      sum += c * buk[c]
    else:
      if k != c:
        sum += k * buk[k]
    # dprint('sum', '=', sum)
  print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if b[i] in A:
        A = [s.replace(b[i],c[i]) for s in A]
    print(sum(int(i) for i in A))

Submission status: Time Limit Exceeded

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

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

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


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

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())
    change_num=A.count(b)
    for i in range(change_num):
        A[A.index(b)]=c
    base+=change_num*(c-b)
    print(base)

Submission status: Time Limit Exceeded

Solution code:
def main() -> None:
    N = int(input())
    A = [int(_) for _ in input().split(' ')]
    Q = int(input())

    for i in range(Q):
        _1, _2 = map(int, input().split(' '))
        for j in range(N):
            if A[j] == _1:
                A[j] = _2

        print(sum(A))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    x=0
    b,c=map(int,input().split())
    ans[c]+=ans[b]
    ans[b]=0
    for j in range(10**5+1):
        if ans[j]==0:
            continue
        x+=(ans[j])*j
    print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l={}
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    if i in l:
        l[i]+=1
    else:
        l[i]=1
for (f,t) in move:
    if f in l:
        if t in l:
            l[t]+=l[f]
        else:
            l[t]=l[f]
    l[f]=0
    sum = 0
    for k,v in l.items():
        sum += k*v
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline


def sum_d(d):
    total = 0
    for k, v in d.items():
        total += k * v
    return total


N = int(readline())
A = map(int, readline().split())
Q = int(readline())
d = Counter(A)

answer = [0] * Q

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

    if B in d:
        if C in d:
            d[C] = d[C] + d.pop(B)
        else:
            d[C] = d.pop(B)

    answer[i] = sum_d(d)

print(*answer, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
la = list(map(int, input().split()))
q = int(input())
sa = set(la)
ls = list(sa)
s = sum(la)
d = dict()
for i in range(len(ls)):
  k = ls[i]
  d[k] = la.count(k)
for j in range(q):
  count = 0
  b, c = map(int, input().split())
  if b in d:
    count = d[b]
    if c in d:
      d[c] += count
    else:
      d[c] = count
  s -= count*(b-c)
  print(s)

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 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())
result = []
for i in range(q):
    b = list(map(int, input().split()))
    for k in range(n):
        if a[k] == b[0]:
            a[k] = b[1]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
BC = [list(map(int,input().split())) for _ in range(q)]
A = np.array(A)
 
def main(A,BC):
    for bc in BC:
      A = np.where(A==bc[0],bc[1],A)
      print(sum(A))
        
if __name__ == '__main__':
    main(A,BC)

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(map(int, tmp.split(',')))
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b=[]
c=[]
aa=[]
sum=0
for i in range(n):
    sum+= a[i]
for i in range(q):
    bb,cc = map(int,input().split())
    b.append(bb)
    c.append(cc)
a.sort()

for i in range(q):
    l= bisect.bisect_left(a,b[i])
    h= bisect.bisect_right(a,b[i])
    ll=bisect.bisect_left(a,c[i])
    hh= bisect.bisect_right(a,c[i])
    if b[i]>c[i]:
        a[ll+h-l:h],a[ll:ll+h-l] = a[ll:l],[c[i]]*(h-l)
    else:
        a[l+hh-h:hh],a[l:l+hh-h] = [c[i]]*(h-l),a[h:hh]
    sum=sum + (c[i]-b[i])*(h-l)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
      
      
n = int(input())
a = [int(i) for i in input().split(' ')]
q = int(input())
b = []
c = []
for _ in range(q):
  temp = input().split(' ')
  i = int(temp[0])
  j = int(temp[1])
  k = my_index(a, i)
  while True:
    if k is False:
      break
    a[k] = j
    k = my_index(a, i)
  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())
Gokei=[]

for _ in range(Q):
    B,C=map(int,input().split())
    i=0
    while B in A:
        A.remove(B)
        i+=1
    A.extend([C]*i)
    Gokei.append(sum(A))

for i in range(Q):
    print(Gokei[i])
    i+=1

Submission status: Time Limit Exceeded

Solution code:
x=int(input())
l=list(map(int,input().split()))
for i in range(int(input())):
	s=0
	a,b=map(int,input().split())
	for i in range(len(l)):
		if l[i]==a:
			
			l[i]=b
			

	print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(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(Q):
    for k in range(N):
        if A[k] == BC[j][0]:
            A[k] = BC[j][1]
    total = sum(A)
    print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [list(map(int,input().split())) for b in range(Q)]

for b in B:
    s = 0
    for n in range(0,N):
        if A[n] == b[0]:
            A[n] = b[1]

    for a in A:
        s += a

    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input().rstrip())

line = input().rstrip().split(' ')

a = []
for i in range(n):
    a.append(int(line[i]))

a.sort()

q = int(input().rstrip())

b = []
c = []

for i in range(q):
    line = input().rstrip().split(' ')
    b = int(line[0])
    c = int(line[1])

    for j in range(len(a)):
        if a[j] == b:
            a[j] = c

    ans = 0
    for j in range(len(a)):
        ans += a[j]

    print(ans)

Submission status: Time Limit Exceeded

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

for b in B:
    A = A.replace(b[0], b[1])
    print(sum([int(a) for a in A.split(' ')]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
Q = int(input())
S = []
s = sum(map((lambda x: int(x)), A))


for i in range(Q):
    B, C = map(str,input().split())
    if B in A:
        string = ",".join(A)
        string_new = string.replace(B,C)
        A =  string_new.split(",")
        s = sum(map((lambda x: int(x)), A))
        S.append(s)


    else:
        S.append(s)



for s in S:
    print(s)

Submission status: Time Limit Exceeded

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

n = int(readline())
a = list(map(int, readline().split()))
q = int(readline())
L = []
for i in range(q):
  L.append(list(map(int, readline().split())))

for b, c in L:
  for i, v in enumerate(tuple(a)):
    if v == 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())
dic = dict()

for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for i in range(Q):
    b, c = list(map(int, input().split()))
    if b in dic:
        if c in dic:    
            dic[c] += dic.pop(b)
        else:
            dic[c] = dic.pop(b)
    ans = 0
    for num in dic:
        ans += num * dic[num]
    print(ans)

Submission status: Time Limit Exceeded

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

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()
    replace_a = [i.replace(b, c) if i == b else i for i in a]
    print(sum(map(int, replace_a)))
    a = replace_a

Submission status: Time Limit Exceeded

Solution code:
def getCalc(NUM):
    sum = 0
    for i in range(len(NUM)):
        sum += i * NUM[i]

    return sum


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)

max_num = max(max(A), max(B), max(C))

NUM = [0]*(max_num + 1)

for i in range(max_num+1):
    NUM[i] = A.count(i)

for i in range(Q):
    NUM[B[i]], NUM[C[i]] = 0, NUM[B[i]] + NUM[C[i]]

    print(getCalc(NUM))

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):
    b,c = list(map(int,input().split()))
    f = False
    for j in range(len(a)):
        if a[j] != b and f == True:
            break
        if a[j] == b:
            a[j] = c
            a_sum += c - b
            f = True           
    
    res.append(a_sum)
        
for i in res:
    
    print(i)

Submission status: Time Limit Exceeded

Solution code:
if __name__=='__main__':
    N = int(input())
    A = input().split(" ")
    A = [int(s) for s in A]
    Q=int(input())
    str_list=[]
    S=[]
    for j in range(Q):
        str_list=input().split(" ")
        if int(str_list[0]) in A:
            #A=[s.replace(int(str_list[j][0]),int(str_list[j][1])) for s in A]
            A=[(int(str_list[1])) if i == int(str_list[0]) else i for i in A]

        print(sum(A))

Submission status: Time Limit Exceeded

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

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    time = 0
    A = sorted(A)
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
            time += 1
        elif A[k] > B[i]:
            break
        
    sum += (C[i]-B[i])*time

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from bisect import bisect_left
n=int(input())
a=sorted(list(map(int,input().split())))

sus=deque([sum(a)])
q=int(input())
for i in range(q):
  count=0
  b,c=map(int,input().split())
  idx=bisect_left(a,b)
  while True:
    if idx>=n:break
    if a[idx]==b:
      a[idx]=c
      count+=1
      idx+=1
    else:
      break
  a=sorted(a)
  sus.append(sus[-1]+(c-b)*count)
  print(sus[-1])

Submission status: Time Limit Exceeded

Solution code:
n, *bc = map(int, open(0).read().split())
a = [bc.pop(0) for _ in range(n)]
q = bc.pop(0)

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        if c in d:
            d[c] += d.pop(b)
        else:
            d[c] = d.pop(b)
    ret = sum([k * v for k, v in d.items()])
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n = int(input()) 
A = list(map(str, input().split()))
q = int(input()) 
for i in range(q):
    b, c = map(str, input().split())
    A = [int(str(a).replace(b, c)) for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n =int(input())
a = list(map(int,input().split()))
S = sum(a)
A =set(a)
q =int(input())
for _ in range (q):
  b, q = map(int,input().split())
  if b in A:
    S += (a.count(b))*(q-b)
    a = [q if _ == b else _ for _ in a]
    A =set(a)
  print(S)

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]
    del(dict_n[b])
    
    sum = 0
    for key in dict_n:
        sum += key * dict_n[key]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
z=input()
a=z.split()
A=[]
B=[]
C=[]
flag=[]
replace=0
for n in range(100001):
    flag.append(1)
for i in range(N):
    A.append(int(a[i]))
Q=int(input())
for j in range(Q):
    d=input()
    D=d.split()
    B.append(int(D[0]))
    C.append(int(D[1]))
A.sort()
for j in range(Q):
    if flag[B[j]]==1:
        k=0
        replace=0
        while A[k]<=B[j]:
            if A[k]==B[j]:
               A[k]=C[j]
               replace+=1
            k+=1
            if k==N:
                break
        if replace>0:
            flag[B[j]]=0
            flag[C[j]]=1
    A.sort()
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
an_list = list(map(int, input().split()))
an_list = sorted(an_list)
max_num = max(an_list)
min_num = min(an_list)

q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if min_num <= b and b <= max_num:
        index_l = bisect.bisect_left(an_list, b)
#         print(an_list[index_l] == b)
        if an_list[index_l] == b:
            index_r = bisect.bisect_right(an_list, b)
            an_list[index_l:index_r] = [c] * (len(an_list[index_l:index_r]))
            an_list = sorted(an_list)
            max_num = max(an_list)
            min_num = min(an_list)
    print(sum(an_list))
#     print(an_list)

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:
        ans =[ans[j] if ans[j] != b else c for j in range(n)]        
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = list()
sum_a = 0
output = 0
cnt = 0
for i in range(q):
  bc.append(list(map(int,input().split())))
a.sort()
for i in range(q):
  flag = False
  try:
    nn = a.index(bc[i][0])
    for j in range(nn-1, len(a)):
      if a[j] == bc[i][0]:
        a[j] = bc[i][1]
        flag = True
      else:
        if flag == True:
          break
  except ValueError:
    pass
  print(sum(a))
  a.sort()

Submission status: Time Limit Exceeded

Solution code:
#数字の個数
N = int(input())

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

#操作の回数
Q = int(input())

B = []
C = []


set = (set(A)) #存在する数字のリスト

#数値nの個数を追加
nums = {}
for n in list(set):
    nums[n] = A.count(n)

S = sum(A)

for _ in range(Q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)


for i in range(Q):
    if B[i] in nums:
        Bnum = nums[B[i]]
        S += (C[i]-B[i])*Bnum

        if C[i] in nums:
            nums[C[i]] += Bnum

        else:
            nums[C[i]] = Bnum

        nums[B[i]] = 0
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  BC = list(map(int, input().split()))
  B_i = BC[0]
  C_i = BC[1]
  B_ind = []
  for j in range(len(A)):
    if A[j] == B_i:
      B_ind.append(j)
    if A[j] > B_i:
      break
  for k in B_ind:
    A[k] = C_i
  A = sorted(A)
  print(sum(A))

Submission status: Time Limit Exceeded

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

adic=dict()

for ai in a:
    if ai in adic:
        adic[ai]+=1
    else:
        adic[ai]=1
        
#for i in range(1,max(a)+1):
for i in range(1,10**5+1):
    if not i in adic:
        adic[i]=0
        
        
q=int(input())
b=[0]*(q+1)
c=[0]*(q+1)
for i in range(q):
    bi,ci=map(int,input().split())
    b[i]=bi
    c[i]=ci
    
#print(adic)    
for i in range(q):
    adic[c[i]]=adic[c[i]]+adic[b[i]]
    adic[b[i]]=0
#    print(i,adic)
    asum=0
    for i in range(1,10**5+1):
#        asum+=a[i]*adic[a[i]]
        asum+=i*adic[i]
    print(asum)

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
res = sum(As)
ex = set(As)
dis = {i:As.count(i) for i in set(As)}
Q=int(input().rstrip('\n'))
Qs=[]
for i in range(Q):
  Qs.append(tuple(int(x) for x in input().rstrip('\n').split()))
for B,C in Qs:
  if B in ex:
    res += dis[B]*(C-B)
    if C in ex:
      dis[C] += dis[B]
    else:
      dis[C] = dis[B]
      ex.add(C)
    dis[B]=0
    ex.remove(B)
  print(res)