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:
from operator import mul
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    k = list(cnt.keys())
    v = list(cnt.values())
    print(sum(map(mul, k, v)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in A:
        for n in range(N):
            if A[n] == b:
                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())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in A:
        for n in range(N):
            if A[n] == b:
                A[n] = c
    ans.append(sum(A))
    
for q in range(Q):
    print(ans[q])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = []
ans = sum(A)

for i in range(100001):
    if i in A:
        num.append(A.count(i))
    else:
        num.append(0)

for i in range(Q):
    B, C = map(int, input().split())
    ans += (C - B)*num[B]
    num[C] += num[B]
    num[B] = 0

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = [0 for i in range(100000)]
ans = sum(A)

for i in range(Q):
    B, C = map(int, input().split())
    A = [C if j == B 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())
S = [list(map(int, input().split())) for l in range(Q)]

numList = {}

for n in range(N):
  if A[n] not in numList:
    numList[A[n]] = 0
  numList[A[n]] += 1

for q in range(Q):
  if S[q][0] in numList:
    if S[q][1] not in numList:
      numList[S[q][1]] = 0
    numList[S[q][1]] += numList[S[q][0]]
    numList[S[q][0]] = 0
  sum = 0
  for key, value in numList.items():
    sum += key * value
  print(sum)

Submission status: Time Limit Exceeded

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

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

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())
  count = A.count(B)
  if count >0:
    for _ in range(count):
      A.remove(B)
    A.extend([C]*count)
  print(sum(A))
  #print(A)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
num = [0]*100000
s = sum(A)
for i in range(max(A)):
    num[i] = A.count(i+1)
for i in range(Q):
    B, C = map(int,input().split())
    s += (C-B)*num[B-1]
    print(s)
    num[C-1] += num[B-1]
    num[B-1] = 0

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())
B = [0]*Q
C = [0]*Q
num = [0]*100000
s = sum(A)
for i in range(Q):
    B[i], C[i] = map(int,input().split())
for i in range(max(A)):
    num[i] = A.count(i+1)
for i in range(Q):
    if num[B[i]-1] == 0:
        print(s)
        continue
    else: 
        s += (C[i]-B[i])*num[B[i]-1]
        print(s)
        num[C[i]-1] += num[B[i]-1]
        num[B[i]-1] = 0

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())
B = [0]*Q
C = [0]*Q
num = [0]*100000
s = sum(A)
for i in range(Q):
    B[i], C[i] = map(int,input().split())
for i in range(max(A)):
    num[i] = A.count(i+1)
for i in range(Q):
    s += (C[i]-B[i])*num[B[i]-1]
    print(s)
    num[C[i]-1] += num[B[i]-1]
    num[B[i]-1] = 0

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
num = [0]*100000
s = sum(A)
for i in range(Q):
    B[i], C[i] = map(int,input().split())
for i in range(100000):
    num[i] = A.count(i+1)
for i in range(Q):
    s += (C[i]-B[i])*num[B[i]-1]
    print(s)
    num[C[i]-1] += num[B[i]-1]
    num[B[i]-1] = 0

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
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):
    for j in range(N):
        if A[j] == B[i]:
            A[j] = C[i]
    print(sum(A))

Submission status: Time Limit Exceeded

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

#print(A)
#print(B)
#print(C)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = input()
C_A = Counter(A.split())
S = 0
for ca in C_A:
  S += int(ca) * C_A[ca]

Q = int(input())
for q in range(Q):
  b, c = input().split()
  A = A.replace(b,c)
  S += (int(c) - int(b)) * C_A[b]
  C_A = Counter(A.split())
  
  print(S)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
Q = int(input())
for q in range(Q):
  b, c = input().split()
  A = A.replace(b,c)
  An = list(map(int,A.split()))
  print(sum(An))

Submission status: Time Limit Exceeded

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

adic = {}

for i in a:
  if i in adic:
    adic[i] += 1
  else:
    adic[i] = 1


for i in range(q):
  b, c = input().split()
  if b in adic:
    if c not in adic:
      adic[c] = 0
    adic[c] += adic.pop(b)
  print( sum(int(j)*int(adic[j]) for j in adic)  )

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

anum=[]
asum=sum(a) #当初の数列Aの要素の和

for i in a:
  acnt=a.count(i)
  anum.append([i,acnt])

d=dict(anum) #当初の数列Aの各整数の数のdictionary

for i in range(q):
  if bc[i][0] not in d.keys():
    print(asum)
  if bc[i][0] in d.keys():
    x=(bc[i][1]-bc[i][0])*d[bc[i][0]]
    print(asum+x)
    asum+=x
    if bc[i][1] in d.keys():
      d[bc[i][1]]=d[bc[i][1]]+d[bc[i][0]]
      d[bc[i][0]]=0
    else:
      d[bc[i][1]]=d[bc[i][0]]
      d[bc[i][0]]=0

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

for j in range(q):
  for n, i in enumerate(a):
    if i==bc[j][0]:
      a[n]=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=[list(map(int,input().split())) for i in range(q)]

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

from collections import Counter


def solve(N: int, A: "List[int]", Q: int, BC: "List[(int,int)]"):
    A = Counter(A)
    answers = []
    for b, c in BC:
        if b != c:
            A[c] += A[b]
            del A[b]
        ans = sum((i * n for i, n in A.items()))
        answers.append(ans)
    return answers


def main():
    N = int(input())  # type: int
    A = list(map(int, input().split()))
    Q = int(input())
    BC = [tuple(map(int, input().split())) for _ in range(Q)]
    answer = solve(N, A, Q, BC)
    for ans in answer:
        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())
for _ in range(q):
  b,c = map(int,input().split())
  for n,i in enumerate(a):
    if i == b:
      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())
BCs = [list(map(int,input().split())) for _ in range(Q)]
max_A = max(A)

# Aの値について数列を作る
A_num = [A.count(i) for i in range(10 ** 5 + 1)]
Si = sum(A)

for BC in BCs:
    # BC[0] ...　置換前の値
    # BC[1] ...　置換後の値
    
    if A_num[BC[0]] > 0: 
        Si += A_num[BC[0]] * (BC[1] -BC[0])
        A_num[BC[1]] += A_num[BC[0]]
        A_num[BC[0]] = 0

    print(Si)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import jit

N = int(input())
A = np.asarray(list(map(int, input().split())))
Q = int(input())
B, C = np.zeros(Q, dtype=np.int64), np.zeros(Q, dtype=np.int64)
for i in range(Q):
    [b, c] = list(map(int, input().split()))
    B[i], C[i] = b, c
    
@jit(nopython=True)
def calc(A, B, C, Q):
    for i in range(Q):
        A = np.where(A==B[i], C[i], A)
        print(np.sum(A))

calc(A, B, C, Q)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B, C = np.zeros(Q, dtype=np.int32), np.zeros(Q, dtype=np.int32)
for i in range(Q):
    [b, c] = list(map(int, input().split()))
    B[i], C[i] = b, 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:
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):
    b = bc[i][0]
    c = bc[i][1]
    A = [c if i == b else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def abc171d_replacing():
    n = int(input())
    a = list(map(int, input().split()))
    a_dict = {}
    for v in a:
        if a_dict.get(v) is None:
            a_dict[v] = 1
        else:
            a_dict[v] += 1
    q = int(input())
    s = []
    for _ in range(q):
        b, c = map(int, input().split())
        if b in a_dict.keys():
            if c in a_dict.keys():
                a_dict[c] += a_dict[b]
            else:
                a_dict[c] = a_dict[b]
            del a_dict[b]
        total = 0
        for k, v in a_dict.items():
            total += k * v
        s.append(total)

    for v in s:
        print(v)

abc171d_replacing()

Submission status: Time Limit Exceeded

Solution code:
#N, K = map(int, input().split( ))
#L = list(map(int, input().split( )))
N = int(input())
A = list(map(int, input().split( )))
Q = int(input())
Sum = sum(A)
#Q個の整数の組(B,C)が与えられてB \to Cとして合計を計算
#合計は(Ci-Bi)*(A.count(Bi))変化する.
for i in range(Q):
  E= list(map(int, input().split( )))
  b, c = E[0], E[1]
  dis = c - b 
  Num = A.count(b)
  Sum = Sum + dis*Num
  print(Sum)
  A = [c if a == b else a for a in A]

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

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

    lst = [0 for _ in [0] * (10 ** 5 + 1)]
    sum_lst = 0
    for n in range(10 ** 5 + 1):
        lst[n] = A.count(n)
        sum_lst += n * lst[n]

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

        sum_lst += (C - B) * lst[B]
        print(sum_lst)
        lst[C] += lst[B]
        lst[B] = 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

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

    lst = [0 for _ in [0] * (10 ** 5 + 1)]
    sum_lst = 0
    for n in range(10 ** 5 + 1):
        lst[n] = A.count(n)
        sum_lst += n * lst[n]

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

        sum_lst += (C - B) * lst[B]
        print(sum_lst)
        lst[C] += lst[B]
        lst[B] = 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    _ = int(input())
    A = [int(x) for x in input().split()]
    Q = int(input())

    sum_A = sum(A)
    for _ in range(Q):
        B, C = map(int, input().split())

        sum_A = sum_A + A.count(B) * (C - B)
        print(sum_A)
        A = [C if x == B else x for x in A]


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    _ = int(input())
    A = [int(x) for x in input().split()]
    Q = int(input())

    for _ in range(Q):
        B, C = map(int, input().split())
        res = []
        ans = 0
        for a in A:
            res.append(a if a != B else C)
            ans += res[-1]
        print(ans)
        A = list(res)


if __name__ == '__main__':
    main()

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x.strip()) for x in input().split()]
bc = []
q = int(input())
for i in range(q):
  bc_i = [int(y.strip()) for y in input().split()]
  bc.append(bc_i)
for j in range(q):
  a = [bc[j][1] if bc[j][0] == i else i for i in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
dic = {}
s = sum(a)
for i in range(1,10**5+1):
    if i in a:
        dic[i] = a.count(i)
    else:
        dic[i] = 0
lis=[]
for j in range(q):
    b,c = map(int,input().split())
    lis.append([b,c])

for p in lis:
    s += (p[1]-p[0])*dic[p[0]]
    dic[p[1]] += dic[p[0]]
    dic[p[0]] = 0
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

summ=sum(a)
num=[a.count(i) for i in range(100001)]
for i in range(q):
    b,c=map(int,input().split())
    b_num=num[b]
    num[c] += b_num
    num[b]=0
    summ +=(c-b)*b_num
    print(summ)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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


for nums in B:
    A = [nums[1] if i == nums[0] else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys

def input():
  return sys.stdin.readline()

N=int(input())
A=sorted(list(map(int,input().split())))
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split())
  l=bisect.bisect_left(A,b)
  r=bisect.bisect_right(A,b)
  for j in range(l,r):
    A[j]=c
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys

def input():
  return sys.stdin.readline()

N=int(input())
A=sorted(list(map(int,input().split())))
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split())
  l=bisect.bisect_left(A,b)
  r=bisect.bisect_right(A,b)
  for j in range(l,r):
    A[j]=c
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N=int(input())
A=sorted(list(map(int,input().split())))
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split())
  l=bisect.bisect_left(A,b)
  r=bisect.bisect_right(A,b)
  for j in range(l,r):
    A[j]=c
  print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

c = Counter(a)

ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    c[ci] = c[bi]+c[ci]
    c[bi] = 0
    csum = 0
    for k,v in c.items():
        csum += k*v
    ans.append(csum)
    
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter

n = map(int,input().split())
a = list(map(int,input().split()))
Q = int(input())
a_count = dict(Counter(a))



def change_dict_key(d, old_key, new_key, default_value=None):
    d[new_key] = d.pop(old_key, default_value)


#change_dict_key(a_count, 1, 5)

#print(a_count)



for q in range(Q):
    num = list(map(int,input().split()))
    
    if(num[0] not in a_count):
        print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
        continue

    if(num[1] not in a_count):
        change_dict_key(a_count,num[0],num[1])

    else :
        a_count[num[1]] += a_count[num[0]] 
        del a_count[num[0]] 
    
    #print(a_count.values())

    print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
    #print((sum(list_a))  )

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = map(int,input().split())
a = list(map(int,input().split()))
Q = int(input())

list_a = np.array(a)
#print(list_a)

for q in range(Q):
    num = list(map(int,input().split()))
    list_a = np.where(list_a == num[0] ,num[1],list_a)

    print((sum(list_a))  )

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
for i in range(q):
  b[i],c[i]=map(int,input().split())

s=sum(a)

for i in range(q):
  while b[i] in a:
    s+=c[i]-b[i]
    a.remove(b[i])
    a.append(c[i])
  print(s)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
for i in range(q):
  b[i],c[i]=map(int,input().split())

s=sum(a)

for i in range(q):
  cnt=a.count(b[i])
  s+=cnt*(c[i]-b[i])
  for _ in range(cnt):
    a.remove(b[i])
    a.append(c[i])
  print(s)

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


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

Submission status: Time Limit Exceeded

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

L=[0]*(10**5+1)
B=dict(Counter(A))
for k in B.keys():
    L[k]=B[k]
# print(L)

C=set(B)
sum = 0
for k in B.keys():
    sum += k*B[k]
for i in range(q):
    x=Q[i][0]
    y=Q[i][1]
    if L[x]==0:
        print(sum)
        continue
    L[y]+=L[x]
    L[x]=0
    C.add(y)
    C.remove(x)
    sum=0
    for k in C:
        sum+=k*L[k]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=list(map(int, input().split()))
q=int(input())
Q=[list(map(int, input().split())) for _ in range(q)]
# print(Q)

B=dict(Counter(A))
# print(B)

sum = 0
for k in B.keys():
    sum += k*B[k]
# print(B[2])
for i in range(q):
    x=Q[i][0]
    y=Q[i][1]
    if x not in B:
        print(sum)
        continue
    else:
        v = B[x]
        if y in B:
            B[y] += v
        else:
            B[y]=v
        del B[x]
        sum = 0
        for k in B.keys():
            sum += k*B[k]
        print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
    bc = input().split()
    b[i] = int(bc[0])
    c[i] = int(bc[1])
a_max = max(a+c)
cnt = [0]*a_max
sum = 0
for i in range(a_max):
    m = i + 1
    cnt[i] = a.count(m)
    sum += m*cnt[i]
for i in range(q):
    sum += (c[i]-b[i])*cnt[b[i]-1] 
    cnt[c[i]-1] += cnt[b[i]-1]
    cnt[b[i]-1] = 0

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
M=collections.Counter(A)
for i in range(Q):
  B,C=map(int,input().split())
  M[C]+=M[B]
  M[B]=0
  ans=0
  for j in M.items():
    ans+=j[0]*j[1]
  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(N):
        if A[j]==B:
            A[j]=C
    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())
BC= [list(map(int,input().split())) for _ in range(Q)]

ans = []

for b,c in BC:
    A[A == b] = c
    ans.append(np.sum(A))

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

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

for b,c in BC:
    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())
for i in range(q):
  b, c = map(int, input().split())
  sum = 0
  for j in range(len(a)):
    if a[j] == b:
      a[j] = c
    sum += a[j]
  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())
  a = [c if x == b else x for x in a]
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=int(input())
for i in range(c):
  S=list(map(int,input().split()))
  cou=A.count(S[0])
  for x in range(cou):
    A.append(S[1])
    A.remove(S[0])
  print(sum(A))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    l = [0]*10**5
    for i in a:
        l[i-1] += i
    q = int(input())
    for i in range(q):
        b,c = map(int, input().split())
        l[c-1] += l[b-1]//b*c
        l[b-1] = 0
        print(sum(l))



if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
BC = [list(map(int,input().split())) for _ in range(Q)]
for j in range(Q):
    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:
s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
    l[A[i]] += 1
    s.add(A[i])
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    if l[B] > 0:
        s.add(C)
        l[C] += l[B]
        l[B] = 0
        s.discard(B)
    sum=0
    for i in s:
        sum += i * l[i]
    print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
input()
ip = "-".join(list(input().split()))
for _ in range(int(input())):
    x,y = input().split()
    ip = ip.replace(x,y)
    print(sum(list(map(int, ip.split('-')))))

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())
  while b in a:
    a[a.index(b)]=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())
  for aa in a:
    if aa == b:
      a[a.index(aa)]=c
  print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(100000):
    bucket[i] = A.count(i)

sum = sum(A)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline 
N=int(input())
L=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
  a,b=map(int,input().split())
  L=[i if i!=a else b for i in L]
  print(sum(L))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(input().split())
Q=int(input())
for i in range(Q):
  a,b=input().split()
  L=[i if i!=a else b for i in L]
  print(sum(list(map(int,L))))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A=list(map(int,input().split()))
A=np.array(A)
Q=int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]
for i in range(Q):
	for j in range(len(A)):
		if A[j]==B[i]:
			A[j]=C[i]   
	print(np.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())
P = [list(map(int,input().split())) for i in range(Q)]

A = np.array(A)

for item in P:
  np.place(A, A == item[0], item[1])
  print(np.sum(A))

Submission status: Time Limit Exceeded

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

D={x:A.count(x) for x in set(A)}

for step in range(Q):
    b,c=map(int,input().split())
    if b not in D:
        print(somme)
        continue

    somme=somme+(c-b)*D[b]
    if c in D:
        D[c]=D[c]+D[b]
    else:
        D[c]=D[b]
    del D[b]    
    print(somme)

Submission status: Time Limit Exceeded

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

D={x:A.count(x) for x in set(A)}

for step in range(Q):
    b,c=map(int,input().split())
    if b not in D.keys():
        print(somme)
        continue

    somme=somme+(c-b)*D[b]
    if c in D.keys():
        D[c]=D[c]+D[b]
    else:
        D[c]=D[b]
    del D[b]    
    print(somme)

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 step in range(Q):
    b,c=map(int,input().split())
    A[A==b]=c
    print(sum(A))