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 = np.array([int(a) for a in input().split()])
q = int(input())
bc = np.array([[int(a) for a in input().split()] for j in range(q)])

u, c = np.unique(a, return_counts=True)
dica = dict(zip(u, c))
#print(dica)

keys = dica.keys()

for i in range(q):
  t1, t2 = bc[i]
  if not t1 in keys:
    dica[t1] = 0
    
  if not t2 in keys:
    dica[t2] = 0


for i in range(q):
  t1, t2 = bc[i]
  dica[t2] = dica[t1] + dica[t2]
  dica[t1] = 0
  print(sum([ k*dica[k] for k in dica.keys()]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
A=list(map(int,(input().split())))
npArray = np.array(A)
npArray=npArray.copy()
Q=int(input())
B=[]
C=[]
ans=0
for _ in range(Q):
  L, R = map(int,(input().split()))
  B.append(L)
  C.append(R)

for i in range(Q):
  upNpArray = np.where(npArray == B[i], C[i], npArray)
  print(np.sum(upNpArray))
  npArray=upNpArray.copy()

Submission status: Time Limit Exceeded

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

total = sum(A)

for i in range(Q):
    if(B[i] in A):
        count = A.count(B[i])

        d = C[i] - B[i]
        total += count * d
        print(total)

        A = [C[i] if x == B[i] else x for x in A]
    else:
        print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    B, C = map(str, input().split())
    if B not in A:
        INT = list(map(int, A))                
        print(sum(INT))
    else:
        Ast = ",".join(A)
        Ast_new = Ast.replace(B, C)
        A = Ast_new.split(',')
        INT = list(map(int, A))                
        print(sum(INT))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

for i in range(0, Q):
    BC = list(map(int, input().split()))
    AAA = [BC[1] if m == BC[0] else m for m in AAA]
    print(sum(AAA))

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())
    
    if b in a:
        index = [i for i, x in enumerate(a) if x == b]
        
        if len(index) == n:
            a = [c] * n
        else:  
            for i in index:
                a[i] = c

    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=map(int,input().split())
Q=int(input())
numbers=[]
for _ in range(Q):
    B,C=map(int,input().split())
    numbers.append([B,C])
answers=[0 for _ in range(1000001)]
for a in A:
    answers[a]+=1
for number in numbers:
    suuji=answers[number[0]]
    answers[number[0]]=0
    answers[number[1]]+=suuji
    answer=0
    n=0
    for a in answers:
        answer+=n*a
        n+=1
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
bc=[map(int, input().split()) for _ in range(Q)]
b,c=[list(i) for i in zip(*bc)]

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:
#import math
#import numpy as np
#import torch
#import torch.nn as nn
#import matplotlib.pyplot as plt
#from decimal import Decimal
#import itertools


N=int(input())
#N,K = map(int,input().split())
#B,W = map(int,input().split())
#T=int(input())
A = list(map(int,input().split()))
#S=str(input())
#A=[p for p in range(2*10**5)]
Q=int(input())
l=[]
for i in range(Q):
    #print(A)
    B,C = map(int,input().split())
    a=True
    while a==True:
        try:
            ind=A.index(B)
            A[ind]=C
            #print(A)
        except:
            a=False
    #print(A)
    l.append(sum(A))
for i in l:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
x = 0
count = Counter(a)
q = int(input())
b,c = [],[]
for i in range(q):
    bi,ci = map(int,input().split())
    b.append(bi),c.append(ci)
for i in range(q):
    bi,ci = b[i],c[i]
    count[ci]+=count[bi]
    count[bi] = 0
    x = 0
    for k,v in count.items():
        x+=k*v
    print(x)

Submission status: Time Limit Exceeded

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

b = []
c = []
ret = []

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

    idx = [x for x,v in enumerate(a) if v == b[i]]
    for x in idx:
        a[x] = c[i]
    ret += [str(sum(a))]

print('\n'.join(ret))

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]
    counter[B] = 0
    for i in counter.keys():
      ans = ans + i * counter[i]
  # A = [C if i == B else i for i in A]
    print(ans)
  except:
    break;

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

A=sorted(A)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(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 i in range(Q):
    B,C = map(int,input().split())
    ai = A.count(B)
    #print(ai)

    if B in A:
        for k in range(ai):
            A.append(C)
        for l in range(ai):
            A.remove(B)

    #print(A)
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    k = 0
    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 sys
from collections import Counter

input = sys.stdin.readline


def S():
    return input().rstrip()


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


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


def print_sum(d):
    s = 0
    for k, v in d.items():
        s += k * v
    print(s)


n = I()
al = list(MI())
q = I()

a = Counter(al)

for i in range(q):
    b, c = MI()
    if b in a.keys():
        v = a.pop(b)
        if c in a.keys():
            a[c] += v
        else:
            a[c] = v

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

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())	
A_list = list(map(int,input().split()))
Q = int(input())
result = []
for i in range(Q):
        B, C = map(int,input().split())
        A_list = [C if A == B else A for A in A_list]
        result.append(sum(A_list))

for i in result:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
def main():
    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))


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
s = 0
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]
    s +=a[j]
  print(s)
  s=0

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 = 0
        for acle in acl:
            r += acle * acl[acle]
        print(r)


if __name__ == '__main__':
    main()

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()]
Q=int(input().rstrip('\n'))
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in As:
    cha = [i for i, x in enumerate(As) if x == B]
    for i in cha:
      As[i] = C
  print(sum(As))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N=input()
AList = list(map(int, input().split()))
Q=int(input())
BCList=[list(map(int, input().split())) for i in range(Q)]

for i in range(Q):
    for j in range(len(AList)):
        if AList[j]==BCList[i][0]:
            AList[j]=BCList[i][1]
        
    print(sum(AList))

Submission status: Time Limit Exceeded

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

import collections
collect = collections.Counter(a)
#print(collect)

for i in bc:
    #if i[0] in collect.keys():
    if i[1] in collect.keys():
        collect[i[1]] += collect.pop(i[0],0)
    else:
        collect[i[1]] = collect.pop(i[0],0)
    ans=0
    for k in collect.keys():
        ans += (k * collect[k])
    print(ans)

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 = []

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

X = np.array([0]*(10**5))
Y = np.array([i  for i in range(1, (10**5)+1)])

for j in range(N):
    X[A[j]-1] += 1

for i in range(Q):
    a = X[B[i]-1]
    X[B[i]-1] = 0
    X[C[i]-1] += a
    print(X@Y)

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())
A = list(map(int, input().split()))
val2ind = {}
for i, one in enumerate(A):
    if one not in val2ind:
        val2ind[one] = []
    val2ind[one].append(i)

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:
            cand.extend(val2ind[C])
            val2ind[C] = list(set(cand))
    print(sum([key*len(val2ind[key]) for key in val2ind]))

Submission status: Time Limit Exceeded

Solution code:
import re
N = int(input())
A = input()
Q = int(input())
for i in range(Q):
    B,C = [str(x) for x in input().split()]
    A = re.sub(B,C,A)
    results = [int(x) for x in A.split()]
    print(sum(results))

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):
    a.sort()
    count = 0 
    for j in range(n):
        if b[i]  == a[j]:
            a[j] = c[i]
            count = 1
        else:
            if count == 1:
                break
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
import bisect
def index(a, x):
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

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

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

alist = sorted(alist)

for i in range(q):
    
    b = bclist[i][0]
    c = bclist[i][1]
    try:
        ind = index(alist,b)
        while alist[ind] == b:
            alist.remove(b)
            bisect.insort(alist, c)
            if b > c:
                ind+=1

    except:
        None
    print(sum(alist))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A = np.array(A)
    ans = sum(A)
    for i in range(q):
        b,c = map(int, input().split())
        A[A==b] = c
        print(sum(A))

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

for i in range(Q):
    A_replace = [C[i] if a == B[i] else a for a in A]

    A = A_replace
    S[i] = sum(A_replace)

for i in range(Q):
    print(S[i])

Submission status: Time Limit Exceeded

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

from collections import Counter
a = Counter(A)
x = 0

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

    s = sum(item*a[item] for item in a if item!=0)
    # s = 0
    # for i,v in a.items():
    #     if v != 0:
    #         s += i*v
    print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    alis = list(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())
a = list(map(int,input().split()))
q = int(input())
a_dic = {}

for elem in a:
    if elem not in a_dic:
        a_dic[elem] = 1
    else:
        a_dic[elem] += 1

for i in range(q):
    b, c = map(int,input().split())
    if b in a_dic:
        if c in a_dic:
            a_dic[c] += a_dic[b]
            del a_dic[b]
        else:
            a_dic[c] = a_dic[b]
            del a_dic[b]
    ans = 0
    for ai in a_dic:
        ans += ai * a_dic[ai]
    print(ans)

Submission status: Time Limit Exceeded

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

r = [0] * (10**5)

for a in a_list:
    r[a-1] += a

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

Submission status: Time Limit Exceeded

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

def main():
    from collections import Counter

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

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

        print(sum(A_Counter.elements()))


if __name__ == "__main__":
    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:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
ans=[0]*(10**5+1)
for i in a:
  ans[i]+=1
for _ in range(q):
  b,c=map(int,input().split())
  ans[c]+=ans[b]
  ans[b]=0
  s=0
  for i,x in enumerate(ans):
    s+=i*x
  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:
            A = np.where(A == B, C, 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())
B = []
C = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)

total = sum(A)

for i in range(Q):
    count = A.count(B[i])

    d = C[i] - B[i]
    total += count * d
    print(total)

    A = [C[i] if x == B[i] else x for x in A]

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        B[i], C[i]  = map(int, input().split())
    #print(A_list)
    ans_list = []
    for i in range(Q) :
        val_list = my_index_multi(A_list,B[i])
        #print(val_list)
        for j in val_list :
            #print(A_list[j])
            #print(B[i])
            #print(C[i])
            A_list[j] = C[i]
            #A_list[j].replace(str(B[i]), str(C[i]))
        #print(A_list)
        print(sum(A_list))
    #print(val_list)
    #A_list.replace(B[0],C[0])

    #A_list = list(map(int, A_list))
    #print(sum(A_list))
    #print(B)
    #print(C)

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]
S = 0

for Q_count in range(Q):
    if B[Q_count] in A:

        for N_count in range(N):
            if A[N_count] == B[Q_count]:
                A[N_count] = C[Q_count]
            S = S + A[N_count]

    else:
        S = sum(A)

    print(S)
    S = 0

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
List = list(map(str, input().split()))
Q = int(input())
x = [0] * Q
y = [0] * Q
for i in range(Q):
    x[i], y[i] = map(str, input().split())
    y[i]=int(y[i])

wa=0
for i in List:
    wa+=int(i)

for i in range(Q):
    c =Counter(List)
    num=c['{}'.format(x[i])]
    wa+=num*(y[i]-int(x[i]))
    List=[s.replace('{}'.format(x[i]), '{}'.format(str(y[i]))) for s in List]
    print(wa)

Submission status: Time Limit Exceeded

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

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

for i in range(q):
    cnt = 0
    for h in range(n):
        if a[h] == b[i]:
            a[h] = c[i]
        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())
a_tmp = a

for i in range(q):
  b, c = map(int, input().split())
  a_tmp2 = [c if j == b else j for j in a_tmp]
  ans = sum(a_tmp2)
  a_tmp = a_tmp2
  print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq_c = input()
Q = int(input())
for i in range(Q):
    bi, ci = input().split()
    seq_c = seq_c.replace(bi,ci)
    seq = list(map(int,seq_c.split()))
    print(sum(seq))

Submission status: Time Limit Exceeded

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

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

for op in opelates:
    pre = op[0]
    nex = op[1]
    for i in range(n):
        if ali[i] == pre:
            ali[i] = nex
    print(sum(ali))

Submission status: Time Limit Exceeded

Solution code:
s = [input() for i in range(3)]
l0 = int(s[0])
l1 = list(map(int, s[1].split()))
l2 = int(s[2])
t = [input() for i in range(l2)]
for i in range(l2):
    m = list(map(int, t[i].split()))
    b = m[0]
    c = m[1]
    l1 = [c if j == b else j for j in l1]
    print(sum(l1))

Submission status: Time Limit Exceeded

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

shoki = sum(al)
for i in range(q):
    b = bl[i]
    c = cl[i]
    for i,youso in enumerate(al):
        if youso == b:
            ali = al[i]
            al[i] = c
            shoki += c - ali
    print(shoki)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(q):
    before = b_c[i][0]
    after = b_c[i][1]
    for j in range(n):
        if a[j] == before:
            a[j] = after
    ans = sum(a)
    print(ans)

Submission status: Time Limit Exceeded

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

while True:
    try:
        bc=list(map(int,input().split()))
        b = bc[0]
        c = bc[1]

        a = [c if i == b else i for i in a]
        print(sum(a))
    except:
        break;

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())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=np.cumsum(A)
print(ans[-1])

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans[-1]+=(C-B)*len(rep)
    print(ans[-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from collections import Counter
    N = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    Q = int(sys.stdin.readline())
    BC = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    C = Counter(A)
    '''
    L = [0 for _ in range(10**5+1)]
    for key, value in C.items():
        L[key] = value
    '''
        
        
    for bc in BC:
        C[bc[1]] += C[bc[0]]
        C[bc[0]]= 0
        ans = 0
        for key, value in C.items():
            ans += key*value
        print(ans)
        



if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

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

bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]

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:
N = int(input())

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

Q = int(input())

for i in range(0, Q):
    BC = list(map(int, input().split()))
    B = BC[0]
    C = BC[1]
    AAA = [C if m == B else m for m in AAA]
    print(sum(AAA))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
def main():
    n = int(input())
    a = np.array(list(map(int, input().split())))
    q = int(input())
    for x in sys.stdin.readlines():
        b, c = map(int, x.split())
        a = np.where(a == b, c, a)
        print(np.sum(a))

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
lista = np.array([int(e) for e in input().split()])
q = int(input())



tests = list()
for i in range(q):
    test = [int(e) for e in input().split()]
    tests.append(test)

for i in tests:
    lista = np.where(lista==i[0],i[1],lista)
    print(sum(lista))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
D = {}
for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
for i in range(Q):
  s = 0
  bi, ci = map(int, input().split())
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    del D[bi]
  for j in D:
    s += j * D[j]
  print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_lst = list(map(int, input().split()))
q = int(input())
b_lst = []
c_lst = []
cnt = []
ans = []
# ans = a_lst.copy()
for i in range(q):
    x, y = map(int, input().split())

    b_lst.append(x)
    c_lst.append(y)

for i in range(q):

    for j in range(len(a_lst)):
        if a_lst[j] == b_lst[i]:
            a_lst[j] = c_lst[i]
    cnt.append(sum(a_lst))
    print(cnt[i])

# print(cnt)
# for i in range()
# print(b_lst)

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = input()
q = int(input())
bb = []
cc = []
for i in range(q):
    btmp, ctmp = input().split()
    bb.append(btmp)
    cc.append(ctmp)

for b,c in zip(bb,cc):
    a = a.replace(b,c)
    print(sum([int(j) for j in a.split()]))

Submission status: Time Limit Exceeded

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

Q = int(input())
BC= [input().split() for _ in range(Q)]

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

sumlist = []

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

for k in range(len(sumlist)):
    print(sumlist[k])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from collections import Counter

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

arr = defaultdict(int)

for k, v in Counter(a).items():
    arr[k] = v

q = int(input())
    
for _ in range(q):
    b, c = map(int, input().split())
    arr[c] += arr[b]
    arr[b] = 0
    print(sum([x * y for x, y in arr.items()]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

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

a = np.array(a)

for i in range(q):
    a = np.where(a == b[i], c[i], a)
    print(np.sum(a))