Problem:


Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Time Limit Exceeded

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

sum = sum(a)

for i in bc:
    b = i[0]
    c = i[1]
    count = a.count(b)
    a = [c if i==b else i for i in a]
    sum += count*(c-b)
    # print(a)
    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 i==b else i for i in A]
  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())
ans=sum(a)
for _ in range(q):
  b,c=map(int,input().split())
  i=bisect.bisect_left(a,b)
  j=bisect.bisect_right(a,b)
  k=j-i
  ans=(c-b)*k+ans
  print(ans)
  a=sorted(a[:i]+a[j:]+[c]*k)

Submission status: Time Limit Exceeded

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

change_pair = []
for i in range(Q):
    b, c = map(int, input().split())
    change_pair.append((b, c))

# Aの出現値毎に回数をカウント
counts = {}
for a in A:
    if a in counts:
        counts[a] += 1
    else:
        counts[a] = 1

# 置き換え毎に回数を移動
for b, c in change_pair:
    if b in counts:
        count = counts[b]
        counts[b] = 0
    else:
        count = 0
    if c in counts:
        counts[c] += count
    else:
        counts[c] = count

    # 総和を求める
    total = 0
    for a, count in counts.items():
        total += a * count
    print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
list_q = []
dic_a={}
for i in range(q):
    b, c = (int(x) for x in input().split())
    list_q.append((b,c))
for i in a:
    if i not in dic_a:
        dic_a[i]=1
    else:
        dic_a[i]+=1
for i in range(q): 
    ans = 0
    b = list_q[i][0]
    c = list_q[i][1]
    if b in dic_a:
        if c in dic_a:
            dic_a[c]+=dic_a[b]
        else:
            dic_a[c]=dic_a[b]
        dic_a[b]=0
    for i in dic_a.keys():
        ans += (i*dic_a[i])
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
  sum = 0
  for j in range(n):
    if bc[i][0] == a[j]:
      sum += bc[i][1]
      a[j] = bc[i][1]
    else:
      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())
  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().strip())
A = [int(x) for x in input().strip().split(" ")]
Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    A = [C if a == B else a for a in A]
    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)]

x = Counter(a)
for b, c in bc:
    x[c] += x[b]
    x[b] = 0

    ans = 0
    for key, value in x.most_common():
        ans += key * value
    print(ans)

Submission status: Time Limit Exceeded

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

Q=int(input())
comb=[]
for i in range(Q):
    comb.append(list(map(int,input().split())))

ra=len(A)

sum_num=[0 for i in range(Q)]
for i in range(Q):
    A=[comb[i][1] if A[j]==comb[i][0] else A[j] for j in range(ra)]
    sum_num[i]=sum(A)

for i in sum_num:
    print(i)

Submission status: Time Limit Exceeded

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

    SUM = []
    for i in range(Q):
        B, C = list(map(int, input().split()))
        A = [C if a == B else a for a in A]
        SUM.append(sum(A))

    for s in SUM:
        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):
    x,y = map(int, input().split())
    a = [y if b == x else b for b in a]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(q):
  s=list(map(int, input().split()))
  np.place(a, a==s[0], s[1])
  print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
bcs = []
for i in range(Q):
  j,k = map(int,input().split())
  bcs.append([j,k])
cnts = [0]*100000
for a in A:
  cnts[a-1]+=1
for b,c in bcs:
  t = cnts[b-1]
  cnts[b-1]=0
  cnts[c-1]+=t
  s = 0
  for i in range(1,100001):
    s += cnts[i-1]*i
  print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
n_data = {i: A.count(i) for i in range(1, 10 ** 5 + 1)}
for _ in range(Q):
  b, c = map(int, input().split())
  n_data[c] += n_data[b]
  n_data[b] = 0
  print(sum([k * v for k, v in n_data.items()]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[tuple(map(int,input().split())) for _ in range(q)]
d={}
for ai in a:
    if ai not in d:
        d[ai]=0
    d[ai]+=1
# print(d)
for bi,ci in bc:
    if bi in d:
        tmp=d[bi]
        d[bi]=0
        if ci not in d:
            d[ci]=0
        d[ci]+=tmp
    ans=0
    for key in d:
        ans+=key*d[key]
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  A_string = ",".join(A)
  A_new = A_string.replace(B[i],C[i])
  A =  A_new.split(",")
  int_A = [int(n) for n in A]
  print(sum(int_A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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] in A:
        list = [j for j, x in enumerate(A) if x == B[i]]
        for j in range(len(list)):
            A[list[j]] = C[i]
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

    resultList.append(sum(NumList))

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

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  B,C = map(int,input().split())
  if B in A:
  	D = A[A.index(B)]
  	while D in A:
  		A[A.index(B)] = 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().strip().split()))
q = int(input())
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]

for i in range(q):
    for aa in range(n):
        if(a[aa]==b[i]):
            a[aa]=c[i]
    print(sum(a))

Submission status: Time Limit Exceeded

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


num = int(input())
for i in range(num):
    B, C = map(int, input().split())
    A = [C if i == B else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
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 range(0, len(b)):
  if b[i] in arr:
      for j in arr:
          if j == b[i]:
              ind = arr.index(b[i])
              arr[ind] = c[i]
  print(sum(arr))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(q):
  s=list(map(int, input().split()))
  np.place(a, a==s[0], s[1])
  print(sum(a))

Submission status: Time Limit Exceeded

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

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

for i in range(q):
    A = [C[i] if a == B[i] else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())

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

q = int(input())

num_dict = dict(collections.Counter(a))
num_dict = {key : key * value for key, value in num_dict.items()}

for _ in range(q):
    b, c = map(int,input().split())

    num_dict[c] = int((c * num_dict.get(b, default=0) / b) + num_dict.get(c, default=0))

    if num_dict.get(b, default=0) != 0:
        del(num_dict[b])

    print(sum(num_dict.values()))

Submission status: Time Limit Exceeded

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

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

#[真の値 if 条件式 else 偽の値 for 任意の変数名 in 元のリスト]

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)]
sum = 0
for i in range(q):
    for j in range(n):
        if a[j] == bc[i][0]:
            a[j] = bc[i][1]
    for k in range(n):
        sum += a[k]
    print(sum)
    sum = 0

Submission status: Time Limit Exceeded

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

for i in range(Q):
  A = [str(s).replace(str(k[i][0]),str(k[i][1])) for s in A]
  A = [int(s) for s in A]
  print(sum(A))

Submission status: Time Limit Exceeded

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

aDict = dict()
for aElement in A:
    if aElement in aDict:
        aDict[aElement] += 1
    else:
        aDict[aElement] = 1

for b, c in zip(B, C):
    if b in aDict:
        if c in aDict:
            aDict[c] += aDict.pop(b)
        else:
            aDict[c] = aDict.pop(b)
    print(sum([k * v for k, v in aDict.items()]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
Bs = []
for i in range(Q):
    Bs.append(list(map(int, input().split(' '))))
    
    
A_list = [0]*(10**5+1)
for i in A:
    A_list[i] += 1
    
for B in Bs:
    b, c = B
    A_list[c] += A_list[b]
    A_list[b] = 0
    print(sum([i*num for num, i in enumerate(A_list)]))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    B, C = [], []
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))
        B.append(b)
        C.append(c)

    for i in range(q):
        summ = 0
        for j in range(n):
            if A[j] == B[i]:
                A[j] = C[i]
            summ += A[j]
        print(summ)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as c

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

d=c(a)

for _ in range(q):
  b,c=map(int,input().split())
  num=d.pop(b,0)
  d.setdefault(c,0)
  d[c]+=num
  
  s=0
  for k,v in d.items():
    s+=k*v
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from decimal import Decimal
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
#print(N,A,Q,BC)
for i in range(Q):
    b,c = BC[i]
    #print(b,c,A)
    for i,a in enumerate(A):
        if a > b:
            break
        elif a == b:
            A[i] = c

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

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 BC:
    for j in range(N):
        if A[j]==i[0]:
            A[j]=i[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
As = list(map(int, input().split()))
Q = int(input())
BCs = [list(map(int, input().split())) for _ in range(Q)]

#rint(BCs)

ansArray = np.zeros(10**5 + 1, dtype = int)
indexArray = np.array(range(10**5+1))

for A in As:
    ansArray[A] += 1

#print(ansArray)
#print(indexArray)

for BC in BCs:

    ansArray[BC[1]] += ansArray[BC[0]]
    ansArray[BC[0]] = 0

    print(np.dot(ansArray, indexArray))

Submission status: Time Limit Exceeded

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

for i in range(q):
    for aa in range(n):
        if(a[aa]==b[i]):
            a[aa]=c[i]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    counter = collections.Counter(a)
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        cnt = counter[b]
        del counter[b]
        counter[c] += cnt
        ans = 0
        for key in counter:
            ans += key * counter[key]
        print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from decimal import Decimal
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
#print(N,A,Q,BC)
for i in range(Q):
    b,c = BC[i]
    #print(b,c,A)
    for i,a in enumerate(A):
        if a > b:
            break
        if a == b:
            A[i] = c

    #A = [c if a == b else a for a in A]
    #print(A)
    A.sort()
    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 _ in range(q)]

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

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n=int(input())
    r=range
    a=list(map(int, input().split()))
    q=int(input())
    
    for _ in r(q):
        bi, ci = map(int, input().split())
        c=0
        for i in r(n):
            if a[i] == bi:
                a[i]=ci
            c+=a[i]
        print(c)


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    x=sum(a)
    p=0
    for i in range(len(a)):
        y=a[i]
        if y==b:
            a[i]=c
            p+=1
    print(x+(c-b)*p)

Submission status: Time Limit Exceeded

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

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

for i in range(q):
    a = [c[i] if x == b[i] else x for x in a]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
arr = list(map(int, input().split()))
Sum = sum(arr)
t1 = int(input())
ans = []
for i in range(t1):
  b,c = list(map(int, input().split()))
  arr = [c if x==b else x for x in arr]
  ans.append(sum(arr))
  
for i in range(len(ans)):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
member = [0]*(10**5+1)

li = list(map(int,input().split()))
for i in range(n):
  member[li[i]] +=1

q = int(input())
for j in range(q):
  x,y = map(int,input().split())
  member[y]+=member[x]
  member[x]=0
  total = 0
  for k in range(10**5+1):
    total += k*member[k]
  print(total)

Submission status: Time Limit Exceeded

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

list_list = []
for i in range(Q):
  list_ =  list(map(int, input().split()))
  list_list.append(list_)
  
  
for i in range(Q):
  for j in range(N):
    if(A_list[j] == list_list[i][0]):
      A_list[j] = list_list[i][1]
  print(sum(A_list))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy

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

a=int(input())

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
As = list(map(int, input().split()))
Q = int(input())
BCs = [list(map(int, input().split())) for _ in range(Q)]

#rint(BCs)

ansArray = np.zeros(10**5 + 1, dtype = int)
indexArray = np.array(range(10**5+1))

for A in As:
    ansArray[A] += 1

#print(ansArray)
#print(indexArray)

for BC in BCs:
    B = BC[0]
    C = BC[1]

    tmp = ansArray[B]
    ansArray[B] = 0
    ansArray[C] += tmp

    print(np.dot(ansArray, indexArray))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0 for _ in range(Q)]
C = [0 for _ in range(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(np.sum(A))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = input()
inp = map(int, input().split())
dic = dict(collections.Counter(inp))
wa = 0
for i in range(len(dic)):
    wa += list(dic.keys())[i] * list(dic.values())[i]
se = set(dic.keys())
for i in range(int(input())):
    a, b = map(int, input().split())
    if a in se:
        wa += dic[a] * (b-a)
        if b in se:
            pass
        else:
            dic[b] = 0
        se.add(b)
        dic[b] += dic[a]
        dic[a] = 0
    print(wa)

Submission status: Time Limit Exceeded

Solution code:
def sums(l):
	s = 0
	for v in l:
		s += v
	return s

n = int(input())
a = list(map(int, input().split()))
q = int(input())
for _ in range(q):
	b, c = map(int, input().split())
	while b in a:
		a[a.index(b)] = c

	print(sums(a))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')

import numpy as np



def main():
    N = I()
    A_list = LI()
    Q = I()
    act_list = LLIN(Q)
    np_array = np.array(A_list)
    for z in act_list:
        # A_list = [z[1] if i == z[0] else i for i in A_list]
        np.place(np_array, np_array == z[0], z[1])
        print(sum(np_array))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
  n=int(input())
  #A=list(map(int,input().strip("\n").split()))
  A=input().strip("\n")
  q=int(input())
  Ans=[]
  for i in range(q):
    #b,c=map(int,input().strip("\n").split())
    b,c=input().strip("\n").split()
    A=A.replace(b,c)
    Ans.append(eval(A.replace(" ","+")))
  for ans in Ans:
    print(ans)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
l=[]
for _ in range(Q):
    l.append(list(map(int,input().split())))
for i in range(Q):
    if l[i][0] in A:
        index_num = [n for n, v in enumerate(A) if v == l[i][0]]
        for j in index_num:
            A[j]=l[i][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 i in range(q):
  # 数値の変換
  for j in range(n):
    if a[j] == bc[i][0]:
      a[j] = bc[i][1]
  print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    B, C = map(int, input().split(" "))
    while B in A:
        A.remove(B)
        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())
result = []
for i in range(q):
    b, q = list(map(int, input().split()))
    a = [q if x == b else x for x in a]
    result.append(sum(a))
for r in result:
    print(r)

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())
for i in range(q):
    b, c = map(int, input().split())
    a = np.where(a == b, c, a)
    print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect

def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def LS(): return [list(x) for x in sys.stdin.readline().split()]


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


sys.setrecursionlimit(1000000)
mod = 1000000007


def solve():
    n = I()
    a = LI()
    q = I()
    li = LIR(q)
    def my_index_multi(l, x):
        return [i for i, _x in enumerate(l) if _x == x]
    for i in range(q):
        ch = my_index_multi(a, li[i][0])
        if len(ch) == 1:
            a[ch[0]] = li[i][1]
        else:
            for j in range(len(ch)):
                a[ch[j]] = li[i][1]
        print(sum(a))
    return


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
A_replace = A
for i in range(Q):
  b,c = (int(x) for x in input().split())
#  print(b,c,A)
  A_replace = [c if i == b else i for i in A_replace]
#  print(A_replace)
  print(sum(A_replace))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    for _ in range(Q):
        b, c = map(int, input().split())
        while b in A:
            A.remove(b)
            A.append(c)
        print(sum(A))
resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(input())
for a in A:
  l[a] += a

for _ in range(Q):
  b,c = map(int,input().split())
  l[c] += (l[b]//b)*c
  l[b] = 0
  print(sum(l))