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 bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
Q = I()
sum = 0

for s in range(Q):
    BC = LI()
    for t in range(N):
        if A[t] == BC[0]:
            A[t] = BC[1]
        sum += A[t]
    print(sum)
    sum = 0

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
a0,a1= np.unique(a,return_counts=True)
suma=np.sum(a)
#print(suma)
#print(a0)
#print(a1)
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  sumb=a1[np.where(a0==b)]
  a0=np.where(a0==b,c,a0)
  #sumc=0
  #print(a0)
  #print(a1)
  sumc=np.sum(sumb)
  if sumc ==None :
    sumc=0
  #print(sumc)
  sumc*=(c-b)
  #print(sumc)
  suma+=sumc
  print(suma)

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
A = map(int, input().split())
Q = int(input())
for _ in range(Q):
  b, c = map(int, input().split())
  A = list(map(lambda a: c if a == b else a, A))
  print(sum(A))

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)]
    
    A_kind = []
    A_num = []
    ans = 0
    for a in A:
        ans += a
        if not a in A_kind:
            A_kind.append(a)
            A_num.append(1)
        else:
            A_num[A_kind.index(a)] += 1
    
    for q in range(Q):
        B[q],C[q] = map(int,input().split())
        
    for q in range(Q):
        b = B[q]
        c = C[q]
        if b in A_kind:
            n = A_kind.index(b)
            ans += (c - A_kind[n])*A_num[n]
            if c in A_kind:
                A_num[A_kind.index(c)] += A_num[n]
                del A_kind[n]
                del A_num[n]
            else:
                A_kind[n] = c
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

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(A,B,C)

c = collections.Counter(A)
values, counts = map(np.array,zip(*c.most_common()))
#print(values,counts)

for i in range(Q):
  values=np.where(values==B[i],C[i],values)
  print(np.dot(values,counts))

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

mydict = {}
for i in A:
    if i in mydict:
        score = mydict[i]
        mydict[i] = score + 1
    else:
        mydict[i] = 1


    
for i in range(Q):
    if b[i] in mydict:
        if c[i] in mydict:
            score1 = mydict[b[i]]
            score2 = mydict[c[i]]
            del mydict[b[i]]
            mydict[c[i]] = score1 + score2
        else:
            mydict[c[i]] = mydict[b[i]]
            del mydict[b[i]]
            
    count = 0
    for k, v in mydict.items():
        count = count + k*v
    
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter, defaultdict

def update(dic, b, c):
    dic = dic.copy()
    n = dic.pop(b)
    dic[c] += n
    return dic

def calc_diff(dic, b, c):
    return c * dic[b] - b * dic[b]

def calc_total(dic):
    return sum(k * v for k, v in dic.items())

N = int(input())
lis = list(map(int, input().split()))
dic = defaultdict(int)
dic.update(Counter(lis))
Q = int(input())
total = calc_total(dic)
for _ in range(Q):
    B, C = map(int, input().split())
    total += calc_diff(dic, B, C)
    print(total)
    dic = update(dic, B, C)

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):
    BTemp,CTemp = map(int, input().split())
    B.append(BTemp)
    C.append(CTemp)
for BTemp,CTemp in zip(B,C):
    ATemp = []
    for i in A:
        if i == BTemp:
            i = CTemp
        ATemp.append(i)
    A = ATemp
    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())
    A = [C if i == B else i for i in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = input()
P = input()
Q = int(input())
for i in range(Q):
    B, C = map(str, input().split())
    P = P.replace(B, C)
    print(sum(map(int, P.split())))

Submission status: Time Limit Exceeded

Solution code:
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N=int(sys.stdin.readline().rstrip())
A_row=LI()
Q=int(sys.stdin.readline().rstrip())
B_C=[LI() for _ in range(Q)]

for i in range(Q):
    for j in range(N):
        if A_row[j]==B_C[i][0]:
            A_row[j]=B_C[i][1]
    print(sum(A_row))

Submission status: Time Limit Exceeded

Solution code:
#d
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())
ans=sum(A)
for i in range(Q):
    P=A.count(B[i])
    for j in range(P):
        A[A.index(B[i])]=C[i]
    ans=ans+P*(C[i]-B[i])
    print(ans)

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):
    values = [ my_dict[key]*key for key in my_dict.keys() if my_dict[key] != 0]
    #for key in my_dict.keys():
    #    value = my_dict[key]
    #    sum += int(key) * int(value)
    return sum(values)

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

str1 = input()
A = [int(str1.split(" ")[i]) for i in range(N)]
  
Q = int(input())

for j in range(Q):
  SUM = 0
  str2 = input()
  B, C = int(str2.split(" ")[0]), int(str2.split(" ")[1])
  A = [C if a == B else a for a in A]
  SUM = sum(A)
  print(SUM)

Submission status: Time Limit Exceeded

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


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

BC= [list(map(str, input().split())) for i in range(Q)]


for i in range(Q):
    A = [s.replace(BC[i][0],BC[i][1]) for s in A]

    print(sum(list(map(int,A))))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n=int(input())
a=np.array(list(map(int,input().split())))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    a=np.where(a == b, c, a)
    print(np.sum(a))

Submission status: Time Limit Exceeded

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

    q = int(input())
    bc = [list(map(int,input().split())) for i in range(q)]
    
    for j in range(q):
        if (bc[j][0] not in a):
            print(sum(a))
            continue
        else:
            rep = [bc[j][1] if i == bc[j][0] else i for i in a]
            a = rep
            print(sum(a))

main()

Submission status: Time Limit Exceeded

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0
    
for i in range(N):
    lst[A[i]] += 1

Q = int(input())

ans = []
for i in range(Q):
    B, C = map(int, input().split())
    if B in lst:
        if C in lst:
            lst[C] += lst[B]
            lst.pop(B)
        else:
            lst[C] = lst[B]
            lst.pop(B)
            
    ans_tmp = 0
    for i in lst:
        ans_tmp += i*lst[i]
    
    ans.append(ans_tmp)

for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
numpy_a = np.array(list(map(int, input().split())))
Q=int(input())
ans=[]
for i in np.arange(Q):
  B,C=map(int, input().split())
  numpy_a=np.where(numpy_a == B, C, numpy_a)
  ans.append(np.sum(numpy_a))
print('\n'.join(map(str, ans)))

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 a == B else a for a in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
q=int(input())
b=[[],[]]

for i in range(q):
    b1,c1=np.array(list(map(int,input().split())))
    b[0].append(b1)
    b[1].append(c1)

for i in range(q):
    for j,k in enumerate(a):
        if b[0][i]==k:
            a[j]=b[1][i]
    sum=np.sum(a)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())

A = np.array(A)
l = A
for i in range(Q):
    B, C = map(int, input().split())
    A[A==B]=C
    l = np.vstack((l,A))

s = np.sum(l, axis=1)
s = s.tolist()
for j in range(Q):
    print(s[j+1])

Submission status: Time Limit Exceeded

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

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

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

B = []
C = []


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

#数値nの個数を追加
nums = {}
for n in list(set):
    nums[n] = 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):
    if B[i] in nums:
        Bnum = nums[B[i]]
        S += (C[i]-B[i])*Bnum

        if C[i] in nums:
            nums[C[i]] += Bnum

        else:
            nums[C[i]] = Bnum

        nums[B[i]] = 0
    print(S)

Submission status: Time Limit Exceeded

Solution code:
import collections

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]
  a=collections.Counter(A)
  check=list(a.items())
  ans=0
  for j in range(len(check)):
    ans+=check[j][0]*check[j][1]
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
li = list(map(int,input().split()))
q = int(input())
num_li=[list(map(int,input().split())) for i in range(q)]

li.sort()
for i,j in num_li:
    if i in li:
        cnt = li.count(i)
        li = [k for k in li if k != i] + [j]*cnt
    print(sum(li))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
q=int(input())
dic=dict()
list_sum=sum(l)
for i in range(len(l)):
    if(l[i] not in dic.keys()):
        dic[l[i]]=1
    else:
        dic[l[i]]+=1
# print(dic)

for i in range(q):
    s=0
    b,c=map(int,input().split())
    if(b in dic and dic[b]!=0):
        # s=(c-b)*dic[b]+list_sum
        if(c in dic):
            dic[c]+=dic[b]
            dic[b]=0
        else:
            dic[c]=dic[b]
            dic[b]=0
    for x in dic:
        if(dic[x]!=0):
            s+=x*dic[x]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
s = 0
for q in range(Q):
    count = 0
    B,C = [int(x) for x in input().split()]
    for i in range(len(A)):
        if A[i]==B:
            A[i]=C
            count+=1
    if q>0:
        s = s+count*(C-B)
    else:
        s = sum(A)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

dct = defaultdict(int)
for _ in range(N):
  dct[nums.pop()] += 1

for _ in range(Q):
  B, C = map(int,input().split())
  if B in dct:
    if C in dct:
      dct[C] += dct.pop(B)
    else:
      dct[C] = dct.pop(B)

  sum = 0
  for k, v in dct.items():
    sum += k * v
  
  print(sum)

Submission status: Time Limit Exceeded

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

dic = {a+1:0 for a in range(10**5)}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  dic[C] += dic[B]
  dic[B] = 0
  
  ans = 0
  for k, v in dic.items():
    ans += k*v
  
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for j in range(q):
  bc = list(map(int, input().split()))
  res = bc[0]
  for j in range(n):
    if a[j] == res:
      a[j] = bc[1]
    else:
      a[j] = a[j]
  print(sum(a))

Submission status: Time Limit Exceeded

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

    ans = sum(A_list)
    for i in range(Q) :
        num = A_list.count(B[i])
        #print(num)
        if B[i] > C[i] :
            ans = ans - (B[i] - C[i]) * num
        else :
            ans = ans + (C[i] - B[i]) * num
        for j in range(len(A_list)) :
            if B[i] in A_list :
                rep = A_list.index(B[i])
                A_list[rep] = C[i]
            else :
                break
        print(ans)

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

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
q = int(input())
ans = []
for i in range(q):
    b, c = map(int, input().split())
    tmp = ac[b]
    del ac[b]
    ac[c]+= tmp
    ans.append(sum(num*size for num, size in ac.items()))

[print(total) for total in ans]

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

n = int(input())
a = list(map(int, input().split()))
q = int(input())
oper = [list(map(int, input().split())) for _ in range(q)]
a.sort()
res = sum(a)

for i in range(q):
    b, c = oper[i]
    if b in a:
        bCount = a.count(b)
        idx = a.index(b)

        a = a[:idx] + a[idx+1:]
        a.extend([c]*bCount)
        dif = c-b

        res += dif*bCount

    print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = sorted(int(x) for x in 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 = (int(x) for x in input().split())

    for j in range(N):

        try:
            idx = A.index(B)
            A[idx]=C
        except ValueError:
            break
    
    print(sum(A))

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()]
res = sum(As)
dis = {i:As.count(i) for i in set(As)}
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:
    n = dis[B]
    if C in dis:
      dis[C] += dis[B]
      dis[B] = 0
    else:
      dis[C] = dis[B]
      dis[B] = 0
    res += n*(C-B)
  print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(input().split())
q = int(input())
b = list(input().split() for i in range(q))

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

Submission status: Time Limit Exceeded

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

def my_index(l, x, default=False):
    if x in l:
        return l.index(x) + 1
    else:
        return default

for i in range(Q):
  BC.append(list(map(int, input().split())))

for i in range(Q):
  ind = my_index(A, BC[i][0])
  while ind:
    A[ind - 1] = BC[i][1]
    ind = my_index(A, BC[i][0])
  
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
  D[a] += 1
Q = int(input())
B = []
C = []
for _ in range(Q):
  l = list(map(int, input().split()))
  B.append(l[0])
  C.append(l[1])

for i in range(Q):
  k = D[B[i]]
  D[B[i]] = 0
  D[C[i]] += k
  ans = 0
  for k, v in D.items():
    ans += k * v
  print(ans)

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)

ans = sum(A)
d = dict()
for i in range(N):
    if A[i] not in d:
        d[A[i]] = set()
    d[A[i]].add(i)

for i in range(Q):
    if B[i] not in d:
        print(ans)
        continue
    size = len(d[B[i]])
    sa = C[i]-B[i]
    if C[i] not in d:
        d[C[i]] = set()
    d[C[i]] = d[C[i]] | d[B[i]]
    d[B[i]] = set()
    ans += size * sa
    print(ans)

Submission status: Time Limit Exceeded

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

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)]
dic = Counter(a)

for i in range(q):
    (b, c) = l[i] 
    num = dic[b]
    dic[b] = 0
    if dic[c] == 0:
        dic[c] = num
    else:
        dic[c] += num
    ans = 0
    for i, j in dic.items():
        ans += i*j
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
dA = {}
for a in A:
  dA[a] = 0
for a in A:
  dA[a] += 1
Q = int(input())
for q in range(Q):
  B,C = map(int,input().split())
  sA = set(dA)
  if B in sA:
    if C in sA:
      dA[C] += dA.pop(B)
    else:
      dA[C] = dA.pop(B)
  s = 0
  for a in dA:
    s += a * dA[a]
  print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    b = q_list[i][0]
    c = q_list[i][1]
    out = 0
    for j in range(n):
        a_list[j] = c if a_list[j] == b else a_list[j]
    print(sum(a_list))

Submission status: Time Limit Exceeded

Solution code:
n=int(input(''))
a=input('')
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])

q=int(input(''))
b=[]
c=[]

for i in range(q):
    x,y=map(int,input('').split(' '))
    b.append(x)
    c.append(y)

v=[]
for i in range(10**5+1):
    v.append(0)

for i in range(n):
    v[a[i]]+=1

for i in range(q):
    s=0
    v[c[i]]+=v[b[i]]
    v[b[i]]=0
    for j in range(10**5+1):
        if v[j]>0:
            s=s+j*v[j]

    print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    A=[int(x) for x in input().split()]
    Q=int(input())
    ans=[]
    for _ in range(Q):
        B,C=map(int, input().split())
        A=[C if x==B else x for x in A]
        ans.append(str(sum(A)))
    print('\n'.join(ans))
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(str,input().split()))
q = int(input())
d = defaultdict(lambda: 0)
for nums in a:
    d[nums] += 1
for i in range (q):
    Sum = 0
    b,c = map(str,input().split())
    d[c] += d[b]
    del d[b]
    for keys in d:
        cnt = d[keys]
        keys = int(keys)
        Sum += keys*cnt
    print(Sum)

Submission status: Time Limit Exceeded

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

# input
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = [-1 for _ in range(q)]
c = [-1 for _ in range(q)]
for i in range(q):
    b[i], c[i] = map(int, input().split())

def innerproduct(d):
    ans = 0
    for key, value in d.items():
        ans += int(key)*int(value)
    return ans

# calc
d = {}
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1

# print(d)
ans = sum(a)
for i in range(q):
    if b[i] in d:
        # ans -= b[i]*d[b[i]]
        if c[i] not in d:
            d[c[i]] = d[b[i]]
        else:
            d[c[i]] += d[b[i]]
        # tmp = d[b[i]]
        d[b[i]] = 0
        # ans += tmp*d[c[i]]

    #print(d)
    print(innerproduct(d))

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = list(map(int, input().split()))
q =int(input())
rest = 0
for i in range(q):
  b,c = map(int, input().split())
  numb = a.count(b)
  if numb > 0:
    for i in range(numb):
        a.remove(b)
        a.append(c)
  s = sum(a)
  print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())

list_a = map(int, input().split())

q = int(input())

for i in range(q):
    b, c = map(int, input().split())
    list_a = list(map(lambda a: c if a==b else a, list_a))
    print(sum(list_a))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    index_r = bisect.bisect_right(an_list, b)
    index_l = bisect.bisect_left(an_list, b)
#     an_list[index_l:index_r] = [c for x in range(len(an_list[index_l:index_r]))]
    an_list[index_l:index_r] = [c] * (len(an_list[index_l:index_r]))
    print(sum(an_list))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
#数を取得
count = int(input())
seq = input().split()
repeat = int(input())
#置換する
for i in range(0, repeat):
  replace = input().split()
  seq = [replace[1] if l == replace[0] else l for l in seq]
  seq2 = [int(l) for l in seq]
  print(sum(seq2))

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())
    ans = 0
    for j in range(len(a)):
        if a[j] == b:
            a[j] = c
            ans += c
        else:
            ans += a[j]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
given_list = [int(i) for i in input().split()]
Q = int(input())

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

    if B in given_list:
        given_list = sorted(given_list)
        for i in range(N):
          if given_list[i] == B:
              given_list[i] = C
          elif given_list[i] > C:
              break
        print(sum(given_list)) #####          
    else:  
        print(sum(given_list)) #####

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_array = a_array.reshape((1, 100001))
a_val = np.arange(100001)
a_val = a_val.reshape((1, 100001))
a_val = a_val.transpose()

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

q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = list()
sum_a = 0
output = 0
cnt = 0
for i in range(q):
  bc.append(list(map(int,input().split())))
a.sort()
for i in range(q):
  flag = False
  for j in range(len(a)):
    if a[j] == bc[i][0]:
      a[j] = bc[i][1]
      flag = True
    else:
      if flag == True:
        break
  print(sum(a))
  a.sort()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
a = sum(A)
for i in range(Q):
    BC = [int(x) for x in input().split()]
    B = BC[0]
    C = BC[1]
    t = A.count(B)
    A = [C if k == B else k for k in A]
    a += t*(C-B)
    print(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)]

a = sorted(a)

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[]
for _ in range(q):
    b,c=map(int,input().split())
    bc.append([b,c])
d={}
for i in range(n):
    if a[i] not in d:
        d[a[i]]=[i]
    else:
        d[a[i]].append(i)

for i in range(q):
    b=bc[i][0]
    c=bc[i][1]
    if b in d:
        for j in d[b]:
            a[j]=c
        if c not in d:
            d[c]=d[b]
        else:
            d[c]+=d[b]
        d[b]=[]
    print(sum(a))

Submission status: Time Limit Exceeded

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

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)

ans = sum(A)
d = dict()
for i in range(N):
    if A[i] not in d:
        d[A[i]] = set()
    d[A[i]].add(i)
for i in range(Q):
    if B[i] not in d:
        print(ans)
        continue
    size = len(d[B[i]])
    sa = C[i]-B[i]
    if C[i] not in d:
        d[C[i]] = set()
    d[C[i]] = d[C[i]] | d[B[i]]
    d[B[i]] = set()
    ans += size * sa
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    b, c = map(str, input().split())
    A=A.replace(b, c)
    A2 = list(map(int, A.split()))
    t.append(sum(A2))

for _ in t:
    print(_)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if b not in a:
        pass
    else:
        a[a.index(b):a.index(b)+a.count(b)]=[c for i in range(a.count(b))]
    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())
    a = [c if i == b else i for i in a]
    print(sum(a))

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):
  for k in range(n):
    if list_a[k]==list_b[i][0]:
      list_a[k]=list_b[i][1]
  print(sum(list_a))

Submission status: Time Limit Exceeded

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

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 A:
        if A[A.index(j)] == B:
            A[A.index(j)] = C
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

nums = input().split()

q = int(input())

ans = []

for _ in range(q):
    b, c = input().split()
    
    nums = " ".join(nums)
    nums = nums.replace(b, c)
    nums = nums.split()
    t = [int(num) for num in nums]
    ans.append(sum(t))

for a in ans:
    print(a)

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):
  while B[i] in A:
    check = A.index(B[i])
    A[check] = C[i]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(map(int,input().split()))
Q=int(input())
B=[0]*Q
C=[0]*Q
for i in range(Q):
	B[i],C[i]=map(int,input().split())
import collections
A=collections.Counter(a)
for i in range(Q):
	if B[i] in A:
		f=A.pop(B[i])
		if C[i] in A:
			g=A[C[i]]
			h=f+g
		else:
			h=f
		A[C[i]]=h
	S=0
	keys=list(A.keys())
	values=list(A.values())
	for j in range(len(keys)):
		S=S+keys[j]*values[j]
	print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    print(Gokei[i])
    i+=1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
Q = int(input())
for i in range(1,Q+1):
  x, y = map(str, input().split())
  A = [s.replace(x, y) for s in A]
  A_int = list(map(int, A))
  print(sum(A_int))