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 = map(int, input().split())
Q = int(input())

val = []
heavy = []

for x in A:
    if x in heavy:
        val[heavy.index(x)] += 1
    else:
        heavy.append(x)
        val.append(1)

for i in range(Q):
    B, C = input().split()
    B = int(B)
    C = int(C)
    if C not in heavy:
        heavy.append(C)
        val.append(0)
    if B in heavy:
        val[heavy.index(C)] += val[heavy.index(B)]
        val[heavy.index(B)] = 0
    sum = 0
    for x, y in zip(val, heavy):
        sum += x * y
    print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lis=list(map(int,input().split()))
hoge=[0 for i in range(10**5+1)]
hogesum=[0 for i in range(10**5+1)]
for i in range(n):
  hoge[lis[i]]+=1
  hogesum[lis[i]]+=lis[i]
m=int(input())
for i in range(m):
  a,b=map(int,input().split())
  hoge[b]+=hoge[a]
  hoge[a]=0
  hogesum[a]=0
  hogesum[b]=b*hoge[b]
  print(sum(hogesum))

Submission status: Time Limit Exceeded

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

N=input().rstrip()
A=list(map(int,input().split()))
Ac = collections.Counter(A)
Q = int(input())
for i in range(Q):
    B,C = list(map(int,input().split()))
    Ac[C] += Ac[B]
    Ac[B] = 0

    ans = 0
    for key,val in Ac.items():
        ans += key*val
    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())
bc = [list(map(int, input().split())) for _ in range(Q)]

for i in range(Q):
    A[A==bc[i][0]] = bc[i][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from sys import stdin
N = int(stdin.readline().rstrip())
A = Counter([int(_) for _ in stdin.readline().rstrip().split()])
Q = int(stdin.readline().rstrip())
BC = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]
ans = []
for bc in BC:
    if bc[1] not in A:
        A[bc[1]] = A[bc[0]]
    else:
        A[bc[1]] += A[bc[0]]
    A.pop(bc[0], None)
    ans.append(sum(k*v for k, v in A.items()))
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
a = [int(x) for x in raw_input().split()]
q = int(raw_input())
op = []
for _ in xrange(q):
    v = raw_input().split()
    op.append([int(x) for x in v])

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

for src, dest in op:
    c = d.pop(src, 0)
    d[dest] = d.get(dest, 0) + c
    total = 0
    for k, v in d.iteritems():
        total += k * v
    print total

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())
#print(B,C)
#print(A)
#a = 0
#for i in range(N):
    #a += A[i]
#print(a)
for j in range(Q):
    a = 0
    for i in range(N):
        if A[i] == B[j]:
            #a = a + (C[j]-A[i])
            a += C[j]
            A[i] = C[j]
            #print(A)
        else:
            a += A[i]
            #print(A)
    #print(A)
    print(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())
#print(B,C)
#print(A)
a = 0
for i in range(N):
    a += A[i]
#print(a)
for j in range(Q):
    for i in range(N):
        if A[i] == B[j]:
            a = a + (C[j]-A[i])
            A[i] = C[j]
    #print(A)
    print(a)

Submission status: Time Limit Exceeded

Solution code:
N = raw_input()
ns = raw_input()
ns = ns.split(' ')

data = {}
for i in ns:
    i = int(i)
    if i in data:
        data[i] += 1
    else:
        data[i] = 1

Q = raw_input()
opr = []
for _ in range(int(Q)):
    tmp = raw_input()
    tmp = tmp.split(' ')
    opr.append([int(tmp[0]), int(tmp[1])])

ans = 0
for k, v in data.iteritems():
    ans += k * v
for op in opr:
    if op[0] not in data:
        print ans
        continue
    ans = 0
    old = data[op[0]]
    del data[op[0]]
    old2 = 0
    if op[1] in data:
        old2 = data[op[1]]
    data[op[1]] = old + old2
    for k, v in data.iteritems():
        ans += k * v
    print ans

Submission status: Time Limit Exceeded

Solution code:

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

val = {}

for i in s:
    if i not in val:
        val[i] = 1
    else:
        val[i] += 1

# print(val)

while q:
    c, d = map(int, input().split())
    # print(c, d)
    if c in val:
        if d in val:
            val[d] += val[c]
        else:
            val[d] = val[c]
        del val[c]
    # print(val)
    suss = 0
    for k, v in val.items():
        # print(k, v)
        suss += k *v

    print(suss)

    q -= 1

Submission status: Time Limit Exceeded

Solution code:

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


while q:
    c, d = map(int, input().split())
    # print(c, d)
    for i in range(len(s)):
        if s[i] == c:
            s[i] = d
    # print(s)
    print(sum(s))

    q -= 1

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_dict = {}
for i in a:
    if i not in a_dict:
        a_dict[i] = 1
    else:
        a_dict[i] += 1
        
for b, c in bc:
    sumA = 0
    if b in a_dict:
        bn = a_dict[b]
        a_dict[b] = 0
        if c in a_dict:
            a_dict[c] += bn
        else:
            a_dict[c] = bn
    for k, v in a_dict.items():
        sumA += k*v
    print(sumA)

Submission status: Time Limit Exceeded

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

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print(sum(s.values()))

Submission status: Time Limit Exceeded

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

s = {}

for v in a:
  if v in s:
    s[v] += 1
  else:
    s[v] = 1

for t in r:
  if t[0] in s:
    if t[1] in s:
      s[t[1]] += s[t[0]]
    else:
      s[t[1]] = s[t[0]]
    del s[t[0]]
  print(sum([i * s[i] for i in 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 b, c in bc:
    sumA = 0
    for i in range(0, n):
        if a[i] == b:
            a[i] = c
        sumA += a[i]
    print(sumA)

Submission status: Time Limit Exceeded

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

dic = [0] * (10 ** 5 + 1)
for x in A:
    dic[x] += 1

for i in range(Q):
    B, C = map(int, input().split())
    dic[C] += dic[B]
    dic[B] = 0
    sum = 0
    for j, x in enumerate(dic):
        sum += j * x
    print(sum)

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

b_arr = []
c_arr = []
for _ in range(q):
    b, c = map(int, input().split())
    b_arr.append(b)
    c_arr.append(c)

for i in range(len(b_arr)):
    a = np.where(a == b_arr[i], c_arr[i], a)
    print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = map(int, input().split())
c = Counter(A)
Q = int(input())
for _ in range(Q):
    B, Q = map(int, input().split())
    c[Q] += c[B]
    c[B] = 0
    summ = 0
    for k, v in c.items():
        summ += k * v
    print(summ)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
import fractions
import functools
import copy
from collections import deque
from functools import reduce
from decimal import Decimal


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

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
#import numpy as np
#a2=np.array(a2)
#print(a2)

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj][0]*a2[jj][1]
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  for jj in range(len(a2)):
    if a2[jj][0]==b:
      r+=(c-b)*a2[jj][1]
      a2[jj][0]=c
  print(r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=""
l=list(map(int,input().split()))
q=int(input())
for i in range(q):
    j,k=map(int,input().split())
    for i in range(len(l)):
        if l[i]==j:
            l[i]=k
    print(sum(l))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

N = int(input())
d = defaultdict(int)
A = list(map(int, input().split()))
for a in A:
    d[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    d[C] += d[B]
    del d[B]
    ans = 0
    for k, v in d.items():
        ans += k * v
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
from bisect import bisect_right, bisect_left

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
# A_sort = np.sort(A)
A_sum = sum(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    # A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    '''
    plus_num = BC[q][1] - BC[q][0]
    left = bisect_left(A_sort, BC[q][0])
    right = bisect_right(A_sort, BC[q][0])
    plus_multiple = right - left
    A_sort[left:right] = BC[q][1]
    A_sort = np.sort(A_sort)
    '''
    plus_num = BC[q][1] - BC[q][0]
    plus_multiple = np.count_nonzero(A == BC[q][0])
    A[A==BC[q][0]] = BC[q][1]

    A_sum = A_sum + plus_num*plus_multiple
    print(A_sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
from bisect import bisect_right, bisect_left

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)
A_sum = sum(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    # A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    plus_num = BC[q][1] - BC[q][0]
    left = bisect_left(A_sort, BC[q][0])
    right = bisect_right(A_sort, BC[q][0])
    plus_multiple = right - left
    A_sort[left:right] = BC[q][1]
    A_sort = np.sort(A_sort)

    A_sum = A_sum + plus_num*plus_multiple
    print(A_sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
import bisect

readline = sys.stdin.readline

# 入力にかかる最大計算量O(NlogN)：クイックソート
N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

# ここの計算量O(N(logN + logN))
for q in range(Q):
    # A = [BC[q][1] if x==BC[q][0] else x for x in A]
    A[bisect.bisect_left(A, BC[q][0]):bisect.bisect_right(A, BC[q][0])] = BC[q][1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys

readline = sys.stdin.readline

N = int(input())
A = np.array(readline().split(), np.int64)
Q = int(input())
BC = np.array([np.array(readline().split(), np.int64) for n in range(Q)], np.int64)
# BC[N][0,1]

for q in range(Q):
    A = [BC[q][1] if x==BC[q][0] else x for x in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict as dd
d = dd(int)
N = int(input())
for i in [int(n) for n in input().split()]:
  d[i] = d[i]+1
times = int(input())
for i in range(times):
  a, b = [int(n) for n in input().split()]
  d[b], d[a] = d[b] + d[a], 0
  print(sum([ key*value for key, value in d.items() if value > 0]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
an = np.array(list(map(int, input().split())))
q = int(input())
bici=[[] for i in range(q)]
for i in range(q):
    bici[i] = list(map(int, input().split()))
    an = np.where(an == bici[i][0],bici[i][1],an)
    Ans = np.sum(an)
    print(Ans)

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())
ls = []
lsans=[]
D = A
ans = 0
for z in range(N):
    ans += A[z]

for j in range(Q):
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    ans -= (B[j]-C[j])*len(ls[j])
    print(ans)
    for i in range(len(ls[j])):
        D[ls[j][i]] = C[j]
    #for i in range(len(lsans)):
    #print (lsans[i])

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())
ls = []
lsans=[]
D = A
for j in range(Q):
    ans = 0
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    for i in range(len(ls[j])):
        D[ls[j][i]] = C[j]
    for z in range(N):
        ans += D[z]
        D = A
    print(ans)
#for i in range(len(lsans)):
    #print (lsans[i])

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())
ls = []
lsans=[]
D = A
for j in range(Q):
    ans = 0
    #print(ls)
    ls.append([a for a,x in enumerate(A) if x == B[j]])
    #print(B[j])
    #print(B)
    #print([a for a,x in enumerate(A) if x == B[j]])
    for i in range(len(ls[j])):
        D[ls[j][i]] = C[j]
    for z in range(N):
        ans += D[z]
        D = A
    lsans.append(ans)
for i in range(len(lsans)):
    print (lsans[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print(sum(A))

Submission status: Time Limit Exceeded

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

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
xy = [map(int, input().split()) for _ in range(Q)]
x, y = [list(i) for i in zip(*xy)]


def replace_list(num_list):
    num_list = [y[index] if j==i else j for j in num_list]
    return num_list




for index, i in enumerate(x):
    #first_sum = sum(A)
    #c = collections.Counter(A)
    #first_sum = c[i]*(y[index]-i)+first_sum
    while i in A :
        A.remove(i)  #全てのiを削除します。
        A.append(y[index])
    first_sum = sum(A)
    print(first_sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input(""))
ain=input("").split(" ")
a=[]
for i in range(n):
    a+=[int(ain[i])]
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    listb+=[int(c[i][0])]
    listc+=[int(c[i][1])]
    if (listb[i] in a):
       
        for k in range(n):
            if (a[k]==listb[i]):
                a[k]=listc[i]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n=int(input(""))
ain=input("").split(" ")
a=[]
for i in range(n):
    a+=[int(ain[i])]
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    listb+=[int(c[i][0])]
    listc+=[int(c[i][1])]
    for k in range(n):
        if (a[k]==listb[i]):
            a[k]=listc[i]
    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())
xy = [map(int, input().split()) for _ in range(Q)]
x, y = [list(i) for i in zip(*xy)]


def replace_list(num_list):
    num_list = [y[index] if j==i else j for j in num_list]
    return num_list

for index, i in enumerate(x):
    A = replace_list(A)
    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)]
temp = A
for i in range(Q):
    temp = [BC[i][1] if a == BC[i][0] else a for a in temp]
    print(sum(temp))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
Adic={}
B=0

for i in A:
    Adic[i]=0

for i in A:
    Adic[i]+=1

for i in range(Q):
    b,c=map(int,input().split())
    
    if b not in Adic:
        for k,v in Adic.items():
            B+=k*v
        print(B)
        B=0
    else:
        
        if c in Adic:
            Adic[c]+=Adic[b]
            Adic[b]=0
        elif c not in Adic:
            Adic[c]=0
            Adic[c]+=Adic[b]
            Adic[b]=0
            
        for k,v in Adic.items():
            B+=k*v
        print(B)
        B=0

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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
        else:
            pass
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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):
    for j in range(N):
        if A[j]==B[i]:
            A[j]=C[i]
        else:
            pass
    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):
    bc = list(map(int,input().split()))
    for j in range(n):
        if a[j] == bc[0]:
            a[j] = bc[1]
    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())
B = []
C = []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import Counter
N = int(input()) 
A = Counter(map(int, input().split()))
Q = int(input())
for i in range(Q):
    sum = 0
    b, c = map(int, input().split())
    if A[b] != 0:
        A[c] += A[b]
        A[b] = 0
    for n in A:
        sum += A[n] * n
    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(str, input().split())
    if int(b) in A:
        A = [int(str(a).replace(b, c)) for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
Q = int(input())
Bs = [tuple(tuple(map(int, input().split()))) for i in range(Q)]
X = [0]
for i in range(1, 100001):
  X.append(A.count(i))
S = sum(A)
for B in Bs:
  S += X[B[0]]*(B[1]-B[0])
  X[B[1]] += X[B[0]]
  X[B[0]] = 0
  print(S)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
Q = int(input())
Bs = [[int(j) for j in input().split()] for i in range(Q)]
X = [0]
for i in range(1, 100001):
  X.append(A.count(i))
S = sum(A)
for B in Bs:
  S += X[B[0]]*(B[1]-B[0])
  X[B[1]] += X[B[0]]
  X[B[0]] = 0
  print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in A:
    ans += i

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
B = []
C = []
D = []
for i in range(q):
    ans = 0
    b, c = map(int, input().split())
    for j in range(len(A)):
        if A[j] == b:
            A[j] = c
            ans += c
        else:
            ans += A[j]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = input().split()
A = [int(c) for c in A]

Q = int(input())

i = 1

B = []
C = []

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

for j in range(Q):
    count = A.count(B[j])
    for h in range(count):
        A.remove(B[j])
    C1 = [C[j]] * count
    A.extend(C1)
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
numlista=list(map(int,input().split()))
#aの合計を求める(suma)
suma=0
for i in range(n):
  suma+=numlista[i]
#1~10000がそれぞれいくつあるのか調べる(numkosuu)
numkosuu=[]
for i in range(100000):
  numkosuu.append(numlista.count(i+1))
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
#変化したところのみ計算する
  suma+=(c-b)*numkosuu[b-1]
#個数を更新する
  numkosuu[c-1]+=numkosuu[b-1]
  numkosuu[b-1]=0
  print(suma)

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input()) # N
b=input().split(" ") # A
c=int(input()) # Q

final=[]
for i in range(c):
    t=0 # total
    d=input().split(" ")
    for i in range(len(b)):
        if int(d[0]) == int(b[i]): # replace
            b[i]=d[1]
        t+=int(b[i]) #sum
    final.append(t)
for j in range(c): # print result
    print (final[j])

Submission status: Time Limit Exceeded

Solution code:
#input the number of A and an array of A
#then from the number of input Q input number on B and C array
#using while loop to replaces number that same in A and B with C in A
#print the total sum of A on each operation

A=[]
B=[]
C=[]
N=int(input()) #O(1)
A=list(map(int,input().split())) #O(n)
Q=int(input()) #O(1)

x=0
while x<Q:
    b,c=map(int,input().split()) #O(n)
    B.append(b) #O(1)
    C.append(c) #O(1)
    x+=1

x=0  
y=0
while x<Q:
    while y<N:
        if A[y]!=B[x]: #O(1)
            y+=1
            continue
        else:
            A[y]=C[x] #O(1)
            continue

    z=0
    total=0
    while z<N:
        total=total+A[z] #O(1)
        z+=1
        
    print(total) #O(1)
    y=0
    x+=1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())  # Read N Number of integers
A = list(map(int, input().split(' ')))  # read the sequence of A
Q = int(input())   # read Q from user
def myindex(num1, num2, default=False):  # create the Function myindex 
    if num2 in num1:
        return num1.index(num2)
    else:
        return default
 
list1 = []  # create a list 
for a in range(0, Q): # for loop to get B and C
  c = list(map(int, input().split(' ')))
  t = myindex(A, c[0], -1)
  while t != -1:
    A[t] = c[1]
    t = myindex(A, c[0], -1)
  F = sum(A)
  list1.append(F) # add to the list1 
for s in list1:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())  # Read N Number of integers 
A = list(map(int,input().split()))  # read the sequence of A
Q=int(input()) # read Q from user
 
for i in range(Q):
    B,C = map(int,input().split()) # read B and C from user
    for j in range(N):
        if A[int(j)] == B:
            A[int(j)]=C
    print(sum(A)) # print the result

Submission status: Time Limit Exceeded

Solution code:
#Take value of N
#Take all value of A
#Take Q, the number of time operation occur
#Take value x and y and append it on B and C
#using while loop, replace the value of A[counter] that's equal to B with C
#calculate the sum and then print the value

N = int(input())
A = []
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

counter = 0

while counter<Q:
    x, y = map(int, input().split(" "))
    B.append(x)
    C.append(y)
    counter += 1

counter = 0
counter2 = 0

while counter2<Q:
    while counter<N:
        if A[counter] != B[counter2]:
            counter += 1
            continue
        else:
            A[counter] = C[counter2]
            continue

        
    total = 0
    finalcounter = 0
    while finalcounter<N:
        total = total + A[finalcounter]
        finalcounter += 1
    print(total)
    counter = 0
    counter2 += 1

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def list_replace(lst, old, new):
    i = -1
    try:
        while 1:
            i = lst.index(old, i + 1)
            lst[i] = new
    except ValueError:
        pass
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b,c = [],[]
for i in range(q):
    bt,ct = input().split()
    b.append(int(bt))
    c.append(int(ct))
    list_replace(a,b,c)

for i in range(q):
    list_replace(a,b[i],c[i])
    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(Q):#O(N**2)⇒TLE
    B,C = map(int,input().split())
    for j in range(N):
        if li[int(j)] == B:
            li[int(j)]=C
    print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

SS = []
for ll in range(0, Q):
  c = list(map(int, input().split(' ')))
  t = my_index(A, c[0], -1)
  while t != -1:
    A[t] = c[1]
    t = my_index(A, c[0], -1)
  FS = sum(A)
  SS.append(FS)
for s in SS:
  print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().strip().split()))[:n]
q=int(input())
i=1
s=sum(lst)
while(i<=q):
    b,c=map(int,input().split())
    count=lst.count(b)
    # print((s+(count*c))-count*b)
    if(count!=0):
        lst = list(filter((b).__ne__, lst))
        lst+=[c]*count
    print(sum(lst))
    i+=1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().strip().split()))[:n]
q=int(input())
i=1 
while(i<=q):
    b,c=map(int,input().split())
    lst=[c if x==b else x for x in lst] 
    print(sum(lst))
    i+=1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().strip().split()))[:n]
q=int(input())
i=1 
while(i<=q):
    b,c=map(int,input().split())
    for j in range(0,n):
        if(lst[j]==b):
            lst[j]=c 
    print(sum(lst))
    i+=1

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