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: Wrong Answer

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
dica=Counter(a)
q=int(input())
b=[]
c=[]
for i in range(q):
  b_,c_=map(int,input().split())
  b.append(b_)
  c.append(c_)

i=0
j=0

while i<=(q-1):
  num=dica[b[i]]
  new_sum=suma-b[i]*num+c[i]*num
  print(new_sum)
  
  suma=new_sum
  try:
    dica[c[i]]+=num
  except:
    dica[c[i]]=num
  i+=1

Submission status: Wrong Answer

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

for num, rep in zip(b, c):
    if num not in a: continue
    else:
        a = [rep if ele == num else ele for ele in a]
    print(sum(a))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
B = [0]*(N+1)
for n in range(N):
    B[n+1] = B[n] + A[n]
C = max(B)

Z = {}
for n in range(N):
    Z[A[n]] = 0
for n in range(N):
    Z[A[n]] += 1

D = []
Q = int(input())
for q in range(Q):
    x, y = map(int, input().split())
    if x in Z:
        C = C - x*Z[x]
        C = C + y*Z[x]
        if y in Z:
            Z[y] += 1
        else:
            Z[y] = Z[x]
        D.append(C)
        Z.pop(x)
    else:
        D.append(C)
for d in D:
    print(d)

Submission status: Wrong Answer

Solution code:
n, a = int(input()), ''.join(map(str, input().split()))
for i in range(int(input())):
    b, c = map(str, input().split())
    a = a.replace(b, c)
    print(sum(list(map(int, a))))

Submission status: Wrong Answer

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

count = [0 for i in range(10**5+1)]

for a_i in A:
    count[a_i] += 1

bef_ans = 0
for i in range(Q):
    B, C = map(int, input().split())
    tmp = count[B]
    count[B] = 0
    count[C] += tmp
    ans = 0
    if i == 0:
        for j in range(10**5):
            ans += j*count[j]
    else:
        ans = bef_ans - B*tmp + C*tmp
    print(ans)
    bef_ans = ans

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []
    a = sum(A)
    for _ in range(Q):
        b, c = map(int, input().split())
        a += (A.count(b))*(c-b)
        print(a)
        B.append(A.count(b))
        C.append(c)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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):
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
    print(a)
    print(sum(a))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
dica=Counter(a)
q=int(input())
b=[]
c=[]
for i in range(q):
  b_,c_=map(int,input().split())
  b.append(b_)
  c.append(c_)

i=0
j=0

while i<=(q-1):
  num=dica[b[i]]
  new_sum=suma-b[i]*num+c[i]*num
  print(new_sum)
  
  suma=new_sum
  try:
    dica[c[i]]+=num
  except:
    dica.setdefault(c[i],num)
  i+=1

Submission status: Wrong Answer

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

A.sort()
S = 0
d = {}
for i in A:
    S += i
    if i in d:
        d[i] += 1
    else:
        d[i] = 1

Q = int(input())

for i in range(Q):
    B,C = map(int,input().split())
    if B in d:
        x = d[B]
        S -= B * x
        S += C * x
        if C in d:
            d[C] += x
        else:
            d[C] = x
    print(S)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
A = {}
for i in a:
    if not i in A:
        A[i] = 1
    else:
        A[i] += 1

s = 0
for k,v in A.items():
    s += k*v

Q = int(input())
for _ in range(Q):
    b,c = map(int,input().split())
    if b in A:
        if not c in A:
            A[c] = A[b]
        else:
            A[c] += A[b]
        A[b] == 0
        s = s-(b-c)*A[b]

    print (s)

Submission status: Wrong Answer

Solution code:
N = int(input())
suretu = list(input().split())
Q = int(input())
ans = []
for i in range(Q):
    kei = 0
    x, y = input().split()
    l_replace = [s.replace(x, y) for s in suretu]
    for k in l_replace:
        kei += int(k)
    ans.append(kei)
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import collections
s=str(input())
s = list(map(int, input().split()))
q = int(input())
x = [list(map(int,input().split())) for i in range(q)]
c = collections.Counter(s)
ans=s
for i in range(q):
    if x[i][0] not in c.keys():
        print(ans)
        continue
    tmp = c[x[i][0]]
    c[x[i][0]] = 0
    c[x[i][1]] = c[x[i][1]]+tmp
    ans=0
    for j in c.keys():
        ans+=j*c[j]
    print(ans)

Submission status: Wrong Answer

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:
    s = 0
  s = 0
  for a in dA:
    s += a * dA[a]
  print(s)

Submission status: Wrong Answer

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

    d={}
    s=0
    for i in r(n):
        cu=a[i]

        if d.get(cu, None):
            d[cu]+=1
        else:
            d[cu]=1
        
        s+=cu
    
    for _ in r(q):
        bi, ci = map(int, input().split())
        diff=ci-bi

        if d.get(ci, None):
            d[ci]+=d.get(bi, 0)
        else:
            d[ci]=d.get(bi, 0)

        s+=diff * d.get(bi, 0)
        print(s)


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 B:
    for j in range(len(A)):
        if A[j] > i[0]:
            break
        if A[j] == i[0]:
            A[j] = i[1]
    print(sum(A))

Submission status: Wrong Answer

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

    d={}
    s=0
    for i in r(n):
        cu=a[i]

        if d.get(cu, None):
            d[cu]+=1
        else:
            d[cu]=1
        
        s+=cu
    
    c={}
    for _ in r(q):
        bi, ci = map(int, input().split())
        diff=ci-bi
        if c.get(ci, None):
            c[ci]+=d.get(bi, 0)
        else:
            c[ci]=d.get(bi, 0)
        s+=diff * (d.get(bi, 0) + c.get(bi, 0))
        print(s)


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()

Submission status: Wrong Answer

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(a)
  print(sum(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#入力
N = int(input())
Alist = list(map(int,input().split()))
Alist.sort()
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

for j in range(Q):
  num = Alist.count(Blist[j])
  if num == 0:
    break
  else:
    firstindex = Alist.index(Blist[j])
    for l in range(num):
      Alist[firstindex+l] = Clist[j]
  print(sum(Alist))
  Alist.sort()

Submission status: Wrong Answer

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

S = 0
d = {}
for i in A:
    S += i
    if i in d:
        d[i] += 1
    else:
        d[i] = 1


Q = int(input())

for i in range(Q):
    B,C = map(int,input().split())
    if B in d:
        S -= B * d[B]
        S += C * d[B]
        if C in d:
            d[C] += d[B]
        else:
            d[C] = d[B]
    print(S)

Submission status: Wrong Answer

Solution code:
import collections

def calc_sum(c):
    sum_val = 0
    for key, val in c.items():
        sum_val += key * val
    return sum_val

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

actions = []

for i in range(Q):
    B, C = map(int, input().split())
    actions.append({'B':B, 'C':C})

c = collections.Counter(arr)
sum_val = calc_sum(c)

for i in range(Q):
    B = actions[i]['B']
    C = actions[i]['C']
    if B in c:
        c[C] += c[B]
        sum_val += c[C] * C - c[B] * B
        c.pop(B)

    print(sum_val)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
temp=sum(a)
import collections
ca=collections.Counter(a)
print(ca)
for i in range(q):
  b,c=map(int,input().split())
  ba=ca[b]
  temp=temp-b*ba+c*ba
  ca[b]=ca[b]-ba
  ca[c]=ca[c]+ba

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
numl = [0] * 100005
resl = []
suma = sum(a)
for i in range(n):
    numl[a[i]]+=1
for i in range(q):
    b, c = map(int, input().split())
    bn = numl[b]
    suma += (c - b)* bn
    resl.append(suma)
    numl[c] -= bn
    numl[c] += bn
for i in resl:
    print(i)

Submission status: Wrong Answer

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

List = [0]*100000
for i in A:
  List[i-1] += 1
  ans += i

dic = {}
for i in range(100000):
  if List[i] != 0:
    dic[i+1] = List[i]



tmp = 0
for i in range(Q):
  B,C= map(int, input().split())
  if B in dic:
    tmp = dic[B]
  else:
    print(ans)
    continue
  ans = ans + tmp*(C-B) 
  print(ans)
  if C in dic:
    dic[C] += tmp
  else:
    dic.pop(B)
    dic[C] = tmp

Submission status: Wrong Answer

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

S = sum(A)
counter = Counter(A)
for i in range(Q):
    B,C = list(map(int,input().split()))
    S += counter[B]*C - counter[B]*B
    counter[C] += counter[B]
    print(S)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split(' ')]
q = int(input())
j = []
a.sort()
a2 = {}
for i in a:
    if i in a2:
        a2[i] += 1
    else:
        a2[i] = 1

d = 0
for i in range(q):
    b, c = (int(i) for i in input().split(' '))
    if b in a2:
        if c in a2:
            a2[c] += a2[b]
        else:
            a2[c] = a2[b]
        del a2[b]
        d = 0
        for k in a2:
            d += k * a2[k]
    j.append(d)
 
print("\n".join([str(i) for i in j]))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
d=dict(Counter(a))
ans=sum(a)
q=int(input())
for _ in range(q):
    b,c=map(int,input().split())
    if b not in d.keys(): continue
    ans+=(c*d[b]-b*d[b])
    print(ans)
    if c in d.keys(): d[c]+=d[b]
    else: d[c]=d[b]
    d[b]=0

Submission status: Wrong Answer

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

from collections import Counter
z = Counter(a)

sum = sum(a)
for i in range(q):
    try:
        rev = z.pop(u[i][0])
        print(sum+(u[i][1]-u[i][0])*rev)
        if u[i][1] in z.keys():
            z[u[i][1]]+=rev
        else:
            z[u[i][1]]=rev
    except KeyError:
        print(sum)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans1 =  sum(a)
ans2 =  ans1

for i in range(Q):
    ans1 = ans2
    B,C = map(int, input().split())
    a.sort()
    cnt = a.count(B)
    if cnt == N:
        ans1  = C*N
        print(ans1)
        a = [C for k in range(N)]
        continue
        
    
    index = bisect.bisect(a,B)
    for j in range(cnt):
        a[index+j-cnt] = C
        ans2 += C-B

    print(ans2)
    ans1 = ans2

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = 0
dic = {}

for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for _ in range(Q):
    B, C = map(int, input().split())
    if B in dic.keys():
        tmp = dic[B]
        dic[B] = 0
        if C in dic.keys():
            dic[C] += tmp
        else:
            dic[C] = tmp
    else:
        continue

    for val, num in dic.items():
        ans += val * num

    print(ans)
    ans = 0

Submission status: Wrong Answer

Solution code:
def resolve():
    n=int(input())
    a=list(map(int,input().split()))
    for i in range(n):
        ans=0
        for j in range(n):
            if j==i:
                continue
            ans=ans^a[j]
        print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
Dict = {}
for i in A:
    if i not in Dict:
        Dict[i] = 1
    else:
        Dict[i]+=1
ans = sum(A)
for i in range(Q):
    B, C = map(int,input().split())
    if B in Dict and Dict[B]!=0:
        b = Dict[B]
        Dict[B]=0
        if C not in Dict:
            Dict[C]=0
        Dict[C]+=b
        
        ans+=(C-B)*b
        print(ans)

Submission status: Wrong Answer

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

whole = sum([x * y for x, y in arr.items()])

for b, c in bc:
    whole += c * arr[c] - b * arr[b]
    print(whole)
    arr[c] += arr[b]
    arr[b] = 0

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

sum_num = sum(a_list)
counter = Counter(a_list)

for i, (b, c) in enumerate(bc_list):
    sum_num = sum_num + ((c - b) * counter[b])
    counter[c] = counter[c] + counter[b]
    print(sum_num)

Submission status: Wrong Answer

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

count = 0
c = 0

for i in range(q):
    c = b[i][1]
    for j, item in enumerate(a):
        if item == b[i][0]:
            a.pop(j)
            count = count + 1
            
    print(sum(a) + c*count)

Submission status: Wrong Answer

Solution code:
N = int(input())
li = list(map(int,input().split()))
Q = int(input())
import bisect
for i in range(Q):
    b,c = map(int,input().split())
    li.sort()
    ind = bisect.bisect_left(li,b)
    if ind>N-1:
        print(sum(li))
    elif li[ind]==b:
        while li[ind]==b:
          li[ind]=c
          if ind!=N-1:
              ind+=1
          else:
            break
        print(sum(li))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
q=int(input())
ans=sum(a)
list1=(Counter(a))
b=[0]*q
c=[0]*q
ct=0
for i in range(q):
    b[i],c[i]=list(map(int,input().split()))
for i in range(q):
    if list1[b[i]]>0 :
        if list1[c[i]]==0:
            list1[c[i]]=0
            list1[c[i]]+=list1[b[i]]
            ans=ans-(b[i]*list1[b[i]])+(c[i]*(list1[c[i]]))        
            list1[b[i]]=0
        else:
            ans=ans-(b[i]*list1[b[i]])+(c[i]*(list1[c[i]]))
            list1[c[i]]+=list1[b[i]]  
            list1[b[i]]=0  
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
As = {}
answer = 0
for a in [int(x) for x in input().rstrip().split()]:
    answer += a
    if a in As.keys():
        As[a] += 1
    else:
        As[a] = 1
# print(As)
Q = int(input().rstrip())
d = {}
for i in range(Q):
    b, c = map(int, input().rstrip().split())
    d[i] = {'b': b, 'c': c}
# print(d)
for i in sorted(d.keys()):
    if d[i]['b'] in As.keys():
        diff = d[i]['c'] - d[i]['b']
        diff_num = As.pop(d[i]['b'])
        if d[i]['c'] in As.keys():
            As[d[i]['c']] += diff_num
        else:
            As[d[i]['c']] = diff_num
        answer += (diff * diff_num)
    print(answer)
print('---')

Submission status: Wrong Answer

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: Wrong Answer

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

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

q = int(input())
s = 0
for v in a:
	s += v

for _ in range(q):
	b, c = map(int, input().split())
	ab = ind[b]
	if ab == 0:
		print(s)
		continue

	s += (ab*c)-(ab*b)
	ind[c] += ab

	print(s)

Submission status: Wrong Answer

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

s = sum(a_s)
m = { n: 0 for n in a_s }

for a in a_s:
    m[a] += 1

for b, c in bc_s:
    nb = m.get(b, 0)
    s += nb * (c - b)
    print(s)
    m[c] = nb + m.get(c, 0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
  b, c = map(str, input().split())
  a = [str(n) for n in a]
  s = ''.join(a)
  s = s.replace(b, c)
  a = list(s)
  a = [int(n) for n in a]
  ans.append(sum(a))
for i in range(q):
  print(ans[i])

Submission status: Wrong Answer

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

st = ""
res = 0

for i in A:
    i = str(i)
    st += i
for i in range(Q):
    a = str(B[i][0])
    b = str(B[i][1])
    st = st.replace(a, b)
    for i in range(N):
        res += int(st[i])
    print(res)
    res = 0

Submission status: Wrong Answer

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

    nums = np.zeros(max(A.max(), BC.max())+1)
    sm = A.sum(dtype=np.int64)
    for i in A:
        nums[i]+=1

    for b,c in BC:
        sm += nums[b]*(c-b)
        nums[c] +=nums[b]
        nums[b] = 0
        print(sm)








if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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.sort()
    ans = sum(A)
    from bisect import bisect_left
    for i in range(q):
        b,c = map(int, input().split())
        count = 0
        k = bisect_left(A,b)
        for j,a in enumerate(A[k:]):
            if a != b:
                break
            A[k+j] = c
            count += 1
        ans += (c-b)* count
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s = 0
result = []

r = 1
while r <= q:
  e = list(map(int, input().split()))
  i = 0
  while i < n:
     if(p[i] == e[0]):
        p[i] = e[1]
     s = sum(p)
     i += 1

  r += 1
  result = str(s)+"\n"


print(result.strip())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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,c = map(int,input().split())
        if b in A_kind:
            n = A_kind.index(b)
            ans += (c - A_kind[n])*A_num[n]
            A_kind[n] = c
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from collections import Counter

    N = int(input())
    A = list(map(int, input().split()))
    CA = Counter(A)
    sumA = 0
    for key, value in CA.items():
        sumA += key*value

    Q = int(input())
    for i in range(Q):
        b, c = map(int, input().split())
        sumA = (c-b)*CA[b]
        CA[c] += CA[b]
        CA[b] = 0
        print(sumA)

main()

Submission status: Wrong Answer

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_count = []
count = collections.Counter(a)
for i in range(q):
  num = count[bc[i][0]]
  count[bc[i][0]] = 0
  count[bc[i][1]] += num
  ans_count.append(count)

for ans in ans_count:
  total = 0
  for key, value in zip(count.keys(), count.values()):
    total += int(key) * int(value)
  print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = dict(Counter(a_L))
all_sum = sum(a_L)

ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    try:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    except:
        pass
    ans_L.append(all_sum)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
q=int(input())
s=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if c in d and b in d:
        d[c]+=d[b]
    elif b in d:
        d[c]=d[b]
    if b in d:
        s=s+(c-b)*d[b]
    print(s)

Submission status: Wrong Answer

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)]
a_list=sorted(a_list)
for i in range(Q):
    a=1
    for j in range(N):
        if a<=a_list[j]:
            if a_list[j]==B[i][0]:
                a_list[j]=B[i][1]
        a+=1
    print(sum(a_list))

Submission status: Wrong Answer

Solution code:
N = input()
arr = [int(x) for x in input().split()]

q = int(input())
ans = sum(arr)

hashmap = {}
for val in arr:
    if val in hashmap:
        hashmap[val] += 1
    else:
        hashmap[val] = 1


while q > 0:

    [x, y] =  [int(x) for x in input().split()]
    if x in hashmap:
        if y in hashmap:
            hashmap[y] += hashmap[x]
            ans -= hashmap[x]*x
            ans += y*hashmap[x]
        else:
            diff = hashmap[x] * x
            ans -= diff
            hashmap[y] = hashmap[x]
            del hashmap[x]
            add = hashmap[y] * y
            ans += add
        
    print(ans)
    q -=1

Submission status: Wrong Answer

Solution code:
# ####################################################################
# import io
# import sys

# _INPUT = """\
# 2
# 1 2
# 3
# 1 100
# 2 100
# 100 1000
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################


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

#「groupby」を使って、
from itertools import groupby
L = [[k, len(list(g))] for k, g in groupby(A)]
# print( L )

from collections import defaultdict
d = defaultdict(int)
for k,v in L:
  d[k] = v

# print(d)

sumA = sum(A)

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

  k = d[b]
  sumA += (c-b)*k
  print(sumA)

  d[c] += d[b]
  d[b] = 0

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = dict(Counter(a_L))
all_sum = sum(a_L)



ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    try:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    except:
        pass
    ans_L.append(all_sum+1)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

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

s = 0
result = []

r = 1
while r <= q:
  e = list(map(int, input().split()))
  i = 0
  while i < n:
     if(p[i] == e[0]):
        p[i] = e[1]
     s = sum(p)
     i += 1

  r += 1
  result = str(s)+"\n"


print(result)

Submission status: Wrong Answer

Solution code:
x = int(input())
ar = list(map(int, input().split()))
sum_val = sum(ar)

unique_val = dict()

for i in ar:
    if i in unique_val:
        unique_val[i] += 1
    else:
        unique_val[i] = 1

q = int(input())
while(q):
    a, b = map(int, input().split())
    if a in unique_val:
        value = (b-a)*unique_val[a]
        sum_val += value
        unique_val[b] = unique_val.pop(a)
        
    print(sum_val)
    q -= 1

print(sum_val)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

L = dict(Counter(a_L))
all_sum = sum(a_L)

print(L)

ans_L = []
for i in range(q):
    b,c = map(int,input().split())
    try:
        diff = (c-b) * L[b]
        all_sum = all_sum+diff
        L[c] += L[b]
    except:
        pass
    ans_L.append(all_sum+1)


for i in ans_L:
    print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
A=list(map(int,input().split()))
Adic={}
for i in A:
  if Adic.get(i)==None:
    Adic[i]=1
  else:
    Adic[i]+=1
Q=int(input())
ope=[]

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

A=np.array(A)

sum=0
for key,value in Adic.items():
  sum+=key*value
for b,c in ope:
  if Adic.get(b)!=None:
    if Adic.get(c)==None:Adic[c]=0
    sum-=b*Adic[b]
    Adic[c]+=Adic.pop(b)
    sum+=c*Adic[c]
    print(sum)
  else:
    print(sum)

Submission status: Wrong Answer

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

from collections import defaultdict
dic = defaultdict(int)
for a in A:
  dic[a] += 1 
  
ans = sum(A)
for i in range(q):  
  b,c = map(int,input().split())
  dic[c] += dic[b]
  diff = dic[c]*c-dic[b]*b
  ans += diff
  print(ans)  
  dic[b] = 0

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())

L = list(map(int,input().split()))
L = Counter(L)

q = int(input())

for i in range(q):
    sum1 = 0
    a,b = map(int,input().split())
    
    if a not in L:
        L[b] = 0
    else:
        L[b] +=L[a] 
    L[a] =0
    #print(L)
    for k,v in L.items():
        sum1 +=k*v
    print(sum1)

Submission status: Wrong Answer

Solution code:
import collections

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

a.sort()

# for i in range(q):
#     if b[i] in a:
#         s = a.index(b[i])
#         while s < n and a[s] == b[i]:
#             a[s] = c[i]
#             s += 1
#     print(sum(a))

sum = sum(a)
x = [0] * (max(c) + 1)

col = collections.Counter(a)
# print(col)

for i in range(q):
    cnt = 0
    if (b[i] in a) or x[b[i]] != 0:
        # cnt = a.count(b[i])
        cnt = col[b[i]]
        sum += (cnt + x[b[i]]) * (c[i] - b[i])
        x[c[i]] += cnt
    print(sum)

Submission status: Wrong Answer

Solution code:
def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1

    return None

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())
A.sort()
j = 0
for i in range(Q):
    while binary_search(A,B[i]) != None:
        id = binary_search(A,B[i])
        if A[id] == B[i]:
            A[id] = C[i]
            A.sort()
    print(sum(A))
    print(A)