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())
a = input()
q = int(input())
bc = [input().split() for _ in range(q)]

for b, c in bc:
  a = a.replace(b, c, n)
  print(sum(map(int, a.split())))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  for j in range(lenA):
    if A[j] == B[i]:
      A[j] = C[i]
  for j in range(lenA):
    cnt += A[j]
  print(cnt)
  cnt = 0

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()]
dis = {i:As.count(i) for i in set(As)}
Q=int(input().rstrip('\n'))
res = sum(i*x for i,x in dis.items())
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in dis:
    n = dis[B]
    if C in dis:
      dis[C] += dis[B]
      del dis[B]
    else:
      dis[C] = dis[B]
      del dis[B]
    res += n*(C-B)
  print(res)

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

Submission status: Time Limit Exceeded

Solution code:
B = list()
C = list()
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    bb, cc= map(int, input().split())
    B.append(bb)
    C.append(cc)
   

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_number = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_number:
        numbers[num] += 1

    Q = int(input())

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers.pop(a)
        print(sum([v*k for (k, v) in numbers.items()]))
    
main()

Submission status: Time Limit Exceeded

Solution code:

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

result = a
sum = sum(result)
l = []
for i in range(q):
  b, c = map(int, input().split())

  sum += result.count(b) * (c-b)
  for j in range(len(result)):
    if result[j] == b:
      result[j] = c
  
  print(sum)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
  N = int(input())
  A = [int(x) for x in input().split()]
  Q = int(input())
  for i in range(Q):
    B, C = [int(x) for x in input().split()]
    A = [x if x!=B else C for x in A]
    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

N = int(input())

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

Q = int(input())

for i in range(0, Q):
    BC = list(map(int, input().split()))
    if not BC[0] in AAA:
        print(sum(AAA))
        pass
    else:
        AAA = [BC[1] if m == BC[0] else m for m in AAA]
        print(sum(AAA))

Submission status: Time Limit Exceeded

Solution code:
def Replacing():

    Num = int(input())

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

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        value_A = [value_B[1] if j == value_B[0] else j for j in value_A]

        print(sum(value_A))

if __name__ == '__main__':
    Replacing()

Submission status: Time Limit Exceeded

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

import collections
collect = collections.Counter(a)

for i in bc:
    if i[1] in collect:
        collect[i[1]] += collect.pop(i[0],0)
    else:
        if i[0] in collect:
            collect[i[1]] = collect.pop(i[0],0)
    ans=0
    for k,v in collect.items():
        ans += (k * v)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

def main():
    input = sys.stdin.readline
    n = int(input())
    a = list(map(str, input().strip().split()))
    q = int(input())
    bc = [map(str, input().split()) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]
    ii = 0

    for i in b:
        a = [n.replace(i,c[ii]) for n in a]
        print(sum(list(map(int, a))))
        ii += 1
        

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# 実行環境(0:vscode, 1:atcoder)
MODE = 1

# 入力の次元
DIMENSION = 2

# int型に変換
INT_TYPE = 1

if not (MODE):
    with open("input.txt", "r") as f:
        s = f.read()
else:
    s = sys.stdin.read()

if DIMENSION == 0:
    if INT_TYPE == 1:
        s = int(s)
elif DIMENSION == 1:
    if INT_TYPE == 0:
        s = s.split()
    elif INT_TYPE == 1:
        s = [int(x) for x in s.split()]
elif DIMENSION == 2:
    if INT_TYPE == 0:
        s = [x.split() for x in s.splitlines()]
    elif INT_TYPE == 1:
        s = [[int(y) for y in x.split()] for x in s.splitlines()]

N = s[0][0]
A = np.array(s[1])
Q = s[2][0]
BC = np.array(s[3:])
B = BC[:, 0]
C = BC[:, 1]

A_sum = np.sum(A)

D = np.zeros(100001).astype(np.uint32)
D = np.array([np.count_nonzero(A == i) for i in range(100001)])
for i in range(Q):
    b = B[i]
    c = C[i]
    A_sum += D[b] * (c - b)
    print(A_sum)
    D[c] += D[b]
    D[b] = 0

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):
    a = np.where(a == b[i], c[i],a)
    print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[]
a,*b=map(int,input().split())
l.append(a)
for i in range(len(b)):
    l.append(b[i])
q=int(input())
b=[]
c=[]
for i in range(q):
    y,z=map(int,input().split())
    b.append(y)
    c.append(z)
for j in range(len(b)):
    for n,i in enumerate(l):
        if b[j]==i:
            l[n]=c[j]
    print(sum(l))

Submission status: Time Limit Exceeded

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

ans = sum(a)
A = ''
for i in range(n):
    A += ' ' + str(a[i])

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

for i in range(q):
    s = A.replace(str(B[i]), str(C[i]))
    cnt = A.count(str(B[i]))
    ans += (C[i]-B[i])*cnt
    print(ans)
    A = 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(str(sum(map(int,a.split()))))

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
  for j in range(N):
    if A[j] == pairs[i][0]:
      A[j] = pairs[i][1]
#  print(pairs[i][0])
#  A = A.replace(pairs[i][0], pairs[i][1])
#  print(A)
  ret = sum(list(map(int,A)))
  print(ret)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

a = Counter(a_list)
for i in range(Q):
    b, c = map(int, input().split())
    a[c] += a[b]
    a[b] = 0
    print(sum([x*y for x, y in a.items()]))
    #print(sum(a.elements()))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    A=[int(x) for x in input().split()]
    Q=int(input())
    BC=[list(map(int, input().split())) for _ in range(Q)]
    for bc in BC:
        A=[bc[1] if x==bc[0] else x for x in A]
        print(sum(A))
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=map(int,input().split())
Q=int(input())
numbers=[]
for _ in range(Q):
    B,C=map(int,input().split())
    numbers.append([B,C])
answers=[0 for _ in range(100001)]
for a in A:
    answers[a]+=a
for number in numbers:
    suuji=answers[number[0]]//number[0]
    answers[number[0]]=0
    answers[number[1]]+=suuji*number[1]
    print(sum(answers))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
#print(a)
q = int(input())
#print(q)
for i in range(q):
  b,c = list(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())
num = list(map(int,input().split()))
q = int(input())
ans=0
for i in range(q):
    b,c = map(int,input().split())
    for j in range(n):
        if num[j] == b:
            num[j] = c 
        ans += num[j]
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-06-21 21:56:58 +0900
# LastModified: 2020-06-21 22:06:42 +0900
#


import copy

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
Q=int(input())
D=sum(A)
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
    if B<=C:
      D=D+(C-B)*count
      print(D)
    if B>C:
      D=D+(C-B)*count
      print(D)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from numba import jit
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
for k, v in Counter(a).items():
    arr[k] = v
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit('void(i8, i8[:], i8[:])')
def solve(n, arr, bc):
    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))


solve(n, arr, bc)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
Q = int(input())
S = []
c = 0

for i in range(Q):
    B, C = map(str,input().split())
    if B in A:
        c = A.count(B)
        string = ",".join(A)
        string_new = string.replace(B, C,c)
        A =  string_new.split(",")

    S.append(sum(map((lambda x: int(x)), 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())

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()
q = int(input())
bc = [input().split() for _ in range(q)]

for b, c in bc:
  a = a.replace(b, c, n)
  print(sum(list(map(int, a.split()))))

Submission status: Time Limit Exceeded

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


for _ in range(Q):
  b,c=map(int,input().split())
  time = A.count(b)
  if(time != 0):
    place = A.index(b)
    for i in range(time):
      A[place + i] = c
    A.sort()
  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())
    if B in A:
        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(str, input().split()))
Q = int(input())
for i in range(1,Q+1):
  x, y = map(str, input().split())
  A = [s.replace(x, y) for s in A]
  A_int = map((lambda x: int(x)), A)
  print(sum(A_int))

Submission status: Time Limit Exceeded

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

dic = {a:0 for a in A}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  if C not in dic.keys():
    dic[C] = 0
  if B in dic.keys():
    dic[C] += dic[B]
    dic[B] = 0
  
  ans = 0
  for k, v in dic.items():
    ans += k*v
  
  print(ans)

Submission status: Time Limit Exceeded

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

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

for line in readlines():
    B, C = map(int, line.split())

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

Submission status: Time Limit Exceeded

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

for elem in a:
    a_list[elem][1] += 1

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N=int(input())
A=list(map(int,input().split()))
A=list(sorted(A))
Q=int(input())
B=[0]*Q
C=[0]*Q
P = [list(map(int,stdin.readline().split())) for i in range(Q)]
for i in range(0,Q):
    A=[P[i][1] if j==P[i][0] 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()))
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  for j in range(len(a)):
    if a[j] == b:
      a[j] = c
  print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    B[i], C[i] = map(str, input().split())

for i in range(Q):
    A = list(map(str, A))
    if B[i] in A:
        A = [a.replace(B[i], C[i]) for a in A]
    
    A = list(map(int, A))
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def fun(a):
    x,y = map(int, input().split())
    a = [y if b == x else b for b in a]
    print(sum(a))
    return a

import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
q = int(input())
# c = [fun(a) for i in range(int(input())]
xs = [list(map(int, input().split())) for _ in range(q)]
for i in range(q):
    # x,y = map(int, input().split())
    a = [xs[i][1] if b == xs[i][0] else b for b in a]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]

    Aset = set(A)
    
    others = Aset - set([b for b ,_ in BC]) - set([c for _ ,c in BC])

    Adict = dict()
    for a in A:
        Adict[a] = Adict.get(a, 0) + 1
    
    sum_others = 0
    for o in others:
        v = Adict.pop(o)
        sum_others += o*v
    
    for b, c in BC:
        if b in Adict:
            tmp = Adict[b]
            Adict[c] = Adict.get(c, 0) + tmp
            Adict.pop(b)
        
        goukei = sum([k*v for k, v in Adict.items()]) + sum_others
        
        print(goukei)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:


import collections

n = int(input())
a = list(map(int, input().split(" ")))
q = int(input())
have_map = collections.Counter(a)
for i in range(q):
    x, y = map(int, input().split(" "))
    have_map[y] = have_map.get(x, 0) + have_map.get(y, 0)
    have_map.pop(x, 0)

    cost = 0
    for k,v in have_map.items():
        cost += k * v
    print(cost)

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):
	ind = []
	ind  = [j for j, x in enumerate(a) if x == b[i]]
	l = len(ind)
	if l > 0:
		for k in range(l):
			a[ind[k]] = c[i]
		print(sum(a))
	else:
		print(sum(a))
	"""
	for j in range(n):
		if a[j] ==b[i]:
			a[j] = c[i]
	"""

Submission status: Time Limit Exceeded

Solution code:
N = input()
bucket = {}
for i in map(int, input().split()):
    if i in bucket:
        bucket[i] += i
    else:
        bucket[i] = i

Q = int(input())

s = []
for i in range(Q):
    b, c = map(int, input().split())
    num = bucket.pop(b, 0)
    now = bucket.get(c, 0)
    bucket[c]  = now + (num // b * c)
    s.append(sum(bucket.values()))
    
for ans in s:
    print(ans)

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())
    A_replace=[C if A[j]==B else A[j] for j in range(N)]
    A=A_replace
    print(sum(A_replace))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
#数を取得
count = int(input())
seq = list(map(int, input().split()))
repeat = int(input())
#置換する
for i in range(0, repeat):
  replace = list(map(int, input().split()))
  for i, value in enumerate(seq):
    if value == replace[0]:
      seq[i] = replace[1]
  print(sum(seq))

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 = list(map(int, input().split()))
    A = [(C if a == B else a) for a in A]
    S.append(sum(A))
for s in S:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = [int(i) for i in input().split()]
q = int(input())
bb = []
cc = []
for i in range(q):
    btmp, ctmp = [int(j) for j in input().split()]
    bb.append(btmp)
    cc.append(ctmp)

def temp(b,c,i):
    if i==b:
        return c
    else:
        return i

atol = sum(a)
for b,c in zip(bb,cc):
    
    count = len([ i for i in a  if i == b])
    a = [ temp(b,c,i) for i in a ]
    atol +=  (c - b) * count
    print(atol)

Submission status: Time Limit Exceeded

Solution code:
import collections

def A_sum(A):
  s = 0
  for i in A.items():
    s+=i[0]*i[1]
  print(s)

def A_rep(A,f,t):
  f_num = A.get(f)
  if f_num != None:
      A[t]=A.get(t,0) + f_num
      A.pop(f)

N = int(input())
A = collections.Counter(map(int,input().split()))
Q = int(input())
for i in range(Q):
  b,c = map(int,input().split())
  A_rep(A,b,c)
  A_sum(A)

Submission status: Time Limit Exceeded

Solution code:
import collections

_ = input()
a = collections.Counter([x for x in input().rstrip().split()])
_ = input()


try:
    while True:
        l = input().rstrip()
        b, c = l.split(' ')
        a[str(c)] += a[str(b)]
        a[str(b)] = 0

        ans = 0
        for i, n in a.items():
            ans += int(i) * n
        print(ans)

except EOFError:
    pass

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = map(str, input().split())
a = " ".join(a)

m = int(input())
for i in range(m):
               b, c = map(str,input().split())
               #ans = sum([int(i) for i in a])
               a=a.replace(b,c).replace(" ","+")
               
               print(eval(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

n = int(input())
d = defaultdict(int)
a = list(map(int, input().split()))
for key in range(n):
  d[key] = a[key]
  
q = int(input())

for i in range(q):
  b,c = map(int, input().split())
  keys = [k for k, v in d.items() if v == b]
  for j in keys:
    d[j] = c
  print(sum(d.values()))

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

m = max(max(A), max(B), max(C))+1
res = np.arange(m,dtype=int)
newA = np.zeros((m),dtype=int)


for a in A:
    newA[a] += 1

for i in range(Q):
    newA[C[i]] += newA[B[i]]
    newA[B[i]] = 0
    print(np.dot(res,newA))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#d
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
for i in range(q):
    if b[i] in a:
        for j in [z for z, x in enumerate(a) if x == 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())
ans = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    for j in range(N):
        if A[j] == b:
            A[j] = c

    ans += [sum(A)]
[print(a) for a in ans]

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
def check(a,num,b):
  sum = 0
  for i in range(num):
    if a[i] == b[0]:
      sum += int(b[1])
      a[i] = b[1]
    else:
      sum += int(a[i])
  return a,sum
q = int(input())
for i in range(q):
  b = input().split()
  a,sum = check(a,n,b)
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 

n = int(input())
a = list(map(int, input().split()))
co = Counter(a)
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if b in co.keys():
        co[c] += co[b]
        co[b] = 0
    ans = 0
    for i in co.items():
        ans += i[0] * i[1]
    print(ans)

Submission status: Time Limit Exceeded

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

        ans = []

        temp_s = A

        for i in range(Q):
                if str(BC[i][0]) not in temp_s:
                        pass
                else:
                        temp_s = [s.replace(str(BC[i][0]),str(BC[i][1])) for s in temp_s]
                temp_i = [int(s) for s in temp_s]
                ans.append(sum(temp_i))

        return ans

if __name__=="__main__":
        ans = main()

        n = len(ans)
        for i in range(n):
                print(ans[i])

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())
A=np.array(A)
b=[]
c=[]
for _ in range(Q):
  B,C=map(int,sys.stdin.readline().split())
  if B in set(A):
    A[A==B]=C
  print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
if __name__=='__main__':
    N = int(input())
    A = input().split(" ")
    #A_i = [int(s) for s in A]
    Q=int(input())
    str_list=[]
    S=[]
    for i in range(Q):
        str_list.append(list(input().split()))

    for j in range(Q):
        if str_list[j][0] in A:
            A=[s.replace(str_list[j][0],str_list[j][1]) for s in A]

        A_i = [int(s) for s in A]
        print(sum(A_i))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

for _ in range(q):
    b, c = map(int, input().split())
    l[c] += l[b]
    l[b] = 0
    sum = 0
    for i, e in enumerate(l):
        sum += i * e
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
data = input()
num = int(data)
o = input().rstrip().split(" ")
list =[]
for i in o:
    list.append(int(i))
list.sort()
p = input()
times = int(p)
ans = sum(list)
num_count=[list.count(i) for i in range(100001)]
for i in range(times):
    B, C = map(int, input().split(" "))
    a = num_count[B]
    num_count[B] = 0
    num_count[C] += a
    ans += a*(C-B)
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from numba import jit
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
for k, v in Counter(a).items():
    arr[k] = v
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit
def solve(n, arr, bc):
    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))


solve(n, arr, bc)

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())
BC = [list(map(int, input().split())) for _ in range(Q)]


alist.sort()
sumA = sum(alist)
for b, c in BC:
    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:
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
q=int(input())
bc=[]
for i in range(q):
    bc.append([int(i) for i in input().split()])

import collections
collect = collections.Counter(a)

for i in bc:
    if i[1] in collect:
        collect[i[1]] += collect.pop(i[0],0)
    else:
        if i[0] in collect:
            collect[i[1]] = collect.pop(i[0],0)
    ans=0
    for k,v in collect.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())
 
 
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):
  a.sort()
  b,c=map(int,input().split())
  for j in range(n):
    if a[j] > b:
      break
    if a[j]==b:
      a[j]=c
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy

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

a=int(input())#-1

for i in range(a):
  b=input().split(" ")
  array=numpy.where(array==int(b[0]),int(b[1]),array)
  sum=numpy.sum(array)
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(input().split())

Q = int(input())

for _ in range(Q):
    B, C = input().split()
    A = [s.replace(B, C) for s in A]
    print(sum([int(i) for i in 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)]
sa = set(a)
for i in range(q):
    while True:
        if bc[i][0] in sa:
            a[a.index(bc[i][0])] = bc[i][1]
        else:
            break
        sa = set(a)
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as c

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())
lst=list(map(int,input().split()))[:N]
Q=int(input())
for i in range(Q):
    x,y=map(int,input().split())
    lst.sort()
    for i in range(len(lst)):
        if lst[i]==x:
            lst[i]=y
        elif lst[i]==x+1:
            break
    print(sum(lst))