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())
A_cnt = [0] * 100001
A_sum = 0

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

for _ in range(Q):
    B, C = map(int, input().split())
    A_cnt[C] = A_cnt[C] + A_cnt[B]
    A_cnt[B] = 0

    for i, x in enumerate(A_cnt):
        if A_cnt[i] > 0:
            A_sum = A_sum + (i * A_cnt[i])
    print(A_sum)
    A_sum = 0

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
  s,t=list(map(int,input().split()))
  a=[t if j==s else j for j in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
target = list(map(int, input().split()))
dct = dict()
for t in target:
    if t in dct:
        dct[t] += 1
    else:
        dct[t] = 1
lst = [list(map(int, input().split())) for i in range(int(input()))]
for l in lst:
    if l[0] in dct and not l[1] in dct:
        dct[l[1]] = dct[l[0]]
        dct[l[0]] = 0
    elif l[0] in dct:
        dct[l[1]] += dct[l[0]]
        dct[l[0]] = 0
    a = []
    for k, v in dct.items():
        a.append(k*v)
    print(sum(a))

Submission status: Time Limit Exceeded

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

s = sum(p)
result = ""

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

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

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

max_i = max(max(A), max(C))

A_N = [A.count(i) * i for i in range(max_i + 1)]

sum_i = sum(A_N)

for i in range(Q):
    diff_replace = int(A_N[B[i]] / B[i]) * C[i]
    sum_i += diff_replace - A_N[B[i]]
    
    A_N[C[i]] += diff_replace
    A_N[B[i]] = 0
    
    print(sum_i)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

    answer = sum(A)
    numNumberList = [0] * 100001

    for Ai in A:
        numNumberList[Ai] = A.count(Ai)    

    for i in range(Q):
        B, C = list(map(int, sys.stdin.readline().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    # count_dict = {i+1 : seq.count(i+1) for i in range(100000)}
    # count_list = [seq.count(i+1) for i in range(100000)]
    count_list = [0 if i+1 not in seq else seq.count(i + 1) for i in range(100000)]

    # _sum = sum([i*v for i,v in count_dict.items()])
    # for i in range(num):
    #     b, c = trials[i]
    #     _val = count_dict.get(b)
    #     if _val == 0:
    #         print(_sum)
    #         continue
    #     _sum += (c-b)*_val
    #     print(_sum)
    #     count_dict[b] = 0
    #     count_dict[c] += _val

    _sum = sum((k+1)*v for k,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b-1]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b-1] = 0
        count_list[c-1] += _val

if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(q):
    SUM = 0
    for j in range(len(str_list)):
        for k in range(len(num_list)):
            if str_list[j][0] == num_list[k]:
                num_list[k] = str_list[j][1]
        SUM = sum(num_list)
        print(SUM)
        count+=1
        if(count == q): break
    if(count == q): break

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

for i in range(q):
    b,c = bc[i][0],bc[i][1]
    for j in range(len(a)):
        if a[j] == b:
            a[j] = c
    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 = map(int, input().split())
    
    B.append(B_i)
    C.append(C_i)

max_i = max(max(A), max(C))

A_N = [A.count(i) * i for i in range(max_i + 1)]

for i in range(Q):
    A_N[C[i]] += int(A_N[B[i]] / B[i]) * C[i]
    A_N[B[i]] = 0
    
    print(sum(A_N))

Submission status: Time Limit Exceeded

Solution code:
#以上をまとめると
N=int(input())
A=list(map(int,input().split()))
Q=int(input())

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

S=sum(A)
#まずはじめの合計求める

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    cnt=A.count(B)
    A=[i if i != B else C for i in A]
    change=cnt*(C-B)
    S+=change
    print(S)

Submission status: Time Limit Exceeded

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

    answer = sum(A)
    numNumberList = [0] * 100001

    for Ai in A:
        numNumberList[Ai] = A.count(Ai)

    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    # count_dict = {i+1 : seq.count(i+1) for i in range(100000)}
    count_list = [seq.count(i+1) for i in range(100000)]

    # _sum = sum([i*v for i,v in count_dict.items()])
    # for i in range(num):
    #     b, c = trials[i]
    #     _val = count_dict.get(b)
    #     if _val == 0:
    #         print(_sum)
    #         continue
    #     _sum += (c-b)*_val
    #     print(_sum)
    #     count_dict[b] = 0
    #     count_dict[c] += _val

    _sum = sum((k+1)*v for k,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b-1]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b-1] = 0
        count_list[c-1] += _val

if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

Submission status: Time Limit Exceeded

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

for j in range(Q):
    b, c = map(int, input().split())

    rep = [i for i in A if i != b]
    for _ in range(len(A) - len(rep)):
        rep.append(c)
    A = rep[::]

    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
b_list = [list(map(int, input().split())) for _ in range(q)]

j = 0
a_replace = a_list
while j < q:
  a_replace = [b_list[j][1] if i == b_list[j][0] else i for i in a_replace]
  s = sum(a_replace)
  print(s)
  j += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ad = {}
for i in a:
  if i not in ad:
  	ad[i] = a.count(i)
q = int(input())
ll = []
for i in range(q):
	l = list(map(int, input().split()))
	ll.append(l)
	if l[1] not in ad:
		ad[l[1]] = 0

for i in ll:
  if i[0] in ad:
    ad[i[1]] = ad[i[1]] + ad[i[0]]
    ad[i[0]] = 0
    adsum = 0
    for j in ad:
      adsum += j*ad[j]
    print(adsum)
  else:
    adsum = 0
    for j in ad:
      adsum += j*ad[j]
    print(adsum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lsn = list(map(int,input().split()))
Q = int(input())
ls2 = []
for i in range(Q):
    Bi,Ci = map(int,input().split())
    ls2.append([Bi,Ci])
index = [0]*(10**5+1)
for i in range(10**5+1):
    index[i] = lsn.count(i)
sum1 = sum(lsn)
for l in ls2:
    sum1 = sum1 +(l[1]-l[0])*index[l[0]]
    index[l[1]] += index[l[0]]
    index[l[0]] = 0
    print(sum1)

Submission status: Time Limit Exceeded

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

arr = [0] * (10**5+1)
# arr = [i for i in range(1, 10**5+1)]

for i in A:
    arr[i] += i

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

    print(sum(arr))

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

a.sort()
uni = list(set(a))

kosu = [a.count(i) for i in uni]

for i in bc:
    b = i[0]
    c = i[1]

    if b in uni:
        # remove original
        tar = uni.index(b)
        uni.pop(tar)
        num = kosu.pop(tar)
        
        # add new
        if c in uni:
            idx = uni.index(c)
            kosu[idx] += num
        else:
            uni.append(c)
            kosu.append(num)

    ans = 0
    for ii in range(len(uni)):
        ans += uni[ii] * kosu[ii]

    print(ans)

Submission status: Time Limit Exceeded

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

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

for j in range(Q):
    rep = [i for i in A if i != B[j]]
    for _ in range(len(A) - len(rep)):
        rep.append(C[j])
    A = rep[::]

    print(sum(A))

Submission status: Time Limit Exceeded

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

while(q!=0):
    b,c=map(int,input().split())
    s=0
    for i in range(n):
        if(a[i]==b):
            a[i]=c
            s+=c
        else:
            s+=a[i]
    k.append(s)
    q-=1
for i in range(len(k)):
    print(k[i],end="\n")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()));
q = int(input())
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [0]*(100000);

for i in range(100000):
    bu[i] = a.count(i+1);
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c-1]);
    bu[d-1] += bu[c-1];
    bu[c-1] = 0;
    print(aa);

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
AA=[0]*(10**5+1)
for i in A:
  AA[i]+=1
for i in range(Q):
  B,C=map(int,input().split())
  AA[C]+=AA[B]
  AA[B]=0
  r=0
  for j in range(len(AA)):
    r+=(j)*AA[j]
  print(r)

Submission status: Time Limit Exceeded

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

for j in range(Q):
    rep = [i for i in A if i != B[j]]
    for _ in range(len(A) - len(rep)):
        rep.append(C[j])
    A = rep[::]

    print(sum(A))

Submission status: Time Limit Exceeded

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

Adic = collections.Counter(Alist)

def dicsum(dic):
  sum = 0
  for i in range(len(dic)):
    sum += list(dic.keys())[i]*list(dic.values())[i]
  return sum


for j in range(Q):
  if (Blist[j] in Adic) == True:
    Adic[Clist[j]] += Adic[Blist[j]] 
    Adic.pop(Blist[j])
    print(dicsum(Adic))
  else:
    print(dicsum(Adic))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
res = []
for i in range(q):
    b,c = map(int,input().split())
    while b in a:
        a.remove(b)
        a.append(c)
    res.append(sum(a))

for x in res:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
def run(n, seq, num, trials):
    '''
    '''
    count_dict = {i+1 : seq.count(i+1) for i in range(100000)}

    _sum = sum([i*v for i,v in count_dict.items()])
    for i in range(num):
        b, c = trials[i]
        _val = count_dict.get(b)
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_dict[b] = 0
        count_dict[c] += _val


if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

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:
import numpy as np

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

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

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

  ans = 0
  for num2 in range(n):
    if int(a[num2]) == int(b1):
      a[num2] = int(b2)
    ans += int(a[num2])
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

d = defaultdict(int)

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

operations = []

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


# n = 5
# a = [1, 2, 3, 4, 5]
# q = 3

# operations = [
#     (1, 2),
#     (2, 3),
#     (3, 4)
# ]

for hoge in a:
    d[hoge] += 1


for op in operations:
    target, goto = op

    d[goto] += d[target]

    try:
        del d[target]
        sum = 0
        for key, value in d.items():
            sum += key * value

        print(sum)

    except:
        sum = 0
        for key, value in d.items():
            sum += key * value

        print(sum)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
	N = int(input())
	A = input().split()
	Q = int(input())
	for _ in range(Q):
		b, c = input().split()
		A = [a.replace(b, c) for a in A]
		print(sum(map(int, A)))
resolve()

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

adic=dict()

for ai in a:
    if ai in adic:
        adic[ai]+=1
    else:
        adic[ai]=1
        
q=int(input())
b=[0]*(q)
c=[0]*(q)
bc=[[0]*2 for i in range(q)]
for i in range(q):
#    b[i],c[i]=map(int,input().split())
    bc[i]=map(int,input().split())
    
#print(adic)    
#for bi,ci in zip(b,c):
for bi,ci in bc:
    if bi in adic and ci in adic:
        adic[ci]=adic[ci]+adic[bi]
        del adic[bi]
    elif bi in adic and (not ci in adic):
        adic[ci]=adic[bi]
        del adic[bi]
#    print(i,adic)
    asum=0
    for i,ad in adic.items():
        asum+=i*ad
    print(asum)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b,c = [],[]
for i in range(q):
    B,C = map(int,input().split())
    b.append(B)
    c.append(C)
for i in range(q):
    while b[i] in a:
        a.remove(b[i])
        a.append(c[i])
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
_ = input()
s = list(map(int, input().split()))
n = int(input())
list_ = []
for _ in range(n):
  k,v = map(int, input().split())
  list_.append({k:v})

for maps in list_:
  k=list(maps.keys())[0]
  v=list(maps.values())[0]
  for i in range(len(s)): 
    if s[i]==k:
      s[i] = v
  ans = 0
  for num in s:
    ans += int(num)
  print(ans)

Submission status: Time Limit Exceeded

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

for j in range(Q):
    if BC[j][0] in A:
        for k in range(N):
            if A[k] == BC[j][0]:
                A[k] = BC[j][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)] #
B, C = [list(i) for i in zip(*BC)]
S = sum(A)
for i in range(0, Q):
    D = A.count(B[i])
    A = [C[i] if j == B[i] else j for j in A]
    # print(sum(A))
    S = S + (D * (C[i]-B[i]))
    print(S)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[[a.count(i+1)] for i in range(10**5)]
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split(" ")))
B=[]
S=0
for i in range(1000000):
  B.append(A.count(i))
for i in A:
  S=S+i
q=int(input())
for i in range(q):
  b,c=map(int,input().split(" "))
  S=S+(c-b)*B[b]
  print(S)
  B[c]=B[c]+B[b]
  B[b]=0

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
B = [];C=[]
for i in range(Q):
    b,c = map(int,input().split())
    B.append(b);C.append(c)
from collections import defaultdict
R = defaultdict(int)
for a in A:
    R[a] += 1
for q in range(Q):
    cnt = R[B[q]]
    R[C[q]] += cnt
    del R[B[q]]
    ans = 0
    for key in R.keys():
        ans += (R[key] * key)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
count =int(input())
nums=[]
nums=input().split(" ")
count2=int(input())

nums2=[]
for i in range(count):
    nums2.append(int(nums[i]))
#print(nums2)
#if nums2[0]==1:
#    nums2[0]=10
#print(nums2)

totals=[]
for i in range(count2):
    nums3=[]
    nums3=input().split(" ")
    num1=int(nums3[0])
    num2=int(nums3[1])
    #print(num1,num2)
    total=0
    for k in range(count):
        #print(nums2[k])
        if nums2[k]==num1:
            #print("a")
            nums2[k]=num2
        total=total+nums2[k]
        if k == count-1:
            totals.append(total)
            #print(nums2)
            

#print(totals)
for i in range(len(totals)):
    print(totals[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = list(list(map(int, input().split())) for _ in range(Q))

for i in range(Q):
    for j in range(N):
        if A[j] == S[i][0]:
            A[j] = S[i][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
A=[int(_) for _ in input().split(" ")]
Q=int(input())

c = collections.Counter(A)


#d['k10'] = d.pop('k1')    
    
for i in range(Q):
  B,C=map(int,input().split(" "))
  
  if C in c:
    c[C] += c[B]
    del c[B]
  else:
    c[C] = c[B]
    del c[B]
  
  #print(c)
  
  #s=list(c.keys())
  ans=0
  for v in c:
    ans += v*c[v]
  
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
c = collections.Counter(A)
counter_key = list(c.keys())
counter_value = list(c.values())
list_sum_A=[]
sum_A=sum(A)
list_sum_A.append(sum_A)
B_list = []
C_list = []
for i in range(Q):
    B,C= map(int,input().split())
    B_list.append(B)
    C_list.append(C)

for i in range(1,Q+1):
    B = B_list[i-1]
    C = C_list[i-1]
    if(B not in counter_key):
        list_sum_A.append(list_sum_A[i-1])
    else:
        key_index=counter_key.index(B)
        list_sum_A.append(list_sum_A[i-1]+C*counter_value[key_index]-B*counter_value[key_index])
        if(C not in counter_key):
            counter_key[key_index]=C
        else:
            C_value_index = counter_key.index(C)
            counter_value[C_value_index]=counter_value[C_value_index]+counter_value[key_index]
            counter_value.pop(key_index)
            counter_key.pop(key_index)
for i in range(1,Q+1):
    print(list_sum_A[i])

Submission status: Time Limit Exceeded

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

adic=dict()

for ai in a:
    if ai in adic:
        adic[ai]+=1
    else:
        adic[ai]=1
        
q=int(input())
b=[0]*(q)
c=[0]*(q)
for i in range(q):
    b[i],c[i]=map(int,input().split())
    
#print(adic)    
for bi,ci in zip(b,c):
    if bi in adic and ci in adic:
        adic[ci]=adic[ci]+adic[bi]
        del adic[bi]
    elif bi in adic and (not ci in adic):
        adic[ci]=adic[bi]
        del adic[bi]
#    print(i,adic)
    asum=0
    for i,ad in adic.items():
        asum+=i*ad
    print(asum)

Submission status: Time Limit Exceeded

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

trans=[[] for _ in range(Q)]
for i in range(Q):
    B,C=map(int,input().split())
    trans[i].append(B)
    trans[i].append(C)
    
for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    A=[i if i != B else C for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()

q = int(input())
tmp = []
for i in range(q):
    bc = input().split()
    tmp.append(bc)

for j in range(len(tmp)):
    a = a.replace(tmp[j][0], tmp[j][1])
    answer = a.split()
    answer_int = [int(x) for x in answer]
    print(sum(answer_int))

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

for i in range(Q):
    temp = A == BC[i][0]
    temp = temp.astype(np.int)
    temp = temp * BC[i][1]

    temp2 = A != BC[i][0]
    temp2 = temp2.astype(np.int)

    A = A*temp2 + temp

    print(A.sum())

Submission status: Time Limit Exceeded

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

import copy

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

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

for q in range(Q):
    indexes = [i for i, a in enumerate(A) if a == B[q]]
    for i in indexes:
        A[i] = C[q]
        
    print(sum(A), flush=True)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
q = int(input().strip())
bc = list()
for i in range(q):
    b,c = [int(x) for x in input().strip().split(' ')]
    bc.append((b,c))

a2 = list()
clear_list = a[:]
while clear_list:
    t = clear_list[0]
    a2.append([t,a.count(t)])
    clear_list = [x for x in clear_list if x != t]            

a2.sort()
for x in bc:
    for y in a2:
        if y[0] == x[0]:
            y[0] = x[1]
        elif y[0] > x[0]:
            break
    a2.sort()
    s = math.floor(sum([float(x[0]) * x[1] for x in a2]))
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(map(int,input().split()))
#print(a)
q = int(input())
z = [0 for i in range(100001)]
for i in range(len(a)):
    z[a[i]] += 1

b = []
for _ in range(q):
    b.append(list(map(int,input().split())))
#print(b)


for i in range(q):
    z[b[i][1]] += z[b[i][0]]
    z[b[i][0]] = 0
    #print(z[:5])
    s = 0
    for j in range(len(z)):
        s += j * z[j]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(Q):
	B,C=map(int, input().split())
	A=np.where(A==B, C,A)
	ans.append(sum(A))

for i in ans:
	print(i)

Submission status: Time Limit Exceeded

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

num_A = [A.count(i) for i in range(10**5+1)]
first = sum(A)

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

Submission status: Time Limit Exceeded

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

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

l = [0] * 100000
m = list(range(1,100001))

for i in range(n):
  l[a[i]-1] += 1
  
for j in range(q):
  b = list(map(int,input().split()))
  l[b[1]-1] += l[b[0]-1]
  l[b[0]-1] = 0
  print(sum(list(map(lambda x,y: x*y, l, m))))

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 x==b else x for x in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

num_A = [A.count(i) for i in range(10**5+1)]
first = 0
for i, n in enumerate(num_A):
    first += n*i

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

Submission status: Time Limit Exceeded

Solution code:
import collections 
_ = input()
a = [*map(int, input().split())]
q = int(input())
b = [[*map(int,input().split())] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    if j not in c:
        c[j] = 0
    if i not in c:
        c[i] = 0
    c[j]+=c[i]
    c[i] = 0
    print(sum(k*v for k,v in c.items()))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
l=[ int(_) for _ in input().split()]
q=int(input())
for i in range(q):
  tmpb,tmpc=map(int,input().split())
  l=[tmpc if j==tmpb else j for j in l]
  print(sum(l))

Submission status: Time Limit Exceeded

Solution code:

if __name__=="__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    bc = [list(map(int, input().split())) for l in range(Q)]
#    print(bc)

    for i in range(0,Q):
        tmp = bc[i][0]
#        print(tmp)
        for j in range(N):
            if A[j] == tmp:
                A[j] = bc[i][1]
        total = sum(A)
        print(total)

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

ans = np.sum(a)
for i in range(q):
    if np.count_nonzero(a == b[i]) > 0:
        ans += np.count_nonzero(a == b[i])*(c[i]-b[i])
        a = np.where(a == b[i], c[i],a)
    print(ans)

Submission status: Time Limit Exceeded

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

for j in range(Q):
    res = 0
    A_fake = A_ori
    for k in range(N):
        if A_fake[k] == BC[j][0]:
            A_fake[k] = BC[j][1]
        res += A_fake[k]
    print(res)

Submission status: Time Limit Exceeded

Solution code:
def replace_int(a_dict, b, c):
    if not a_dict.get(c):
        a_dict[c] = 0
    if a_dict.get(b):
        a_dict[c] = a_dict[c] + a_dict[b]
        a_dict[b] = 0



n = int(input())
a_dict = dict()
a_list = map(int, input().split())

for a in a_list:
    a_dict[a] = a_dict[a] + 1 if a_dict.get(a) else 1

q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    replace_int(a_dict, b, c )
    sum = 0
    for k, v in a_dict.items():
        sum += k * v
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
A=[int(_) for _ in input().split(" ")]
Q=int(input())

c = collections.Counter(A)


#d['k10'] = d.pop('k1')    
    
for i in range(Q):
  B,C=map(int,input().split(" "))
  
  if C in c:
    c[C] += c[B]
    del c[B]
  else:
    c[C] = c[B]
    del c[B]
  
  #print(c)
  
  s=list(c.keys())
  j=0
  ans=0
  for v in c.values():
    ans += s[j]*v
    j=j+1
  
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
changes = [tuple(map(int, input().split())) for i in range(Q)]

num = defaultdict(int)
for i in A:
    num[i] += 1
 
for change in changes:
    num[change[1]] += num[change[0]]
    del num[change[0]]
    s = sum([i * c for i, c in num.items()])
    print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(input().split())
Q = int(input())
B = []
C = []
for q in range(Q):
    newB, newC = map(int,input().split())
    B.append(newB)
    C.append(newC)
for i in range(Q):
    A = [int(str(s).replace(str(B[i]), str(C[i]))) for s in A]
    sums = sum(A)
    print(sums)