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

index_A = { a: 0 for a in set(A) }

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

for bc in BC:
  if bc[0] in index_A:
    if bc[1] not in index_A:
      index_A[bc[1]] = 0
    index_A[bc[1]] += index_A[bc[0]]
    index_A[bc[0]] = 0
  print(sum(map(lambda i: i[0]*i[1], index_A.items())))

Submission status: Time Limit Exceeded

Solution code:
import numpy

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

a=int(input())
sum=numpy.sum(array)

for i in range(a):
  b=input().split(" ")
  if int(b[0]) in array:
    b0=int(b[0])
    b1=int(b[1])
    pos=len(numpy.where(array==b0)[0])
    array=numpy.where(array==b0,b1,array)
    diff=pos*(b1-b0)
    sum+=diff
    print(sum)
  else:
    sum=numpy.sum(array)
    print(sum)

Submission status: Time Limit Exceeded

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


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

l = []
A =','.join(A)
for i in range(Q):
    B, C = map(int, input().split())
    B = str(B)
    C = str(C)
    A = A.replace(B,C)
    l.append(A)

for j in range(Q):
    S = l[j]
    S = S.split(',')
    S = [int(str) for str in S]
    print(sum(S))

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

for i, bc in enumerate(BC):
  b = bc[0]
  c = bc[1]
  s = 0
  a[a == b] = c
  S[i] = a.sum()

[print(i) for i in S]

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

q=int(input())
b=[]
d=[]
for i in range(q):
    t,p=map(int,input().split())
    b.append(t)
    d.append(p)

for k in range(q):
    l=[i for i, x in enumerate(a) if x == b[k]]
    for j in l:
        a[j]=d[k]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from collections import Counter
    N = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    Q = int(sys.stdin.readline())
    BC = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    C = Counter(A)
    '''
    L = [0 for _ in range(10**5+1)]
    for key, value in C.items():
        L[key] = value
    '''
        
        
    for bc in BC:
        C[bc[1]] += C[bc[0]]
        C[bc[0]]= 0
        ans = 0
        for key, value in C.items():
            ans += key*value
        print(ans)
        



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = Counter(map(int, input().split()))
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  a[c] += a[b]
  del a[b]
  s = sum(x * y for x, y in a.items())
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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


for i in range(Q):
    B,C = map(int, input().split())
    a.sort()
    cnt = a.count(B)
    index = bisect.bisect(a,B)
    for j in range(cnt):
        a[index+j-cnt] = C

    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
_ = int(input().rstrip().split()[0])
a = [int(x) for x in input().rstrip().split()]
_ = int(input().rstrip().split()[0])

try:
    while True:

        l = input().rstrip()
        b, c = l.split(' ')

        for x in range(0, len(a)):
            if a[x] == int(b):
                a[x] = int( c )

        print(sum(a))
except EOFError:
    pass

Submission status: Time Limit Exceeded

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

def my_index(l, x, default=N):
    if x in l:
        return l.index(x)
    else:
        return default

for i in range(Q):
  BC.append(list(map(int, input().split())))

for i in range(Q):
  for j in range(my_index(A, BC[i][0]), N):
    if A[j] == BC[i][0]:
      A[j] = BC[i][1]
    else:
      break
  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 = list(map(int,input().split()))
    if a.count(b) != 0:
        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(input().split())
q = int(input())

ans = []
for i in range(q):
    b, c = input().split()
    A = [a.replace(b, c) for a in A]
    a = list(map(int, A))
    print(sum(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(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() )
    
    
def index_Multi(List,liter):
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L

listA = []
for q in range(Q):
    A = sorted(A)
    listA.append(sum(A))
    index = index_Multi(A,B[q])
    for i in index:
        A[i] = C[q]

listA.append(sum(A))
for a in range(1,len(listA)):
    print(listA[a])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
val2ind = {}
for i, one in enumerate(A):
    if one not in val2ind:
        val2ind[one] = []
    val2ind[one].append(i)

Q = int(input())

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        del val2ind[B]
        if C not in val2ind:
            val2ind[C] = cand
        else:
            cand.extend(val2ind[C])
            val2ind[C] = list(set(cand))
        for one in cand:
            A[one] = C
    print(sum(A))

Submission status: Time Limit Exceeded

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

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 i in range(Q):
  b, c = map(int, input().split())
  B.append(b)
  C.append(c)
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:
def resolve():
    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 a == b else a for a in A]
        print(sum(A))

resolve()

Submission status: Time Limit Exceeded

Solution code:

import sys


NList = int(input())
NumList = list(map(int,input().split()))

AltNum = int(input())
resultList = []
for i in range(AltNum):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    for j in range(len(NumList)):
        if(NumList[j] == a):
            NumList[j] = b

    resultList.append(sum(NumList))

for k in range(AltNum):
    print(resultList[k])

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

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

for search, replace in replace_map:
    A[A == search] = replace
    print(np.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())
for _ 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())
a = input()
q = int(input())
for i in range(q):
    b,c = map(str,input().split())
    a = a.replace(b,c)
    x = a.split()
    x = [int(s) for s in x]
    print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
  k = list(map(int, input().split()))
  A = [int( str(s).replace( str(k[0]),str(k[1]) ) ) for s in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=[0]*10**5
A=map(int,input().split())

for a in A:
    l[a-1]+=1

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
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:
    newList = [int(str(s).replace(str(i[0]), str(i[1]))) for s in newList]
    total = sum(newList)
    print(total)
  else:
    print(total)

Submission status: Time Limit Exceeded

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

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)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
for b,c in bc:
    N[c] += N[b]
    N[b] = 0
    print(sum([i*x for i,x in enumerate(N) if x != 0]))

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
  n=int(input())
  #A=list(map(int,input().strip("\n").split()))
  A=input().strip("\n")
  q=int(input())

  b,c=input().strip("\n").split()
  A=A.replace(b,c).replace(" ","+")
  ans=''
  ans+=str(eval(A))
  ans+="\n"
  for _ in range(1,q-1):
    #b,c=map(int,input().strip("\n").split())
    b,c=input().strip("\n").split()
    A=A.replace(b,c)
    ans+=str(eval(A))
    ans+="\n"
  b,c=input().strip("\n").split()
  A=A.replace(b,c)
  ans+=str(eval(A))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)

Submission status: Time Limit Exceeded

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

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1


BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in dictA.keys():
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        dictA.pop(x[0])
        # print(dictA)

    sumA = 0
    for k, v in dictA.items():
        sumA += k * v
    print(sumA)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


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

num_count = [0]*(10**5+1)
for i in A:
    num_count[i] += 1

for q in range(Q):
    B,C = map(int,input().split())
    num_count[C] += num_count[B]
    num_count[B] = 0
    ans = 0
    for i in range(10**5+1):
        ans += i*num_count[i]
    print(ans)

Submission status: Time Limit Exceeded

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

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

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:
_=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:
import collections

num = int(input())
original = list(map(int, input().split()))
trial_num = int(input())

total = sum(original)
now_list = original

for i in range(trial_num):
    trial = list(map(int, input().split()))
    c_list = collections.Counter(now_list)
    index = trial[0]
    total += (trial[1] - trial[0]) * int(c_list[index])
    print(total)
    
    now_list = [trial[1] if i == trial[0] else i for i in now_list]

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
l=list(map(int,input().split()))
b=int(input())
grand=0
for i in range(b):
  sum=0
  l1=list(map(int,input().split()))
  for i in range(len(l)):
    if l[i] ==l1[0]:
      l[i]=l1[1]
    sum+=l[i]
  print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

arr_B=[] 
arr_C=[] 

for i in range(Q):
  B, C = list(map(int, input().split()))
  arr_B.append(B)
  arr_C.append(C)

for i in range(Q):
  for j in range(N):
    if arr_B[i] == A[j]:
      A[j] = arr_C[i]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
total=sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    x=A.count(B)
    if x>0:
        for j in range(0,x):
            y=A.index(B)
            total=total-A[y]+C
            A[y]=C
    print(total)

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:
        index = [i for i, x in enumerate(a) if x == b]
        for i in index:
            a[i] = c

    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
#Replacing
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 = list(map(int, input().split()))
    CC = A==B
    NC = CC==False
    A = A*(NC) + C*CC
    print(A.sum())

Submission status: Time Limit Exceeded

Solution code:

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

result = a

l = []

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

  # l.append(sum(result))
  print(sum(result))


# for i in l:
#   print(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
def main():
    n = int(input())
    a = np.array(list(map(int, input().split())))
    q = int(input())
    for x in sys.stdin.readlines():
        b, c = map(int, x.split())
        a = [c if i == b else i for i in a]
        print(np.sum(a))

main()

Submission status: Time Limit Exceeded

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

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

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

    s = sum(item*a[item] for item in a)
    # s = 0
    # for i,v in a.items():
    #     if v != 0:
    #         s += i*v
    print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    k = 0
    B,C = map(int,input().split())
    change = [n for n, v in enumerate(A) if v == B]
    size = len(change)
    for j in range(size):
        A[change[k]] = C
        k += 1
    print(sum(A))

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

for i in range(0,Q):
    before = B[i][0]
    after  = B[i][1]
    X = (A == before)
    A[X] = after
    print(np.sum(A))
#print("\n".join(list(map(str,A.tolist()))))

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

BC= [list(map(str, input().split())) for i in range(Q)]
#
# print(BC)
for i in range(Q):
    A = [s.replace(BC[i][0],BC[i][1]) for s in A]

    print(sum(int(j) for j in A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dic = {}
for i in input().split():
    if i not in dic:
        dic[i] = 0
    dic[i] += 1
q = int(input())
for j in range(q):
    b,c = (str(x) for x in input().split())
    if b not in dic:
        pass
    else:
        if c not in dic:
            dic[c] = dic[b]
            dic[b] = 0
        else:
            dic[c] += dic[b]
            dic[b] = 0
    ans = 0
    for k in dic:
        ans += int(k)*dic[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

q = int(input())

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6
for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def readInt():
    return list(map(int, input().split()))
    
n = int(input())
a = readInt()
q = int(input())
for i in range(q):
    b,c = readInt()
    # print(b,c)
    a_c = []
    for i in a:
        if i==b:
            a_c.append(c)
        else:
            a_c.append(i)
    print(sum(a_c))
    a = a_c

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())
    D = [C if l == B else l for l in A]
    print(sum(D))
    A = D

Submission status: Time Limit Exceeded

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

Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    for j in range(N):
        if l[j] == B:
            l[j] = C
    print(sum(l))

Submission status: Time Limit Exceeded

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

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
    s.append(sum(k * v  for k, v in bucket.items()))
    
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())
for _ in range(Q):
    B, C = map(int, input().split())
    if B in A:
      A = [C if B == i else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
  # Xはもっとも近い整数，Nは与えられるP
  #X,N = map(int, input().split())
  N = input()

  # listを取得
  arr = list(map(int, input().split()))

  Q = int(input())

  for i in range(Q):
    li = list(map(int, input().split()))
    one = li[0]
    two = li[1]
    for j in range(len(arr)):
      if one == arr[j]:
        arr[j] = two
    print(sum(arr))
  
  # 正常終了
  sys.exit()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

    A = [c if a == b else a for a in A] 
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=list(map(int,input().strip().split()))
k=int(input())
for i in range(k):
    sumi=0
    a,b=[int(a) for a in input().split()]
    for j in range(n):
        if(arr[j]==a):
            arr[j]=b
    for i in range(n):
        sumi=sumi+arr[i]
    print(sumi)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
q = int(input())
ansList = [0]*q

for i in range(q):
    b,c = map(int,input().split())
    for j in range(n):
        if A[j] == b:
            A[j] = c
    ansList[i]  = sum(A)


[print(ans) for ans in ansList]

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  a = list(map(int,input().split()))
  q = int(input())
  Rangeq = list(range(q))
  xy = [map(int, input().split()) for _ in Rangeq]
  x, y = [list(i) for i in zip(*xy)]
  for j in Rangeq:
    a = [y[j] if i==x[j] else i for i in a]
    print(sum(a))
main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
num = 0
for i in range(N):
    num += A[i]
Q = int(input())
for _ in range(Q):
    B,C = [int(i) for i in input().split()]
    for i in range(N):
        if A[i] == B:
            A[i] = C
            num += C-B
    print(num)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

count_dict = dict(Counter(A_list))

for b, c in BC_list:
    if b in count_dict:
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
    _sum = 0
    for i, a in count_dict.items():
        _sum += i * a
    print(_sum)

Submission status: Time Limit Exceeded

Solution code:
# abc171_d.py
import collections

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

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Bi = BC[i][0]
    Ci = BC[i][1]

    for j in range(len(A)):
        if A[j] == Bi:
            A[j] = Ci
    
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

B = []
C = []
for _ in range(Q):
    B_i, C_i = list(map(int, input().split()))
    
    B.append(B_i)
    C.append(C_i)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque
from collections import Counter

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


#隣接リスト 1-order
def make_adjlist_d(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
        res[edge[1]].append(edge[0])
    return res


def make_adjlist_nond(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


#nCr
def cmb(n, r):
    return math.factorial(n) // math.factorial(r) // math.factorial(n - r)


def main():
    N = NI()
    A = NLI()
    Q = NI()
    querys = [NLI() for _ in range(Q)]

    Dic = dict(Counter(A))
    for b, c in querys:
        Dic.setdefault(c, 0)
        Dic.setdefault(b, 0)
        Dic[c] += Dic[b]
        del Dic[b]
        res = 0
        for n, x in Dic.items():
            res += n * x
        print(res)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = Counter(A)
    Q = int(input())
    for _ in range(Q):
        b,c = map(int, input().split())
        A[c] += A[b]
        A[b] = 0
        ans = 0
        for k,v in A.items():
            ans += k*v
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

for j in range(Q):
  SUM = 0
  str2 = input()
  B, C = int(str2.split(" ")[0]), int(str2.split(" ")[1])
  for i in range(N):
    if A[i] == B:
      A[i] = C
  SUM = sum(A)
  print(SUM)

Submission status: Time Limit Exceeded

Solution code:
import numpy

tmp=input()
list=input().split(" ")
array=numpy.zeros(len(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(" ")
  if int(b[0]) in array:
    array=numpy.where(array==int(b[0]),int(b[1]),array)
#    for j in pos:
#      array[j]=int(b[1])
    sum=numpy.sum(array)
    print(sum)
  else:
    sum=numpy.sum(array)
    print(sum)

Submission status: Time Limit Exceeded

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

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