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

n = int(input())

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

a_array = np.zeros((100001), dtype = np.int)
a_val = np.arange(100001)

for i in range(len(a)):
  a_array[a[i]] += 1

q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  a_array[c] += a_array[b]
  a_array[b] = 0
  print(np.sum(a_array*a_val))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  for j in range(N):
    if A[j] == pairs[i][0]:
      A[j] = pairs[i][1]
  ret = sum(list(map(int,A)))
  print(ret)

Submission status: Time Limit Exceeded

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

for i in range(q):
  key,rep = map(int,input().split())
  while key in num_list:
    num_list.remove(key)
    num_list.append(rep)
  print(sum(num_list))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for bc in BC:
  b = bc[0]
  c = bc[1]
  s = 0
  if b in a_c:
    a_c[c]+=a_c.pop(b)
  for k, v in a_c.items():
    s += k*v
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

    SUM = []
    for i in range(Q):
        B, C = list(map(int, input().split()))
        if i == 0:
            A = np.where(A == B, C, A)
            SUM.append(sum(A))
        else:
            count = np.count_nonzero(A == B)
            SUM.append(SUM[i-1]+(C-B)*count)
            if count == 0:
                continue
            A = np.where(A == B, C, A)

    for s in SUM:
        print(s)

Submission status: Time Limit Exceeded

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

        ans = []

        temp_s = A

        for i in range(Q):
                temp_s = [s.replace(str(BC[i][0]),str(BC[i][1])) for s in temp_s]
                temp_i = [int(s) for s in temp_s]
                ans.append(sum(temp_i))

        return ans

if __name__=="__main__":
        ans = main()

        n = len(ans)
        for i in range(n):
                print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))
ans = sum(a)
#print(ans)
q = int(input())
b = []
c = []
for i in range(q):
    bb,cc = map(int,input().split())
    b.append(bb)
    c.append(cc)
#print(b)
#print(c)
from itertools import groupby
d = [[key,len(list(group))] for key,group in groupby(sorted(a))]
#print(d)
for j in range(q):
    for i in range(len(d)):
        if b[j]==d[i][0]:
            d[i][0] = c[j]
            ans = ans + ((c[j]-b[j])*d[i][1])
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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:
from collections import Counter

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

a = Counter(a_list)
for i in range(Q):
    b, c = map(int, input().split())
    tmp = a[b]
    a[b] = 0
    a[c] += tmp
    print(sum(a.elements()))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    alis = sorted(map(int, input().split()))
    adic = {}
    for num in alis:
        if num in adic:
            adic[num] += 1
        else:
            adic[num] = 1
    
    anslis = []

    q = int(input())
    bclis = []
    for i in range(q):
        b, c = map(int,input().split())
        if b in adic:
            kazu = adic[b]
            adic[b] = 0
            if c in adic:
                adic[c] += kazu
            else:
                adic[c] = kazu
        ans = 0
        for key in adic:
            ans += adic[key] * key
        print(ans)
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
list_a=list(map(int,input().split()))
k=int(input())
list_b= [list(map(int, input().split())) for _ in range(k)]
for i in range(k):
  list_a=[list_b[i][1] if k==list_b[i][0] else k for k in list_a]
  print(sum(list_a))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        del val2ind[B]
        if C not in val2ind:
            val2ind[C] = cand
        else:
            val2ind[C] += cand
    print(sum([key*val2ind[key] for key in val2ind]))

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
dis = {}
for i in As:
  if i in dis:
    dis[i] += 1
  else:
    dis[i] = 1
Q=int(input().rstrip('\n'))
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in dis:
    if C in dis:
      dis[C] += dis[B]
      del dis[B]
    else:
      dis[C] = dis[B]
      del dis[B]
  print(sum(i*x for i,x in dis.items()))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

Amap=dict()

for i in range(N):
    if A[i] in Amap:
        Amap[A[i]]+=1
    else:
        Amap[A[i]]=1

# print(Amap)

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

    if B in Amap:
        if C in Amap:
            Amap[C] += Amap[B]
        else:
            Amap[C] = Amap[B]

    else:
        if C in Amap:
            Amap[C] += 0
        else:
            Amap[C] = 0

    Amap[B] = 0
    # print(i, B, C, Amap)


    s = 0
    for k in Amap.keys():
        s += (int(k)*Amap[k])
    ans.append(s)
    # print(s)

for s in ans:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
arr={}
for a in A:
	if a in arr:
		arr[a]+=1
	else:
		arr[a]=1
Q=int(input())
for q in range(Q):
	B,C=[int(i) for i in input().split()]
	if B in arr:
		if C in arr:
			arr[C]+=arr[B]
		else:
			arr[C]=arr[B]
		del arr[B]
	print(sum([i[0]*i[1] for i in arr.items()]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.asarray(list(map(int, input().split())))
Q = int(input())
for _ 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:
N = int(input())
list_A = [int(item) for item in input().split()]
Q = int(input())
list_bc = []
for i in range(Q):
    list_bc.append([int(item) for item in input().split()])

for i in range(Q):
    B = list_bc[i][0]
    C = list_bc[i][1]
    # list_A = [C if item == B else item for item in list_A]
    while(B in list_A):
        index = list_A.index(B)
        list_A[index] = C
    print(sum(list_A))

Submission status: Time Limit Exceeded

Solution code:
#数字の個数
N = int(input())

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

#操作の回数
Q = int(input())

B = []
C = []

set = list(set(A)) #存在する数字のリスト

#set の i 番目の数字n個数
nums = []
for n in set:
    nums.append(A.count(n))

S = sum(A)

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

for i in range(Q):
    """
        print("i",i)
        print("set,",set)
        print("nums",nums)
        print("B",B[i],"Bnum",nums[set.index(B[i])])
        print("C",C[i],"Cnum",nums[set.index(C[i])])
        """

    if set.count(B[i]) != 0:
        Bnum = nums[set.index(B[i])]
        S += (C[i]-B[i])*Bnum
    
        if set.count(C[i]) == 0:
            set.append(C[i])
            nums.append(Bnum)
        else:
            nums[set.index(C[i])] += Bnum

        nums[set.index(B[i])] = 0
    print(S)

Submission status: Time Limit Exceeded

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

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

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 i in range(Q)]
from bisect import bisect_left
# def 二分探索(リスト, 要素)
def index(lst, e):
    # Step 1. bisect_left して
    i = bisect_left(lst, e)
    
    # Step 2. 変な値を ValueError で弾いて
    if i == len(lst):
        return -1
    if lst[i] != e:
        return -1
    # Step 3. 値を返す。
    return i
def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right
A=quick_sort(A)

for i in B:
    j=1
    while j!=-1:
        j=index(A,i[0])
        if not j==-1:
            A[j]=i[1]
        A=quick_sort(A)
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import math
# from collections import Counter

# N = int(input()) # 1行, 単数
# arg = list(map(int, input().split())) # 1行, 配列
# D = Counter(arg) # 出現数カウント

# print('Yes')
# print('No')
# print()
import collections
N = int(input()) # 1行, 単数
A = list(map(int, input().split())) # 1行, 配列
Q = int(input()) # 1行, 単数
counter = collections.Counter(A)

while True:
  try:
    # ans = 0
    B,C = list(map(int, input().split())) # 1行, 配列
    # counter[C] = counter[C] + counter[B]
    # del counter[B]
    # for i in counter.keys():
    #   ans = ans + i * counter[i]
    A = [C if i == B else i for i in A]
    
    print(sum(A))
  except:
    break;

Submission status: Time Limit Exceeded

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

import collections

counter = collections.Counter(a)

result = 0
for j in range(q):
  b,c = list(map(int,input().strip().split()))
  if c in counter:
    counter[c] += counter[b]
  else:
    counter[c] = counter[b]
  counter[b] = 0
  result = 0
  for i in counter:
    result += i * counter[i]
  print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*q
for i in range(q):
    b,c=map(int,input().split())
    l[i]=(b,c)
book={}
for i in a:
    if i in book:
        book[i]+=1
    else:
        book[i]=1

for i in range(q):
    ans=0
    if l[i][0] in book:
        if l[i][1] in book:
            book[l[i][1]]+=book[l[i][0]]
            book[l[i][0]]=0
        else:
            book[l[i][1]]=book[l[i][0]]
            book[l[i][0]]=0
    for j in book:
        ans+=book[j]*j
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
a = input()
sequence_integer = [int(idx) for idx in input().split()]
num = int(input())
for idx in range(num):
  a, b = [int(idx) for idx in input().split()]
  while a in sequence_integer:
    sequence_integer[sequence_integer.index(a)] = b
  print(sum([int(idx) for idx in sequence_integer]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]


    Adict = dict()
    for a in A:
        Adict[a] = Adict.get(a, 0) + 1
    
    for b, c in BC:
        if b in Adict:
            tmp = Adict[b]
            Adict[c] = Adict.get(c, 0) + tmp
            Adict.pop(b)
        
        goukei = sum([k*v for k, v in Adict.items()])
        
        print(goukei)


if __name__ == '__main__':
    main()

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

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

Submission status: Time Limit Exceeded

Solution code:
data = {}
N = int(input())

for a in input().split():
    a = int(a)
    data[a] = data.get(a, 0) + 1

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if data.get(b):
        data[c] = data.get(c, 0) + data.pop(b)
    print(sum([k * v for k, v in data.items()]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = input()
N = int(n)
A = [int(a) for a in input().split()]
q = input()
Q = int(q)
B, C = [], []
for i in range(Q):
    b, c = input().split()
    B.append(int(b))
    C.append(int(c))


my_dict = Counter(A)

def cals_sum(my_dict):
    sum = 0
    for key in my_dict.keys():
        value = my_dict[key]
        sum += int(key) * int(value)
    return sum

for i in range(len(B)):
    b = B[i]
    c = C[i]
    tmpb = my_dict[b] 
    my_dict[b] = 0
    tmpc = my_dict[c]
    my_dict[c] = tmpb + tmpc

    print(cals_sum(my_dict))

Submission status: Time Limit Exceeded

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

for q in range(Q):
  b, c = list(map(int, input().split()))
  a_list = [c if x==b else x for x in a_list]
  print(sum(a_list))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_number = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_number:
        numbers[num] += 1

    Q = int(input())

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers[a] = 0
        print(sum([v*k for (k, v) in numbers.items()]))
    
main()

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:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
Q= int(input())
for q in range(Q):
    b, c = map(int, input().split())
    dic[c]+=dic[b]
    dic[b]=0
    ans = 0
    for j in dic:
        ans += dic[j]*j
    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())
    A = [C if i == B else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def Replacing():

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

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

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        value_A = [value_B[1] if j == value_B[0] else j for j in value_A]

        print(sum(value_A))

if __name__ == '__main__':
    Replacing()

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
a = list(map(int, input().split()))
a_c = collections.Counter(a)
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
S = [0]*Q

for i, bc in enumerate(BC):
  b = bc[0]
  c = bc[1]
  s = 0
  if b in a_c:
    a_c[c]+=a_c.pop(b)
  for k, v in a_c.items():
    s += k*v
  S[i] = s

[print(i) for i in S]

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
N=int(sys.stdin.readline())
A=list(map(int,sys.stdin.readline().split()))
Q=int(sys.stdin.readline())
A=sorted(A)
A=np.array(A)
b=[]
c=[]
for _ in range(Q):
  B,C=map(int,sys.stdin.readline().split())
  b.append(B)
  c.append(C)
for i in range(Q):
  A[A==b[i]]=c[i]
  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[i] == b else a[i] for i in range(len(a))]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
def main():
  # Xはもっとも近い整数，Nは与えられるP
  #X,N = map(int, input().split())
  N = input()

  # listを取得
  arr = list(map(int, input().split()))

  Q = int(input())

  for i in range(Q):
    li = list(map(int, input().split()))
    one = li[0]
    two = li[1]
    indexes = [j for j, x in enumerate(arr) if x == one]
    for k in range(len(indexes)):
      arr[indexes[k]] = two
    print(sum(arr))

if __name__ == '__main__':
    main()

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):
    ans=0
    b, c=map(int,input().split())
    for j in range(n):
        if a[j] == b:
            a[j]=c
        ans+=a[j]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
input()
math_dic = collections.Counter((map(int,input().split())))
num = int(input())
for i in range(num):
    a,b = map(int,input().split())
    if a not in math_dic:
        print(sum([index*value for index,value in math_dic.items()]))
        continue
    if b not in math_dic:
        math_dic[b] = 0
    math_dic[b] += math_dic.pop(a)
    print(sum([index*value for index,value in math_dic.items()]))

Submission status: Time Limit Exceeded

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


def aaa(n):
    if n == B:
        return C
    else:
        return n


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

for i in range(q):
    (b, c) = l[i] 
    dic = Counter(a)
    for i in range(dic[b]):
        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())
BC=[]
for i in range(Q):
    BC.append(list(map(int, input().split())))

print()
for i in range(Q):
    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:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
a = li()
a.sort()

s = sum(a)

q = ii()

from bisect import bisect_left, bisect_right
def bisect_find(a, x):
    i = bisect_left(a, x)
    if i == len(a):
        return -1
    elif a[i] != x:
        return -1
    return i

def bisect_r_find(a, x):
    i = bisect_right(a, x)
    # if i == len(a):
    #     return -1
    return i

for i in range(q):
    b,c = mi()


    # 左を探す
    l = bisect_find(a,b)
    # 右を探す
    if l != -1:
        r = bisect_r_find(a,b)
        # 個数をカウント
        count = r-l
        # 前回の合計との差を求める
        s = s + (c-b)*count

        # 配列を上書き
        a[l:r] = [c]*count
        a.sort()
    # print('-------------ans----------------',s)
    print(s)

Submission status: Time Limit Exceeded

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

res = [0 for _ in range(10 ** 5 + 1)]
for a_i in a:
    res[a_i] += 1

for b_i, c_i in zip(b, c):
    num = res[b_i]
    res[b_i] = 0
    res[c_i] += num

    ans = 0
    for i in range(len(res)):
        ans += i * res[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 _ 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())

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    nums = [(c if num == b else num) for num in nums]
    ans.append(sum(nums))

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

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

    MAX = 10 ** 5 + 10
    k = [0] * MAX
    # v = [0] * MAX

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

    # print(k[:10])

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b=[]
c=[]
sum=0
for i in range(n):
    sum+= a[i]
for i in range(q):
    bb,cc = map(int,input().split())
    b.append(bb)
    c.append(cc)
a.sort()

for i in range(q):
    l= bisect.bisect_left(a,b[i])
    h= bisect.bisect_right(a,b[i])
    for j in range(l,h):
        a[j]=c[i]
    a.sort()
    sum=sum + (c[i]-b[i])*(h-l)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
INF = 1000000000000

import bisect

n = I()
a = LI()
a.sort()
q = I()
val = sum(a)
for i in range(q):
    b,c = LI()
    l = bisect.bisect_left(a,b)
    r = bisect.bisect_right(a,b)
    val += (c-b)*(r-l)
    for j in range(l,r):
        a[j] = c
    a.sort()
    print(val)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [list(map(int, input().split())) for _ in range(Q)]
def change(A, B, C):
    l = my_index_multi(A, B)
    for i in l:
        A[i] = C
    return A
    
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


for i in range(0, Q):
    if B[i][0] in A:
        A = change(A, B[i][0], B[i][1])
        print(sum(A))
    else:
        print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
b, c = map(int, input().split())
BC = [b,c]
for i in range(Q - 1):
    b, c = map(int, input().split())
    BC = np.vstack((BC,[b,c]))
for i in range(Q):
    for j in range(len(A)):
        if A[j] == BC[i,0]:
            A[j] = BC[i,1]
    print(np.sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []

for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)
    result = 0
    for num in list(numset):
        result += numcount[num]*num
    anslist.append(result)

for ans in anslist:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    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 q in range(Q):
        b,c = map(int,input().split())
        A = [c if a == b else a for a in A]
        print(sum(A))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


for _ in range(q):
    mo = input().split()
    b = mo[0]
    c = mo[1]
    cnt = 0
    for h in range(n):
        if a[h] == b:
            a[h] = c
        cnt += int(a[h])
    print(cnt)

Submission status: Time Limit Exceeded

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

data = {}

for i in range(len(A)):
    a = A[i]
    if a in data.keys():
        data[a].append(i)
    else:
        data[a] = [i]


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

    temp = data.get(B)

    if temp is None:
        print(sum(A))
        continue

    for j in temp:
        A[j] = C

    if C in data.keys():
        data[C].extend(temp)
        del data[B]
    else:
        data[C] = data[B]

        del data[B]

    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

        ans = []

        temp_A = A

        for i in range(Q):
                temp_s = [s.replace(str(BC[i][0]),str(BC[i][1])) for s in temp_A]
                temp_i = [int(s) for s in temp_s]
                ans.append(sum(temp_i))
                temp_A = temp_s

        return ans

if __name__=="__main__":
        ans = main()

        n = len(ans)
        for i in range(n):
                print(ans[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    ac = Counter(a)
    acl = defaultdict(int)
    for ae in ac:
        acl[ae] = ac[ae]

    for _ in range(q):
        b, c = map(int, input().split())
        rmed = acl[b]
        acl[c] += rmed
        acl[b] = 0
        r = sum([i * v for i, v in acl.items()])
        print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

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

for id in range(q):
    query = list(map(int, input().split()))
    for i in range(n):
        if int(a_list[i]) == int(query[0]) :
            a_list[i] = int(query[1])

    print(sum(a_list))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

for i in range(q):
    b,c = map(int,input().split())
    indexes = [i for i, x in enumerate(a_li) if x == b]
    for i in indexes:
        a_li[i] = c
    print(sum(a_li))

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 = [int(i) for i in input().split()]
q = int(input())
b = []
for _ in range(q):
    b.append([int(i) for i in input().split()])


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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
num=list(map(int,input().split()))
add=sum(num)
q=int(input())
for i in range(q):
    cnt=0
    b,c=map(int,input().split())
    num.sort()
    for i in range(len(num)):
        if num[i]==b:
            num[i]=c
            cnt+=1
        elif num[i]>b:
            break
    add+=(c-b)*cnt
    print(add)