Problem:


Score : 400 points

Problem Statement

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

You will now successively do the following Q operations:

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

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

Constraints

All values in input are integers.

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

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

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

Output

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

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

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

Sample Input 1

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

Sample Output 1

11
12
16

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

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

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

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



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


Sample output:
11
12
16

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
import itertools
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
line = [0]*Q
for i in range(Q):
  line[i] = list(map(int,input().split()))
  #print(line)
#print(N,A,Q)
line2 = list(itertools.chain.from_iterable(line))
X = [0]*(max(line2)+Q+1)
#print(max(line2))
total = 0
for i in range(N):
  X[A[i]-1] = A.count(A[i])
  total += A[i]

for i in range(Q):
  #print(B,C)
  total += (line[i][1]-line[i][0])*X[line[i][0]-1]
  X[line[i][1]-1] += X[line[i][0]-1]
  X[line[i][0]-1] = 0
  print(total)

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
import itertools
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
line = [0]*Q
for i in range(Q):
  line[i] = list(map(int,input().split()))
  #print(line)
#print(N,A,Q)
line2 = list(itertools.chain.from_iterable(line))
X = [0]*(max(line2)+Q+1)
total = 0
for i in range(N):
  X[A[i]-1] = A.count(A[i])
  total += A[i]

for i in range(Q):
  B = line[i][0]
  C = line[i][1]
  #print(B,C)
  total += (C-B)*X[B-1]
  X[C-1] += X[B-1]
  X[B-1] = 0
  print(total)

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
#print(N,A,Q)
X = [0]*(n)
total = 0
for i in range(N):
  X[A[i]-1] = A.count(A[i])
  total += A[i]
#for i in range(len(X)):
#	total += (i+1) * X[i] 
#print(X)
for i in range(Q):
  B,C = map(int,input().split())
  #print(B,C)
  total += (C-B)*X[B-1]
  X[C-1] += X[B-1]
  X[B-1] = 0
  print(total)

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
#print(N,A,Q)
X = [0]*(n)
for i in range(N):
  X[A[i]-1] = A.count(A[i])
total = 0
for i in range(len(X)):
	total += (i+1) * X[i] 
#print(X)
for i in range(Q):
  B,C = map(int,input().split())
  #print(B,C)
  total += (C-B)*X[B-1]
  X[C-1] += X[B-1]
  X[B-1] = 0
  print(total)

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
#print(N,A,Q)
X = [0]*(n)
for i in range(N):
  X[A[i]-1] = A.count(A[i])
#print(X)
for i in range(Q):
  B,C = map(int,input().split())
  #print(B,C)
  X[C-1] += X[B-1]
  X[B-1] = 0
  total = 0
  #print(X)
  for j in range(len(X)):
    total += (j+1) * X[j]
  print(total)

Submission status: Time Limit Exceeded

Solution code:
#N,A,Q,B,C
n = 100000
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
#print(N,A,Q)
X = [0]*(n)
for i in range(n):
  X[i] = A.count(i+1)
#print(X)
for i in range(Q):
  B,C = map(int,input().split())
  #print(B,C)
  X[C-1] += X[B-1]
  X[B-1] = 0
  total = 0
  #print(X)
  for j in range(len(X)):
    total += (j+1) * X[j]
  print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
t=int(input())
#s=sum(l)
for i in range(t):
    b,c=map(int,input().split())
    p=l.count(b)
    co=0
    #print(p)
    for x,y in enumerate(l):
        if(y==b):
            l[x]=c
            co+=1
        if(co==p):
            co=0
            break
        
    
    
    
    print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
t=int(input())
#s=sum(l)
for i in range(t):
    b,c=map(int,input().split())
    p=l.count(b)
    #print(p)
    for x,y in enumerate(l):
        if(y==b):
            l[x]=c
    
    
    print(sum(l))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_a = list(map(int, input().split()))
many = []
for i in range(1, 100001) :
    many.append(l_a.count(i))
bef = sum(l_a)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*many[b - 1]
    ans.append(bef)
    many[c - 1] += many[b - 1]
    many[b - 1] = 0

for value in ans :
    print(value)

Submission status: Time Limit Exceeded

Solution code:
def numsum(a) :
    ans = 0
    for i in range(len(a)) :
        ans += (i + 1) * a[i]
    return ans

n = int(input())
l_a = list(map(int, input().split()))
many = []
for i in range(1, 100001) :
    many.append(l_a.count(i))
bef = numsum(many)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*many[b - 1]
    ans.append(bef)
    many[c - 1] += many[b - 1]
    many[b - 1] = 0

for value in ans :
    print(value)

Submission status: Time Limit Exceeded

Solution code:
def numsum(a) :
    ans = 0
    for i in range(len(a)) :
        ans += (i + 1) * a[i]
    return ans

n = int(input())
l_a = list(map(int, input().split()))
many = []
for i in range(1, 100001) :
    many.append(l_a.count(i))
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    many[c - 1] += many[b - 1]
    many[b - 1] = 0
    ans.append(numsum(many))

for value in ans :
    print(value)

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

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

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

q = int(input())
for _ in range(q):
    b,c = map(int,input().split())
    if dd[b]>0:
        dd[c]+=dd[b]
        dd[b]=0
    sum = 0
    for key in dd.keys():
        sum+=dd[key]*key
    print(sum)

Submission status: Time Limit Exceeded

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

for value in ans :
    print(value)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check

sum_A = sum(list(A.elements()))

for before, after in BC:
    if before in A.keys():
        cnt = A[before]
        del A[before]
        sum_A -= before * cnt
        add = Counter({after: cnt})
        A = A + add
        sum_A += after * cnt

    print(sum_A)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v

#print(d)
ans=sum(d2.values())
for i in range(q):
  #print(d2)
  if d.get(b[i]):
    #removed=d.pop(b[i])
    _=d2.pop(b[i])
    d[c[i]]+=d[b[i]]
    d2[c[i]]=c[i]*d[c[i]]
    d[b[i]]=0
  else:
    pass
  #print((np.array(list(d2.values()))))
  #ans+=(c[i]-b[i])*d[removed]
  print(sum(d2.values()))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
for before, after in BC:
    if before in A.keys():
        cnt = A[before]
        del A[before]
        add = Counter({after: cnt})
        A = A + add
    print(sum(list(A.elements())))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
for before, after in BC:
    if before in A.keys():
        cnt = A[before]
        del A[before]
        add = Counter({after: cnt})
        A = A + add
    print(sum([value * cnt for value, cnt in A.most_common()]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v

#print(d)
for i in range(q):
  #print(d2)
  if d.get(b[i]):
    removed=d.pop(b[i])
    _=d2.pop(b[i])
    d[c[i]]+=removed
    d2[c[i]]=c[i]*d[c[i]]
  else:
    pass
  #print((np.array(list(d2.values()))))
  print(np.sum(np.array(list(d2.values()))))

Submission status: Time Limit Exceeded

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

Q = int(input())
BC = [tuple(map(int, input().split())) for q in range(Q)]

# check
for before, after in BC:
    if before in A:
        A = [after if a == before else a for a in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v

#print(d)
for i in range(q):
  #print(d2)
  if d.get(b[i]):
    removed=d.pop(b[i])
    _=d2.pop(b[i])
    d[c[i]]+=removed
    d2[c[i]]=c[i]*d[c[i]]
  else:
    pass
  print(sum(d2.values()))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v

#print(d)
for i in range(q):
  ans=0
  #print(d2)
  for k,v in d.items():
    if k==b[i]:
      #print("b[i]",b[i])
      removed=d.pop(b[i])
      no=d2.pop(b[i])
      d[c[i]]+=removed
      d2[c[i]]=c[i]*d[c[i]]
      break
  print(sum(d2.values()))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
#print(d)
for i in range(q):
  ans=0
  for k,v in d.items():
    if k==b[i]:
      removed=d.pop(b[i])
      d[c[i]]+=removed
      break
  #print(d)
  for k,v in d.items():
    #print(ans,k,v)
    ans+=k*v
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

ans=sum(a)
for i in range(q):
  #print(a)
  count=a.count(b[i])
  ans+=(c[i]-b[i])*count
  print(ans)
  a=[c[i] if b[i]==a[ii] else a[ii] for ii in range(n)]

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
  bi,ci=map(int,input().split())
  a=[ci if bi==a[i] else a[i] for i in range(n)]
  #print(a)
  print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby
def main():
    n=int(input())
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(input())
    bc=[]
    for _ in range(q):
        b,c=map(int,input().split())
        bc.append((b,c))
    for b,c in bc:
        if b in d:
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print(sum(k*v for k,v in d.items()))
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int, input().split()))
q = int(input())
sum_i = sum(li)
gokei = []

while True:
    b, c = map(int, input().split())
    li = [c if i == b else i for i in li]
    sum_i += (c - b) * li.count(b)
    gokei.append(sum(li))
    q -= 1
    if q == 0:
        [print(i) for i in gokei]
        break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int, input().split()))
q = int(input())
gokei = []

while True:
    b, c = map(int, input().split())
    li = [c if i == b else i for i in li]
    gokei.append(sum(li))
    q -= 1
    if q == 0:
        [print(i) for i in gokei]
        break

Submission status: Time Limit Exceeded

Solution code:
import collections as collect
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC=[list(map(int,input().split())) for _ in range(Q)]
mylis=collect.Counter(A)
for bc in BC:
    tmp=mylis[bc[0]]
    mylis[bc[0]]=0
    mylis[bc[1]]+=tmp
    out=0
    for k,v in mylis.items():
        out+=k*v
    print(out)

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

numbers = list(map(int, input().split()))
count = int(input())

sum_numbers = []
for i in range(count):
    before, after = map(int, input().split())
    numbers = [after if value == before else value for value in numbers]
    sum_numbers.append(sum(numbers))

for i in range(count):
    print(sum_numbers[i])

Submission status: Time Limit Exceeded

Solution code:
#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    q = ni()
    for i in range(q):
        b, c = na()
        alist[c] += alist[b]
        alist[b] = 0
        ans = 0
        
        for i, num in enumerate(alist):
            ans += i*num
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
l=list(map(int,input().split()))
b=int(input())
initial_sum=sum(l)
for _ in range(b):
  l1=list(map(int,input().split()))
  x=l1[0]
  y=l1[1]
  z=l.count(l1[0])
  for i in range(len(l)):
    if l[i] ==l1[0]:
      l[i]=l1[1]
  initial_sum+=(y-x)*z
  print(initial_sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
r = list(map(int,input().split(" ")))
l = []
ar = {}
s = sum(r)
for a in r:
    if a not in l:
        ar[a] = 1
        l.append(a)
    else:
        ar[a] += 1
q = int(input())
for i in range(q):
    x,y = map(int,input().split(" "))
    if x in l:
        s += (y - x) * ar[x]
        if y in l:
            ar[y] += ar[x]
        else:
            ar[y] = ar[x]
            l.append(y)
        ar[x] = 0
    print(s)

Submission status: Time Limit Exceeded

Solution code:
#171D
#Replacing
#自分の解き方

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
L=len(A)
S=sum(A)

for i in range(Q):
    ans=0
    B,C=map(int,input().split())
    for j in range(L):
        if A[j]==B:
            A[j]=C
            S+=(C-B)
    ans=S
    print(ans)

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 = map(int,input().split())
  B.append(b)
  C.append(c)

S = [0]*100001
for i in A:
  S[i] += 1

for i in range(Q):
  S[C[i]] += S[B[i]]
  S[B[i]] = 0

  ans = 0

  for i in range(len(S)):
    ans += i*S[i]

  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input()
q = int(input())

l2= []
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    l2.append(sum([int(i) for i in l.split()]))
    
for e in l2:
    print(e)

Submission status: Time Limit Exceeded

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

# D - Replacing
def get_sum(dic):
	sum = 0
	
	for k, v in dic.items():
		sum += k * v
	
	return sum


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

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

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

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count
	
	print(get_sum(dic))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input()
q = int(input())

l2= []
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    l2.append(sum(map(int, l.split())))
    
for e in l2:
    print(e)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input()
q = int(input())

d = {}
for i in range(q):
    _l = input().split()
    l = l.replace(_l[0], _l[1])
    d[i] = sum(map(int, l.split()))
    
for key in d.keys():
    print(d[key])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(input().split())
q = int(input())

d = {}
for i in range(q):
    _l = list(input().split())
    l2 = [i for i, x in enumerate(l) if x == _l[0]]
    for e in l2:
        l[e] = _l[1]
    d[i] = sum(map(int, l))
    
for key in d.keys():
    print(d[key])

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

    g = {}
    for a in A:
        if a not in g:
            g[a] = 1
        else:
            g[a] += 1

    for i in range(Q):
        B, C = tuple(BC[i])
        if B in g:
            t = g.pop(B)
            if C in g:
                g[C] += t
            else:
                g[C] = t
        ans = 0
        for k, v in g.items():
            ans += k * v
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def calc(b, c):
  _sum = 0
  for i in range(n):
    a[i] = c if a[i] == b else a[i]
    _sum += a[i]
  return _sum

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

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 i in range(Q):
        B, C = tuple(BC[i])
        for j in range(N):
            if A[j] == B:
                A[j] = 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())
for i in range(q):
    sum = 0
    b, c = map(int, input().split())
    for j in range(len(a)):
        if a[j] == b:
            a[j] = c
            sum += a[j]
        else:
            sum += a[j]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = 0
    for j in range(len(numbers)):
        if numbers[j] == B : 
            cnt += 1
            numbers[j] = C
    sums += cnt * (C - B)
    print(sums)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
for i in range(numOfOperation):
    B,C = map(int,input().split())
    for j in range(len(numbers)):
        if numbers[j] == B : numbers[j] = C
    print(sum(numbers))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_row = input().split(" ")
for i in range(len(A_row)):
  A_row[i] = int(A_row[i])
Q = int(input())
D = [0]*Q
for i in range(Q):
  D[i] = input().split(" ")
  B = int(D[i][0])
  C = int(D[i][1])
  
  for i in range(len(A_row)):
    if B == A_row[i]:
      A_row[i] = C
    else:
      None
  print(sum(A_row))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
q=int(input())
dp=[0]*(10**5+1)
for i in range(1,10**5+1):
  dp[i]=a.count(i)

for j in range(q):
  b,c=map(int,input().split())
  ans+=(c-b)*dp[b]
  dp[c]+=dp[b]
  dp[b]=0
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_sum = sum(a)
q = int(input())
b = []
c = []
for i in range(q):
    b_temp, c_temp = map(int, input().split())
    b.append(b_temp)
    c.append(c_temp)
for j in range(q):
    les = c[j]-b[j]
    count = a.count(b[j])
    check_list = [i for i, x in enumerate(a) if x == b[j]]
    if check_list:
        for k in check_list:
            a[k] = c[j]
    plass_total = les * count
    a_sum += plass_total
    print(a_sum)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
Q = int(stdin.readline())
D = dict()
S = set()
sum=0

for y in A:
    sum+=y
    s = len(S)
    S.add(y)
    if len(S)==s:
        D[y]+=1
    else:
        D[y]=1



for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0

for y in A:
    sum+=y

for t in S:
    D[t]=A.count(t)

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

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_temp, c_temp = map(int, input().split())
    b.append(b_temp)
    c.append(c_temp)
for j in range(q):

    check_list = [i for i, x in enumerate(a) if x == b[j]]
    if check_list:
        for k in check_list:
            a[k] = c[j]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
    bc = list(map(int,input().split()))
    #bc.append(list(map(int,input().split())))
    #while bc[i][0] in a:
        #a[a.index(bc[i][0])] = bc[i][1]
    aa = [bc[1] if bc[0] == a[j] else a[j] for j in range(len(a))]
    a = aa
    ans.append(sum(aa))
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
    bc.append(list(map(int,input().split())))
    while bc[i][0] in a:
        a[a.index(bc[i][0])] = bc[i][1]
    ans.append(sum(a))
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

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

    while bc[i][0] in a:
        a[a.index(bc[i][0])] = bc[i][1]
    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  B_C = list(map(int,input().split()))
  for j, item in enumerate(A):
    if item == B_C[0]:
      A[j] = B_C[1]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(an) for an in input().split()]
q = int(input())

a_sum = sum(a)
for i in range(q):
    stdins = input().split()
    b = int(stdins[0])
    c = int(stdins[1])
    for j in range(n):
        if a[j] == b:
            a[j] = c
            a_sum = a_sum - b + c
    print(a_sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(an) for an in input().split()]
q = int(input())

for i in range(q):
    stdins = input().split()
    for j in range(n):
        if a[j] == int(stdins[0]):
            a[j] = int(stdins[1])
    ans = sum([int(an) for an in a])
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
    stdins = input().split()
    for j in range(n):
        a[j] = a[j].replace(stdins[0], stdins[1])
    ans = sum([int(an) for an in a])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
q = int(input())
b = []
c = []
for i in range(q):
    stdins = input().split()
    b.append(stdins[0])
    c.append(stdins[1])

for i in range(q):
    for j in range(n):
        a[j] = a[j].replace(b[i], c[i])
    ans = sum([int(an) for an in a])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys 

def main(n, a_n, q, bc_n):
    m = {}
    for i in range(n):
        if a_n[i] not in m:
            m[a_n[i]] = 0 
        m[a_n[i]] += 1

    s = []
    for j in range(q):
        b, c = bc_n[j]

        if b in m:
            if c not in m:
                m[c] = m[b]
            else:
                m[c] += m[b]
            del m[b]

        ts = 0 
        for k, v in m.items():
            ts += k * v 
        s.append(ts)
    return s


n = int(sys.stdin.readline().strip())
a_n = [int(x) for x in sys.stdin.readline().strip().split(" ")]
q = int(sys.stdin.readline().strip())
bc_n = []
for _ in range(q):
    b, c = [int(x) for x in sys.stdin.readline().strip().split(" ")]
    bc_n.append((b, c)) 
for i in main(n, a_n, q, bc_n):
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

ans = sum(A)

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

Submission status: Time Limit Exceeded

Solution code:
import collections

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

ans = sum(A)

for i in range(Q):
    counter = collections.Counter(A)
    if BC[i][0] in counter:
        ans += counter[BC[i][0]]*(BC[i][1]-BC[i][0])
        print(ans)
        A = [BC[i][1] if x==BC[i][0] else x for x in A]
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
process = []
proc_append = process.append
for i in range(Q):
	proc_append(tuple(input().split()))
	process[i] = tuple(map(int,process[i]))
A.sort()
for b,c in process:
	if b in set(A):
		for i in range(N):
			if b==A[i]:
				begin = i
				break
		for i in range(begin,N):
			if b!=A[i]:
				end = i-1
				break
			if i==N-1:
				end = i
		for i in range(begin,end+1):
			A[i] = c
	print(sum(A))