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: Wrong Answer

Solution code:
from collections import Counter
input = raw_input
n = int(input())
As = map(int, input().split(" "))

counter = Counter(As)
cur_sum = sum(As)

q = int(input())
for i in range(q):
    b, c = map(int, input().split(" "))
    try:
        num1 = counter[b]
    except:
        num1 = 0
    try:
        counter[c] += num1
    except:
        counter[c] = num1

    diff = (c - b) * num1
    cur_sum += diff
    print(cur_sum)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
Q=int(input())
for i in range(Q):
  total=0
  B,C=map(int,input().split())
  if B in A:
    A.remove(B)
    B=C
    A.append(C)
    for j in range(len(A)):
      total+=A[j]
    print(total)

Submission status: Wrong Answer

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

def solve():
    N = int(readline())
    A = list(map(int, readline().split()))
    Q = int(readline())
    BC = [list(map(int, readline().split())) for i in range(Q)]

    count = {}
    for a in A:
        count[a] = count.get(a, 0) + 1
    
    s = sum(A)

    for b, c in BC:
        if b in count:
            nb = count[b]
            diff = (c - b) * nb
            s += diff
            print(s)

            del count[b]
            count[c] = count.get(c, 0) + nb


solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input().split())
a = "".join(a)
q = int(input())

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n=int(input())
num_dict=defaultdict(int)
a_sum=0
for a in map(int,input().split()):
    num_dict[a]+=1
    a_sum+=a
for i in range(int(input())):
    b,c=map(int,input().split())
    a_sum+=c*num_dict[c]-b*num_dict[b]
    num_dict[c]+=num_dict[b]
    num_dict[b]=0
    print(a_sum)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    A = LI()
    Q = I()
    d = collections.Counter(A)
    ans = []
    cursum = sum(A)
    for i in range(Q):
        b, c = LI()
        cursum -= b * d[b]
        d[c] += d[b]
        cursum += c * d[b]
        ans.append(cursum)
    for a in ans:
        print(a)


main()

Submission status: Wrong Answer

Solution code:
import math as mt
import collections as cc
I=lambda:list(map(int,input().split()))
n,=I()
l=I()
f=cc.Counter(l)
s=sum(l)
for i in range(int(input())):
    x,y=I()
    s-=x*(f[x])
    s+=f[x]*y
    f[y]=f[x]
    f[x]=0
    print(s)

Submission status: Wrong Answer

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

from collections import Counter
C = Counter(A)
S = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b in C:
        vb = C[b]
        S -= b*vb
        S += c*vb
        print(S)
        if c in C:
            C[c] += vb
        else:
            C[c] = vb
    else:
        print(S)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = defaultdict(int)

for i in a:
  d[i] += 1
s = sum(a)

for i in range(q):
  b, c = map(int, input().split())
  if b < c:
    x = (c-b)*d[b]
    s = s + x
  else:
    x = (b-c)*d[b]
    s = s - x
  d[c] += d[b]
  print (s)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(0,q):
  bi, ci = list(map(int, input().split()))
  b.append(bi)
  c.append(ci)
for i in b:
  if i in arr:
    ind1 = arr.index(i)
    ind2 = b.index(i)
    arr[ind1] = c[ind2]
    print(sum(arr))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    Q=int(input())
    S = tuple(tuple(map(int, input().split())) for i in range(Q)) #改行行列
    ca=Counter(A)
    tot=sum(A)
    for B,C in S:
        red=ca[B]
        ca[C]+=red
        tot+=red*(C-B)
        print(tot)





if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import math

# import copy
# from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 998244353

def solve():
    n = getN()
    nums = getList()
    d = defaultdict(int)
    for num in nums:
        d[num] += 1
    ans = sum(nums)
    # print(d)
    Q = getN()
    for i in range(Q):
        b, c = getList()
        if b in d and d[b] > 0:
            ans -= b * d[b]
            ans += c * d[b]
            d[c] += d[b]

        print(ans)



def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
aa = tuple(map(int, input().split()))
actr = Counter(aa)
q = int(input())

tb = {}
for i in range(q):
    b, c = map(int, input().split())
    tb[b] = c
    for k, v in tb.items():
        if v == b:
            tb[k] = c

    ans = 0
    for a, n in actr.items():
        ans += tb.get(a, a) * n
    print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    dict_A = {}
    for a in A:
        dict_A[a] = dict_A.get(a, 0) + 1
    for i in range(Q):
        tmp = dict_A.get(B[i], 0)
        dict_A[B[i]] = 0
        dict_A[C[i]] = dict_A.get(C[i], 0) + tmp
    ret = 0
    for k in dict_A:
        ret += k * dict_A[k]
    print(ret)

if __name__ == "__main__":
    N = int(input())
    A = 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)
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int, input().split()))
q = int(input())
s = []
S = sum(a)
def find(b, a): #配列aからbを探し出す　
  counter = 0
  for i in range(len(a)):
    if a[i] == b:
      counter += 1
  return counter 
for i in range(q):
  b, c = map(int,input().split())
  count = find(b,a)
  S += count * (c - b)
  print(S)

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = tuple(map(int, input().split()))
q = int(input())

tb = {}
for i in range(q):
    b, c = map(int, input().split())
    tb[b] = c
    for k, v in tb.items():
        if v == b:
            tb[k] = c

    ans = 0
    for i in range(n):
        ans += tb.get(aa[i], aa[i])
    print(ans)

Submission status: Wrong Answer

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

ans = sum(A)

R = {}
for i in A:
  if i in R:
    R[i] += 1
  else:
    R[i] = 1

for b, c in BC:
  if b in R:
    t = R[b]
    ans += t * (c - b)
    if c in R:
      R[c] += t
    else:
      R[c] = t
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
    D[a] += 1

Q = int(input())
SUM = sum(A)
for q in range(Q):
    b, c = map(int, input().split())
    SUM -= D[b] * b
    SUM += D[b] * c
    D[c] += D[b]
    print(SUM)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
num = [0]*(10**5+1)
for i in a:
  num[i] += 1
sm = sum(a)
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  nx = num[x]
  ny = num[y]
  sm = sm+x*ny+y*nx-x*nx-y*ny
  print(sm)
  num[x] = ny
  num[y] = nx

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  if c in dict and b in dict:
    dict[c]+=dict[b]
  elif c not in dict and b in dict:
    dict[c]=dict[b]
  dict[b]=0
  ans=0
  for j in dict:
    ans+=dict[j]*j
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  for j in dict:
    if j==b:
      ans+=c*dict[b]
    else:
      ans+=j*dict[j]
  if b in dict and c not in dict:
    dict[c]=dict[b]
    dict[b]=0
  elif b in dict and c in dict:
    dict[c]+=dict[b]
    dict[b]=0
  print(ans)

Submission status: Time Limit Exceeded

Solution code:

from collections import deque

number = int(input())
aaa = list(map(int, input().split()))
dq = deque(aaa)
cntQ = int(input())
for _ in range(cntQ):
    num1, num2 = map(int, input().split())
    if dq.count(num1) != 0:
        for _ in range(dq.count(num1)):
            dq.appendleft(-1 * num1)
            dq.appendleft(num2)
    ans = sum(list(dq))
    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())
    alist = [c if s == b else s for s in alist]
    print(sum(alist))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
alist = list(map(int,input().split()))
alist.sort()
ssum = sum(alist)
q = int(input())
for i in range(q):
    b,c = map(int,input().split())
    x = 0
    for j in range(n):
        if alist[j] == b:
            x = 1
            alist[j] = c
            ssum = ssum-b+c
        elif x == 1 and alist[j] != b:
            break
    print(ssum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
num = Counter(A)
for i in range(Q):
    b,c = map(int,input().split())
    num[c] += num[b]
    num[b] = 0
    sums = 0
    for j in num:
        sums += num[j]*j
    print(sums)

Submission status: Time Limit Exceeded

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

#c = collections.Counter(A)
#print(c)

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

Submission status: Time Limit Exceeded

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

#c = collections.Counter(A)
#print(c)

for i in range(Q):
  A = [C[i] if B[i]==A[j] else A[j] for j in range(N)]
#  print(A)
  print(sum(A))
#l_replace = [100 if i > 0 else i for i in l]  
#
#  print(C[i]-1,B[i]-1)
##  D[B[i]-1]=0
#  print(int(np.average(D, weights=W)))

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())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    B,C=(map(int,input().split()))
    A_sum+=(C-B)*A_count[B]
    A_count[C]+=A_count[B]
    A_count[B]=0
    print(A_sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = [int(x) for x in input().split()]
a = collections.Counter(A)
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  if B in a:
    a[C] += a[B]
    a[B] = 0
  tmp = 0
  for i in a:
    tmp += a[i] * i
  print(tmp)

Submission status: Time Limit Exceeded

Solution code:
#                               #
    # author : samars_diary #
    # 18-09-2020 │ 15:37:26 #
#                               #

import sys, os.path

if(os.path.exists('input.txt')):
    sys.stdin = open('input.txt',"r")
    #sys.stdout = open('output.txt',"w")

sys.setrecursionlimit(10 ** 5)

def mod(): return 10**9+7
def i(): return sys.stdin.readline().strip()
def ii(): return int(sys.stdin.readline())
def li(): return list(sys.stdin.readline().strip())
def mii(): return map(int, sys.stdin.readline().split())
def lii(): return list(map(int, sys.stdin.readline().strip().split()))

#print=sys.stdout.write

def solve():
    a=ii();b=lii();c=ii();s=sum(b)
    for _ in range(c):
        d,e=mii() ; f=b.count(d)
        for _ in range(f): b.remove(d);b.append(e)
        s-=f*d-f*e
        print(s)
for _ in range(1): solve()

Submission status: Time Limit Exceeded

Solution code:
#                               #
    # author : samars_diary #
    # 18-09-2020 │ 15:37:26 #
#                               #

import sys, os.path

if(os.path.exists('input.txt')):
    sys.stdin = open('input.txt',"r")
    #sys.stdout = open('output.txt',"w")

sys.setrecursionlimit(10 ** 5)

def mod(): return 10**9+7
def i(): return sys.stdin.readline().strip()
def ii(): return int(sys.stdin.readline())
def li(): return list(sys.stdin.readline().strip())
def mii(): return map(int, sys.stdin.readline().split())
def lii(): return list(map(int, sys.stdin.readline().strip().split()))

#print=sys.stdout.write

def solve():
    a=ii();b=lii();c=ii();
    for _ in range(c):
        s=sum(b)
        d,e=mii()
        f=b.count(d)
        for _ in range(f):
            b.remove(d)
            b.append(e)
        s-=f*d;s+=f*e
        print(s)

for _ in range(1):
    solve()

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
Q=int(input())
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=map(int,input().split())
#print(A)

S=[[0 for i in range(N)] for j in range(Q+1)]


for i in range(Q):
  for j in range(N):
    S[i][j]=A[j]
    if B[i]==A[j]:
      A[j]=C[i]
S[Q]=A
for i in range(1,Q+1):
  print(sum(S[i]))

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())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    A_sum+=(BC[i][1]-BC[i][0])*A_count[BC[i][0]]
    A_count[BC[i][1]]+=A_count[BC[i][0]]
    A_count[BC[i][0]]=0
    print(A_sum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC=[]
for i in range(Q):
    BC.append(list(map(int,input().split())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    A_sum+=(BC[i][1]-BC[i][0])*A_count[BC[i][0]]
    A_count[BC[i][1]]+=A_count[BC[i][0]]
    print(A_sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    for j in range(N):
        if BC[i][0]==A[j]:A[j]=BC[i][1]
    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)
  ls = list(map(int,a.split()))
  print(sum(ls))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()
q = int(input())
bcls = []
for _ in range(q):
  bcls.append(list(map(str,input().split())))
for i in range(q):
  a = a.replace(bcls[i][0],bcls[i][1])
  ls = list(map(int,a.split()))
  print(sum(ls))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

def func(A,b,c):
    for i in range(len(A)):
        if A[i]==b:
            A[i]=c

for i in range(Q):
    func(A,B[i],C[i])
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
lis = {}
Q = int(input())
for x in A:
  if x in lis:
    lis[x] += 1
  else:
    lis[x] = 1
for i in range(Q):
  B, C = map(int, input().split())
  try:
    temp = lis[B]
    del lis[B]
    if lis == {} or not C in lis:
      lis[C] = temp
    else:
      lis[C] += temp
  except:
    pass
  ans = 0
  for x in lis:
    ans += x * lis[x]
  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())
  count = 0
  check = True
  while check == True:
    if B in A:
      count += 1
      A.remove(B)
    else:
      check = False
  for i in range(count):
    A.append(C)
  print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

Alist = list(map(int,input().split()))
Q = int(input())

Alistreplace = Alist
ans = sum(Alist)
for i in range(Q):
  B,C = map(int,input().split())
  Bnum = Alistreplace.count(B)
  ans = ans+(C-B)*Bnum
  Alistreplace = [C if j==B else j for j in Alistreplace]
  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)]

from collections import Counter
counter = Counter(a)

for b,c in bc:
    if b in counter:
        cnt = counter.pop(b)
        counter[c] += cnt
    print(sum([k*v for k,v in counter.items()]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

Alist = list(map(int,input().split()))
Q = int(input())

Alistreplace = Alist
for i in range(Q):
  B,C = map(int,input().split())
  #print (B)
  #print (C)
  Alistreplace = [C if j==B else j for j in Alistreplace]
  #print (Alistreplace)
  print (sum(Alistreplace))

Submission status: Time Limit Exceeded

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



for i in range(Q):
    X,Y = map(int,input().split())

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)

Submission status: Time Limit Exceeded

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

num = Counter(A)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
change = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
    a = [change[i][1] if j == change[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())
change = [list(map(int, input().split())) for i in range(q)]
sm = sum(a)
tmp = 0
for i in range(q):
    sm += a.count(change[i][0]) * (change[i][1] - change[i][0])
    print(sm)
    a = [change[i][1] if j == change[i][0] else j for j in a]

Submission status: Time Limit Exceeded

Solution code:
import collections

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

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in a_dict.items()}
    for _ in range(q):
        b, c = map(int, input().split())
        if b in a_dict:
            if c in a_dict:
                a_dict[c] += (a_dict.pop(b)//b) * c
            else:
                a_dict[c] = (a_dict.pop(b)//b) * c
        print(sum(a_dict.values()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

sumAs = sum(As)

for _ in range(Q):
  b, c = map(int, input().split())
  for i in range(len(As)):
    if As[i] == b:
      As[i] = c
      sumAs = sumAs - b + c
  print(sumAs)

Submission status: Time Limit Exceeded

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

s = sum(a)
for i in range(int(input())):
    b, c = map(int, input().split())
    for j in range(n):
        if a[j] == b:
            s -= b
            a[j] = c
            s += c
    print(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())
for _ in [0]*Q:
    b, c = map(int, input().split())
    A[A==b] = c
    print(np.sum(A))

Submission status: Time Limit Exceeded

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

for i in range(n):
    if a[i] not in d.keys():
        d[a[i]] = 1
    else:
        d[a[i]] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in d.keys():
        if c in d.keys():
            d[c] += d[b]
            del d[b]
        else:
            d[c] = d[b]
            del d[b]
    print(sum([k * v for k, v in d.items()]))

Submission status: Time Limit Exceeded

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

s = sum(a)

for i in range(q):
    cnt = 0
    b, c = map(int, input().split())
    for j in range(n):
        if a[j] == b:
            a[j] = c
            cnt += 1
    s += (c-b)*cnt
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def Replacing():
    N = int(input())
    A = np.array(list(map(int,input().split())))
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        A = np.where(A == b, c, A)
        print(A.sum())
        
Replacing()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def Replacing():
    N = int(input())
    A = np.array(list(map(int,input().split())))
    Q = int(input())
    bc = [tuple(map(int,input().split())) for i in range(Q)]
    for i,j in bc:
        res = 0
        A = np.where(A == i, j, A)
        print(A.sum())
        
Replacing()

Submission status: Time Limit Exceeded

Solution code:
def Replacing():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    bc = [tuple(map(int,input().split())) for i in range(Q)]
    for i,j in bc:
        res = 0
        for k in range(len(A)):
            if A[k] == i:
                A[k] = j
            res += A[k]
        print(res)
        
Replacing()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    d = dict()
    for a in list(map(int, input().split())):
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:
            temp = d[b]
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

        print(sum(k*v for k, v in d.items()))

main()

Submission status: Time Limit Exceeded

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

d = dict()

for a in A:
    d[a] = d.get(a, 0) + 1

ans = []
for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:
        temp = d[b]
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

    ans.append(list(d.items()))

for a in ans:
    print(sum(k*v for k, v in a))

Submission status: Time Limit Exceeded

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

    d = dict()

    for a in A:
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:
            temp = d[b]
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

        print(sum(k*v for k, v in d.items()))

main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(input())
A = list(map(int, stdin.readline().split()))

d = dict()

for a in A:
    d[a] = d.get(a, 0) + 1

for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:
        temp = d[b]
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

    print(sum(k*v for k, v in d.items()))

Submission status: Time Limit Exceeded

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

d = dict()

for a in A:
    d[a] = d.get(a, 0) + 1

for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:
        temp = d[b]
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

    print(sum(k*v for k, v in d.items()))

Submission status: Time Limit Exceeded

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

d = dict()

for a in A:
    d[a] = d.get(a, 0) + 1

for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:
        temp = d[b]
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

    print(sum(k*v for k, v in d.items()))

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):
    a = [(c[i] if j == b[i] else j) for j 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())
    bc = []
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    for i in range(q):
        for j in range(n):
            if bc[i][0] == a[j]:
                a[j] = bc[i][1]
        print(sum(a))
        
    


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = set(a)
s = sum(a)
l = [0]*(10**5)
p = []
for i in d:
    l[i-1] = a.count(i)
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + l[b-1]*(c-b)
    l[c-1] += l[b-1]
    l[b-1] = 0
    p.append(s)
for i in p:
    print(i)

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

m = 10**5
d = set(a)
s = sum(a)
l = [0]*m
for i in d:
    l[i-1] = a.count(i)
for b, c in bc:
    s = s + l[b-1]*(c-b)
    l[c-1] += l[b-1]
    l[b-1] = 0
    print(s)

Submission status: Time Limit Exceeded

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

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