Problem:


Score : 400 points

Problem Statement

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

You will now successively do the following Q operations:

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

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

Constraints

All values in input are integers.

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

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

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

Output

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

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

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

Sample Input 1

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

Sample Output 1

11
12
16

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

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

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

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



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


Sample output:
11
12
16

Submission status: Time Limit Exceeded

Solution code:
def main():
    import collections
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    ans = sum(A_list)
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict as dd

def c(it):
  counter = dd(int)
  for x in it:
    counter[x] += 1
  return dict(counter)

n=int(input())
a=sorted((int(x) for x in input().split()))
q=int(input())

d=c(a)

for _ in range(q):
  b,c=(int(x) for x in input().split())
  num=d.pop(b,0)
  d.setdefault(c,0)
  d[c]+=num
  
  s=0
  for k,v in d.items():
    s+=k*v
  print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = str(input())
q = int(input())
s = []

for i in range(q):
    b,c = map(str,input().split())
    a = a.replace(b,c)
    s.append(sum(map(int,a.split())))

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    d = Counter(A)
    # print(d)

    for i in range(Q):
        ans = 0
        b, c = map(int, input().split())
        if b in d.keys():
            d[c] += d[b]
            d[b] = 0
        for k, v in d.items():
            ans += k * v
        # print(d)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

s = 0

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

    for a in A:
        s += a

    print(s)
    s = 0

Submission status: Time Limit Exceeded

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

n_dic = {}
for i in n_list:
    n_dic.setdefault(i, 0)
    n_dic[i] += 1

for i in range(q):
    b, c = map(int, input().split())
    if b in n_dic:
        n_dic.setdefault(c, 0)
        n_dic[c] += n_dic[b]
        del n_dic[b]

    result = 0
    for k in n_dic:
        result += k * n_dic[k]
    print(result)

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
A = map(int, 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:
n = int(input())
a = list(map(int, input().strip().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 aa in range(n):
        if(a[aa]==b[i]):
            a[aa]=c[i]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

n=int(input())
A = np.array(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(A,B,C)

#c = collections.Counter(A)
#values, counts = map(np.array,zip(*c.most_common()))
#print(values,counts)


for i in range(Q):
  A=np.where(A==B[i],C[i],A)
  print(np.sum(A))

Submission status: Time Limit Exceeded

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

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

for _ in range(q):
    b, c = map(int, input().split())
    if b in l:
        l[c] = l[c] + l[b] if l.get(c) else l[b]
        del l[b]
    sum = 0
    for k, v in l.items():
        sum += k * v
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = list(map(int, input().split()))
q = int(input())
res = []
for _ in range(q):
    x = list(map(int, input().split()))
    for i in range(n):
        if lst[i] == x[0]:
            lst[i] = x[1]
    res.append(sum(lst))

for i in res:
    print(i)

Submission status: Time Limit Exceeded

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

    q = int(input())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        count[c - 1] += count[b - 1] * (c / b)
        count[b - 1] = 0
        print(int(sum(count)))


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

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):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
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()))
#a, b = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
sA = sum(A)
q = int(input())
B = []
for _ in range(q):
    b, c = map(int, input().split())
    k = A.count(b)
    sA += k*(c-b)
    if k != 0:
      A = [c if i == b else i for i in A]
    print(sA)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

def func2(l, value):
    i = -1
    c = len(l)
    result = []
    while i < c:
        try:
            i = l.index(value, i + 1)
            result.append(i)
        except:
            return result
    return result

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

for i in range(q):
    b,c = map(int,input().split())
    if b not in a_li:
        print(sum(a_li))
        continue
    indexes = func2(a_li, b)
    for i in indexes:
        a_li[i] = c
    print(sum(a_li))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
N=int(sys.stdin.readline())
A=list(map(int,sys.stdin.readline().split()))
Q=int(sys.stdin.readline())
AA=set(A)
A=np.array(A)
b=[]
c=[]
for _ in range(Q):
  B,C=map(int,sys.stdin.readline().split())
  if B in AA:
    A[A==B]=C
    AA.add(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())

npdata = np.arange(10**5+1)

kosuu=np.zeros(10**5+1)
kosuumoto=kosuu
for i in range(N):
    a=A[i]
    kosuu[a]+=1

BC_list=[]

for i in range (Q):
    bc=list(map(int,input().split()))
    BC_list.append(bc)
S=0
for i in range(Q):
    b=BC_list[i][0]
    c=BC_list[i][1]
    kosuu[c]=kosuu[c]+kosuu[b]
    kosuu[b]=0
    S=sum(np.multiply(npdata,kosuu))
    print(int(S))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
seq = list(map(int,input().split()))
np_seq = np.array(seq)
Q = int(input())
for i in range(Q):
    bi, ci = map(int,input().split())
    np_seq = np.where(np_seq==bi,ci,np_seq)
    print(np_seq.sum())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    if BC[i][0] in a:
        
        a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))

Submission status: Time Limit Exceeded

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

a = set(A)
for i in a:
    dic[i] = A.count(i)

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

for j in range(Q):
  A = [L[j][1] if i == L[j][0] 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()))
Q=int(input())
c=sum(L)
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)
  c=c+a*(C-B)
  print(c)

Submission status: Time Limit Exceeded

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

b = []
c = []

A.sort()

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

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]-b[i]
            else:
                continue
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
cnt=0
while cnt < q:
    cnt+=1
    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())
listA=list(map(int, input().split()))
Q=int(input())

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;

for i in range(Q):
  for j in range(len(listA)):
    if listA[j]==listB[i][0]:
      listA[j]=listB[i][1]
  #print(listA)
  print(sum(listA))

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())
    b=A.count(B)
    for j in range(b):
        A.remove(B)
        A.append(C)
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

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):
    BTemp,CTemp = map(int, input().split())
    B.append(BTemp)
    C.append(CTemp)
for BTemp,CTemp in zip(B,C):
    ATemp = []
    try:
        while True:
            A[A.index(BTemp)] = CTemp
    except:
        pass
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
#入力
N = int(input())
Alist = list(map(int,input().split()))
Alist.sort()
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

for j in range(Q):
  num = Alist.count(Blist[j])
  if num == 0:
    pass
  else:
    firstindex = Alist.index(Blist[j])
    for l in range(num):
      Alist[firstindex+l] = Clist[j]
  print(sum(Alist))
  Alist.sort()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_lst = list(map(int, input().split()))
q = int(input())
# b_lst = []
# c_lst = []
cnt = []
ans = []
# ans = a_lst.copy()
for i in range(q):
    x, y = map(int, input().split())

    # b_lst.append(x)
    # c_lst.append(y)
    a_lst = [y if j == x else j for j in a_lst]
    ans.append(a_lst)

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

#     for j in range(len(a_lst)):
#         if a_lst[j] == b_lst[i]:
#             a_lst[j] = c_lst[i]
#     cnt.append(sum(a_lst))
#     print(cnt[i])

# print(cnt)
# for i in range()
# print(b_lst)

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

Submission status: Time Limit Exceeded

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


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


alist.sort()
sumA = sum(alist)
for _ in range(Q):
    b, c = map(int, input().split())
    idx_bl = bisect.bisect_left(alist, b)
    idx_br = bisect.bisect_right(alist, b)

    alist = alist[:idx_bl] + alist[idx_br:]
    idx_cl = bisect.bisect_left(alist, c)
    alist = alist[:idx_cl] + [c] * (idx_br - idx_bl) + alist[idx_cl:]

    sumA -= (idx_br - idx_bl) * (b - c)
    print(sumA)

Submission status: Time Limit Exceeded

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

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
answer = [0] * Q

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

    A = [(C if num == B else num) for num in A]
    answer[i] = sum(A)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
q = int(input())
S = []

for i in range(q):
    b, c = [int(i) for i in input().split()]
    a = np.where(a == b, c, a)
    S.append(np.sum(a))
    
for i in range(q):
    print(S[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = input().split()
p_int = [int(n) for n in p]
Q = int(input())
SUM = sum(p_int)
B2 = 0
for k in range(Q):
  B1, C1 = map(int, input().split())
  if B1 != B2:
    SUM += (C1 - B1) * p_int.count(B1)
    p_int = [C1 if i == B1 else i for i in p_int]
  B2 = B1
  print(SUM)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

newList = A
total = sum(newList)
for i in L:
  if i[0] in newList:
    n = newList.count(i[0])
    diff = i[1] - i[0]
    total = total + (diff * n)
    print(total)
    newList = [j for j in newList if j != i[0]]
    newList.extend([i[1]] * n)
  else:
    print(total)

Submission status: Time Limit Exceeded

Solution code:
a = input()
sequence_integer = [int(idx) for idx in input().split()]
for idx in range(int(input())):
  a, b = [int(idx) for idx in input().split()]
  while a in sequence_integer:
    sequence_integer[sequence_integer.index(a)] = b
  print(sum([int(idx) for idx in sequence_integer]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
X = {}

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

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

Q = int(input())
B = []
C = []

ANS = []
for i in range(Q):
  b, c = map(int, input().split())
  if b in X:
    if c in X:
      X[c] += X[b]
    else:
      X[c] = X[b]
    X[b] = 0
  ans = 0
  for i in X.keys():
    ans += i * X[i]
  ANS.append(ans)

for i in range(len(ANS)):
  print(ANS[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

str1 = input()
A = [int(str1.split(" ")[i]) for i in range(N)]
  
Q = int(input())

for j in range(Q):
  str2 = input()
  B, C = int(str2.split(" ")[0]), int(str2.split(" ")[1])
  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(str,input().split()))
Q=int(input())
B=[list(map(str,input().split())) for i in range(Q)]
A=','.join(A)
for i in B:
    A=A.replace(str(i[0]),str(i[1]))
    print(sum(list(map(int,str(A).split(',')))))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_kind = []
    A_num = []
    ans = 0
    for a in A:
        ans += a
        if not a in A_kind:
            A_kind.append(a)
            A_num.append(1)
        else:
            A_num[A_kind.index(a)] += 1
    
    for q in range(Q):
        b,c = map(int,input().split())
        if b in A_kind:
            n = A_kind.index(b)
            ans += (c - A_kind[n])*A_num[n]
            if c in A_kind:
                A_num[A_kind.index(c)] += A_num[n]
                del A_kind[n]
                del A_num[n]
            else:
                A_kind[n] = c
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
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(sum(a))

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import collections
import copy
import heapq
import itertools
import math
import numpy
import string
import sys
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()

for s in range(Q):
    BC = LI()
    for t in range(N):
        if A[t] == BC[0]:
            A[t] = BC[1]
    print(numpy.sum(A))

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

a =""
for i in A[:N-1]:
  a += i+" " 
a += A[N-1]

for i in BC:
  B = i[0]
  C = i[1]
  if B in a:
    a = a.replace(B,C)
  print(sum(list(map(int, a.split()))))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def resolve():
  N = int(input())
  countA = Counter([int(x) for x in input().split(" ")])
  Q = int(input())

  sum_A = 0
  for a in countA.items():
    sum_A += a[0]*a[1]

  for _ in range(Q):
    B, C = map(int, input().split(" "))
    if B in countA.keys():
      countA[C] += countA.pop(B)
      sum_A = 0
      for a in countA.items():
        sum_A += a[0]*a[1]
      print(sum_A)
    else:
      print(sum_A)


if __name__ == "__main__":
  resolve()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

A.sort()

dict_a = dict()

for a in A:
    if not (a in dict_a):
        dict_a[a] = 1
    else:
        dict_a[a] += 1


Q = int(input())

B = []
C = []

sums = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)
   

for b, c in zip(B, C):
    if (b in dict_a):
        num = dict_a[b]
    
        if not (c in dict_a):
            dict_a[c] = num
        else:
            dict_a[c] += num

        dict_a.pop(b)

    keys = dict_a.keys()
    vals = dict_a.values()

    sumval = 0
    for k, v in zip(keys, vals):
        sumval += k * v

    print(sumval)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n=int(input())
a=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())
  for j in range(n):
    if a[j]==b:
      a[j]=c
      count+=1
  sus.append(sus[-1]+(c-b)*count)
  print(sus[-1])
#print(sus)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=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)

for j in range(q):
  for k in range(len(a)):
    if a[k]==bl[j]:
      a[k]=cl[j]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(input().split())
q = int(input())
b = list(input().split() for i in range(q))

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

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

s = 0
S = []

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

    for a in A:
        s += a

    print(s)
    S.append(s)
    s = 0

Submission status: Time Limit Exceeded

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



for i in range(Q):
    
    a = [j for j, x in enumerate(A) if x == B[i]]
    for k in range(len(a)):
        A[a[k]] = C[i]
    # print(A)
    print(sum(A))

Submission status: Time Limit Exceeded

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

for Q_count in range(Q):
    B_count = A.count(B[Q_count])
    S = S + (C[Q_count] - B[Q_count]) * B_count
    print(S)
    A = [C[Q_count] if A[i] == B[Q_count] else A[i] for i in range(N)]

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=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())

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


def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

for i in range(q):
    if a.count(bc[i][0]) == 0 :
        print(sum(a))
    else :
        idx = func1(a,bc[i][0])
        for j in idx :
            a[j]=bc[i][1]
        print(sum(a))

Submission status: Time Limit Exceeded

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

for b, c in zip(B, C):
    A = [a if a != b else c for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
input()
a = list(map(int, input().split()))
b = int(input())
for i in range(b):
  c = list(map(int, input().split()))
  for d, n in enumerate(a):
    if n == c[0]:
      a[d] = c[1]
  f = 0
  for e in a:
    f += e
  print(f)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    a, b = map(int, input().split())
    B.append(a)
    C.append(b)
d = collections.Counter(A)
ans = 0
c = list(d.keys())
cs = list(d.values())
for i in range(Q):
    l=[j for j,x in enumerate(c) if x == B[i]]

    for k in l:
        c[k]=C[i]
    for m in range(len(c)):
        ans += c[m]*cs[m]
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

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

s = 0
result = ""

for r in range(0, q, 1):
  e = list(map(int, input().split()))
  for i in range(0, n, 1):
     if(p[i] == e[0]):
        p[i] = e[1]
     s = sum(p)
  result += str(s)+"\n"

print(result.rstrip("\n"))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re
n=int(input())
lst=list(map(str,input().split()))
q=int(input())
for i in range(q):
  s,t=map(str,input().split())
  lst=[re.sub(s, t, r) for r in lst]
  ans=[int(x) for x in lst]
  print(sum(ans))

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)
        for i in range(A.count(b)):
            A.remove(b)
            A.append(c)
resolve()

Submission status: Time Limit Exceeded

Solution code:
def main():
  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(0,n):
      if(a[i]==b):
        a[i]=c
    print(sum(a))
main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(map(int,input().split()))
Q=int(input())
l=[0]*Q
for i in range(Q):
    l[i]=list(map(int,input().split()))
    
for i in range(Q):    
    a = [(l[i][1] if s == l[i][0] else s) for s 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())
  
  for _ in range(q):
    b, c = map(int, input().split())
    a = [c if i == b else i for i in a]
    print(sum(a))
  
main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input().split()
a = [int(i) for i in a]
ans_list = []
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]
    ans_list.append(sum(a))
for i in ans_list:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
s = sum(arr)
for _ in range(k):
    v = 0
    a,b = map(int, input().split())
    for i in range(len(arr)):
      if arr[i] == a:
        arr[i] = b
        v += 1
    s = s - (a*v) + (b*v)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

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


@njit
def func(a,b,c,a_sum):
  count=np.count_nonzero(a==b)
  a[a==b]=c
  a_sum += count*(c-b)
  return a,a_sum
  
for i in range(q):
  b,c=map(int, input().split())
  a,a_sum=func(a,b,c,a_sum)
  print(a_sum)

Submission status: Time Limit Exceeded

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

operations = [

]

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for op in operations:
    target, goto = op

    for i in range(len(a)):
        if a[i] == target:
            a[i] = goto

    print(sum(a))

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())
oper = [list(map(int, input().split())) for _ in range(q)]
a.sort()
res = sum(a)

for i in range(q):
    b, c = oper[i]
    if b in a:
        bCount = a.count(b)
        idx = a.index(b)

        a = a[:idx] + a[idx+bCount:]
        a.extend([c]*bCount)
        dif = c-b

        res += dif*bCount

    print(res)

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

a =""
for i in range(N-1):
  a += A[i]+" " 
a += A[N-1]

for i in BC:
  B = i[0]
  C = i[1]
  if B in a:
    a = a.replace(B,C)
  print(sum(list(map(int, a.split()))))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
la = list(map(int, input().split()))
q = int(input())
sa = sum(la)
for i in range(q):
  b, c = map(int, input().split())
  cnt = la.count(b)
  sa += (c-b)*cnt
  print(sa)
  
  la += [c]*cnt

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
dic = {}
for i in range(N):
    if A[i] not in dic:
        dic[A[i]] = 1
    else:
        dic[A[i]] += 1;
for i in range(Q):
    B, C = map(int, input().split())
    if B in dic:
        #print(dic)
        j = dic[B]
        dic.pop(B)
        if C in dic:
            dic[C] += j
        else :
            dic[C] = j;
        #print(dic)
    
    summ = 0;
    for item in dic:
        summ += item*dic[item]
    print(summ)

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

dict = {}
for i in range(1, 10**5+1):
    dict[i] = 0
for i in a:
    dict[i] += 1

for i in range(q):
    b, c = map(int, input().split())
    dict[c] += dict[b]
    dict[b] = 0
    ans = 0
    for i, j in dict.items():
        ans += i*j
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
_=input();a=list(map(int,input().split(' ')));q=int(input())
for _ in range(q):
    b,c = map(int,input().split(' '))
    a = list(map(lambda i: c if i == b else i, a))
    print(sum(a))