TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: 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.

Submission attempts (in order):
N = int(input())
A = list(map(int,input().split()))

num_count = [0]*(10**5+1)
tmp_sum = sum(A)
for i in A:
    num_count[i] += 1

for q in range(int(input()):
    B,C = map(int,input().split())
    gain = C*(num_count[B])
    loss = B*(num_count[B])
    num_count[C] += num_count[B]
    num_count[B] = 0
    tmp_sum = tmp_sum + gain - loss
    print(tmp_sum)


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

num_count = [0]*(10**5+1)
tmp_sum = sum(A)
for i in A:
    num_count[i] += 1

for q in range(int(input())):
    B,C = map(int,input().split())
    gain = C*(num_count[B])
    loss = B*(num_count[B])
    num_count[C] += num_count[B]
    num_count[B] = 0
    tmp_sum = tmp_sum + gain - loss
    print(tmp_sum)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
arr = list(map(int, input().split()))

su = sum(arr)

q = int(input())
for i in range(q):
	b,c = map(int, input().split())
    si = su -b + c
    print(si)


def ad(a,bank):
	if a not in bank:
		bank[a] = 1
	else:
		bank[a] += 1

def rp(a,b,bank):
	ans = 0
	if a in bank:
		t = bank[a]
		ans = t
		bank[a] = 0
		if b not in bank:
			bank[b] = t
		else:
			bank[b] += t
		
	return ans
# your code goes here
n = int(input())
arr = list(map(int, input().split()))

bank = dict()
su = 0

for a in arr:
	ad(a,bank)
	su += a

q = int(input())
for i in range(q):
	b,c = map(int, input().split())
	t = rp(b,c,bank)
	if b in bank:
		su = su - (t*b) + (t*c)
	
	print(su)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
mport sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
 = int(input())
A = list(map(int,(input().split())))
Q = int(input())
num_list = [0]*(N+1)
s = []
for i in range(N):
    num_list[A[i]] += 1
for _ in range(Q):
    s_s = 0
    B,C = map(int,(input().split()))
    num_list[C] += num_list[B]
    num_list[B] = 0
    for j in range(N+1):
        s_s += (j)*num_list[j]
    s.append(s_s)
for i in range(Q):
    print(s[i])


import sys,collections
input = sys.stdin.readline

N=input().rstrip()
A=list(map(int,input().split()))
Ac = collections.Counter(A)
Q = int(input())
for i in range(Q):
    B,C = list(map(int,input().split()))
    nb = Ac[B]
    Ac[C] += Ac[B]
    Ac[B] = 0

    if i==0:
        ans = 0
        for key,val in Ac.items():
            ans += key*val
    else:
        ans +=nb*C - nb*B
    
    print(ans)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
C = sum(A)
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]
        D.append(C)
        if x == y:
            continue
        Z[y] = 1*Z[x]
        Z.pop(x)
    else:
        D.append(C)
for d in D:
    print(d)


N = int(input())
A = list(map(int, input().split()))
C = sum(A)
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]
        D.append(C)
        if y in Z:
            Z[y] += 1*Z[x]
        else:
            Z[y] = Z[x]
        Z.pop(x)
    else:
        D.append(C)
for d in D:
    print(d)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import collections
import random
inp = list(range(1, 100001))
dic = dict(collections.Counter(inp))
wa = 0
for i, j in dic.items():
	wa += i * j
se = set(dic.keys())
for i in range(int(input())):
	a = random.randrange(1, 10001)
	b = random.randrange(1, 10001)
	if a in se:
		wa += dic[a] * (b-a)
		if b in se:
			pass
		else:
			dic[b] = 0
		se.add(b)
		dic[b] += dic[a]
		dic[a] = 0
	print(wa)

import collections
N = int(input())
inp = list(map(int,input().split()))
dic = dict(collections.Counter(inp))
wa = 0
for i, j in dic.items():
	wa += i * j
se = set(dic.keys())
for i in range(int(input())):
	a, b = map(int,input().split())
	if a in se:
		wa += dic[a] * (b-a)
		if b in se:
			pass
		else:
			dic[b] = 0
		se.add(b)
		dic[b] += dic[a]
		dic[a] = 0
	print(wa)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())

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

sort(l)

m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    l = [b if j == a else j for j in l]

    print(sum(l))

#from collections import defaultdict

n = int(input())
cnt =[0]*1000000

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

for i in l:
    cnt[i] += 1
    
sums = sum(l)
    
m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    diff = (b-a)*cnt[a]
    sums += diff
    print(sums)
    
    cnt[b] += cnt[a]
    cnt[a] = 0

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import numpy as np


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

sumA = sum(A)
cnt_num = np.zeros(10**5 + 1)

for i in A:
    cnt_num[i] += 1

for i in range(Q):
    B, C = map(int, input().split())
    sumA += (C - B) * cnt_num[B]
    print(sumA)
    cnt_num[C] += cnt_num[B]
    cnt_num[B] = 0

import numpy as np


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

sumA = sum(A)
cnt_num = np.zeros(10**5 + 1)

for i in A:
    cnt_num[i] += 1

for i in range(Q):
    B, C = map(int, input().split())
    sumA += (C - B) * cnt_num[B]
    print(int(sumA))
    cnt_num[C] += cnt_num[B]
    cnt_num[B] = 0
    

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
m = {}
tot = 0
for a in map(int,input().split()):
  tot += m
  if a not in m:
    m[a]=0
  m[a] += 1
q = int(input())
for step in range(q):
  b,c = map(int,input().split())
  if c not in m:
    m[c] = 0
  if b in m:
    m[c] += m[b]
    tot += m[b]*(c-b)
    m[b] = 0
  print(tot)



n = int(input())
m = {}
tot = 0
for a in map(int,input().split()):
  tot += a
  if a not in m:
    m[a]=0
  m[a] += 1
q = int(input())
for step in range(q):
  b,c = map(int,input().split())
  if c not in m:
    m[c] = 0
  if b in m:
    m[c] += m[b]
    tot += m[b]*(c-b)
    m[b] = 0
  print(tot)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    _ = list(map(int, input().split()))
    c.append(_[1])
    b.append(_[0])

s = sum(a)


hist = {}
for i in range(n):
    if a[i] in hist:
        hist[a[i]] += 1
    else:
        hist[a[i]] = 1
print(hist)

for i in range(q):
    diff = c[i] - b[i]
    if b[i] in hist:
        s += hist[b[i]]*diff
        print(s)
        if c[i] in hist:
            hist[c[i]] += hist[b[i]]
        else:
            hist[c[i]] = hist[b[i]]
            del hist[b[i]]
    else:
        print(s)
    # print(hist)


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

s = sum(a)


hist = {}
for i in range(n):
    if a[i] in hist:
        hist[a[i]] += 1
    else:
        hist[a[i]] = 1
# print(hist)

for i in range(q):
    diff = c[i] - b[i]
    if b[i] in hist:
        s += hist[b[i]]*diff
        print(s)
        if c[i] in hist:
            hist[c[i]] += hist[b[i]]
            del hist[b[i]]
        else:
            hist[c[i]] = hist[b[i]]
            del hist[b[i]]
    else:
        print(s)
    # print(hist)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

for _ in range(Q):
  B, C = map(int, input().split())
  count = A.count(B)
  if count >0:
    A.remove(B)
    A.extend([C]*count)
  print(sum(A))

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
cnt = [0]*(10**5+1)

s = sum(A)
for i in A:
  cnt[i] += 1

for _ in range(Q):
  B, C = map(int, input().split())
  s += (C-B)*cnt[B]
  print(s)
  cnt[C] += cnt[B]
  cnt[B] = 0

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100000
for i in range(A):
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100001
for i in A:
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
H,W,K=map(int,input().split())
c=[list(input()) for i in range(H)]
b=0
for i in range(H):
  b+=c[i].count('#')
if b==K:
  print(1)
  exit()
  
else:
  a=K-b
  count=0
  hc=[]
  wc=[]
  for i in range(2 ** H):
    h=['0']*H 
    for j in range(H):
        if ((i >> j) & 1):
            h[H- 1 - j] = '1'
    hc.append(h)
  for i in range(2 ** W):
    w=['0']*W
    for j in range(W):
        if ((i >> j) & 1):
            w[W- 1 - j] = '1'
    wc.append(w)
  
  for k in range(len(hc)):
    for l in range(len(wc)):
      c1=list(c)
      for m in range(H):
        if hc[k][m]=='0':
          c1[m]='.'
        for n in range(W):
          if wc[l][n]=='0' and c1[m][n]=='#':
                c1[m][n]=c1[m][n].replace('#','.')
      for i in range(H):
        b+=c1[i].count('#')
        print(c1)
      if b==a:
        count+=1
        
print(count)
    
            
  

import collections

N=int(input())
A=list(map(int, input().split()))
Q=int(input())
countA=collections.Counter(A)
ans=sum(A)

for i in range(Q):
    B,C=map(int, input().split())
    b=countA[B]
    ans=ans+(C-B)*b
    print(ans)
    countA[B]=0
    countA[C]+=b



Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (max(A)+1)
for i in range(len(A)):
    print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)



N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (max(A)+1)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)



N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)



N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5+1)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
def main():
    N = int(input())
    A = input()
    Q = input()

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

    operateList = (open(0).read().split("\n"))[:-1]

    for operate in operateList:
        B, C = operate.split()
        numB = A.count(B)

        answer = answer + (int(C) - int(B)) * numB
        print(answer)

        A = A.replace(B, C)

if __name__ == "__main__":
    main()

def main():
    N = int(input())
    A = input()
    Q = input()

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

    operateList = (open(0).read().split("\n"))[:-1]
    print(operateList)

    for operate in operateList:
        B, C = operate.split()
        numB = A.count(B)

        answer = answer + (int(C) - int(B)) * numB
        print(answer)

        A = A.replace(B, C)

if __name__ == "__main__":
    main()

def main():
    N = int(input())
    A = input()
    Q = input()

    answer = sum(list(map(int, A.split())))
    A = A.replace(" ", "")

    operateList = (open(0).read().split("\n"))[:-1]

    for operate in operateList:
        try:
            B, C = operate.split()
            numB = A.count(B)

            answer = answer + (int(C) - int(B)) * numB
            print(answer)

            A = A.replace(B, C)

        except:
            pass

if __name__ == "__main__":
    main()

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

    answer = sum(A)
    numNumberList = [0] * 100000
        
    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

import sys

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

    answer = sum(A)
    setA = set(A)
    numNumberList = [0] * 100001

    for Ai in A:
        numNumberList[Ai] = numNumberList[Ai] + 1    

    for i in range(Q):
        B, C = list(map(int, sys.stdin.readline().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = [0] * q
c = [0] * q
ans = sum(a)
number = [0] * 100000
for i in range(q):
    b[i],c[i] = map(int,input().split())
for i in range(n):
    number[a[i]] += 1
for i in range(q):
    if number[b[i]] > 0:
        ans += (c[i] - b[i]) * number[b[i]]
        number[c[i]] = number[b[i]]    
        number[b[i]] = 0
    print(ans)

n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = [0] * q
c = [0] * q
ans = sum(a)
number = [0] * 100001
for i in range(q):
    b[i],c[i] = map(int,input().split())

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

for i in range(q):
    if number[b[i]] > 0:
        ans += (c[i] - b[i]) * number[b[i]]
        number[c[i]] += number[b[i]]    
        number[b[i]] = 0
    print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]

asum=sum(a) 
d={}

for i in a:
  if i in d:
    d[i]+=1
  else:
    d[i]=1

for b,c in bc:
  print(b)
  print(c)
  if b in d:
    asum+=d[b]*(c-b)
    if c in d:
      d[c]+=d[b]
    else:
      d[c]=d[b]
    d[b]=0
  print(asum)

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

asum=sum(a) 
d={}

for i in a:
  if i in d:
    d[i]+=1
  else:
    d[i]=1

for b,c in bc:
  if b in d:
    asum+=d[b]*(c-b)
    if c in d:
      d[c]+=d[b]
    else:
      d[c]=d[b]
    d[b]=0
  print(asum)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)
    

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

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import collections
N = int(input())
A = list(map(int, input().split()))
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  print(B, C)
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print(sum(A))

N = int(input())
A = map(int, input.split())
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(input())
for _ in Q:
  B, C = map(int, input.split())
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print(sum(A))

import collections
N = int(input())
A = list(map(int, input().split()))
count = collections.defaultdict(int)
currSum = 0
for a in A:
  count[a] += 1
  currSum += a
Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  currSum += (count[B] * (C - B))
  count[C] += count[B]
  count[B] = 0
  print(currSum)

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,dic[b])
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    print(answer)

a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    dic.setdefault(a[i],1)
    dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,dic[b])
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    if a[i] in dic:
        dic[a[i]]+=1
    else:
        dic.setdefault(a[i],1)
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if b not in dic:
        dic.setdefault(b,0)
    elif c not in dic:
        dic.setdefault(c,dic[b])
    else:
        dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
from collections import Counter

import sys

readline = sys.stdin.buffer.readline

def solver(int N, list A, int Q, list BC):
    d = Counter(A)

    sub_s = sum(A)

    for b, c in BC:
        sub_s += d[b] * (c - b)
        d[c] += d[b]
        d[b] = 0

        print(sub_s)


def run():
    N = int(readline())
    A = list(map(int, readline().split()))

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

    solver(N, A, Q, BC)

run()


import sys
from collections import Counter

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = tuple(map(int, readline().split()))
Q = int(readline())
BC = map(int, read().split())

d = Counter(A)

sub_s = sum(A)

for b, c in zip(BC, BC):
    sub_s += d[b] * (c - b)
    d[c] += d[b]
    d[b] = 0

    print(sub_s)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import sys
import collections

ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
il = list(set(a))
li = collections.Counter(a)
q = ri()
res = []
bc = [rm() for _ in range(q)]
for vvv in bc:
    b, c = vvv
    if b in il:
        if c in il:
            li[c] += li[b]
        else:
            li[c] = li[b]
            il.append(c)
        li[b] = 0
        il.remove(b)
    ans = 0
    for k, v in li.items():
        ans += k*v
    res.append(ans)
for aaa in res:
    print(aaa)



import sys
import collections

ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
il = list(set(a))
li = collections.Counter(a)
q = ri()
res = []
for _ in range(q):
    b, c = rm()
    if b in il:
        if c in il:
            li[c] += li[b]
        else:
            li[c] = li[b]
            il.append(c)
        li[b] = 0
        il.remove(b)
    ans = 0
    for k, v in li.items():
        ans += k*v
    res.append(ans)
for aaa in res:
    print(aaa)



import sys
import collections

ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
il = list(set(a))
li = collections.Counter(a)
q = ri()
for _ in range(q):
    b, c = rm()
    if b in il:
        if c in il:
            li[c] += li[b]
        else:
            li[c] = li[b]
            il.append(c)
        li[b] = 0
        il.remove(b)
    ans = 0
    for k, v in li.items():
        ans += k*v
    print(ans)



import sys
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
t = sum(a)
cnt_li = [0]*10**5
for i in a:
    cnt_li[i-1] += 1

for _ in range(ri()):
    b, c = rm()
    t += (c-b)*cnt_li[b-1]
    print(t)
    cnt_li[c-1] += cnt_li[b-1]
    cnt_li[b-1] = 0

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())

ans = sum(a)
A = Counter(a).most_common()
d = [0] * 10**5+10
for i in range(len(A)):
    d[A[i][0]] = A[i][1]

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

for i in range(q):
    d[C[i]] += d[B[i]]
    cnt = d[B[i]]
    ans += (C[i]-B[i])*cnt
    print(ans)
    d[B[i]] = 0

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())

ans = sum(a)
A = Counter(a).most_common()
d = [0] * (10**5+10)
for i in range(len(A)):
    d[A[i][0]] = A[i][1]

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

for i in range(q):
    d[C[i]] += d[B[i]]
    cnt = d[B[i]]
    ans += (C[i]-B[i])*cnt
    print(ans)
    d[B[i]] = 0

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
from collections import Counter

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

dic = Counter(A)

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if b in dic.keys():
        ans = sumA+(dic[b]*(c-b))

        B = dic[b]
        if c in dic.keys():
            dic[c] += dic[b]
            del dic[b]
        else:
            dic[c] = dic[b]
            del dic[b]
        
        sumA += (dic[c]*c-B*b)
        
        # print(dic)
    print(ans)

from collections import Counter

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

cnt = [0 for _ in range(100005)]
for i in range(N):
    cnt[A[i]] += 1

sumA = sum(A)

# print(sumA)

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


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = [0] * 100001
for i in A:
    L[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    S += (C - B) * L[B]
    L[C] += Ct[B]
    L[B] = 0
    print(S)

N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = [0] * 100001
for i in A:
    L[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    S += (C - B) * L[B]
    L[C] += L[B]
    L[B] = 0
    print(S)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
BC = [list(map(int,input().split())) for _ in range(q)]
A = np.array(A)
 
def main(A,BC):
    for bc in BC:
        A = np.where(A==bc[0],bc[1],A)
        print(max(A))
        
if __name__ == '__main__':
    main(A,BC)

import numpy as np
from numba import njit

n = int(input())
A = list(map(int,input().split()))
q = int(input())
BC = [list(map(int,input().split())) for _ in range(q)]
A = np.array(A)

ans = sum(A)


a_num = [0]*10**5
for  a in A:
    a_num[a-1]+=1

for bc in BC:
    change = bc[1]-bc[0]
    ans += change*a_num[bc[0]-1]
    a_num[bc[1]-1] += a_num[bc[0]-1]
    a_num[bc[0]-1] = 0
    print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    if B in u:
        posB = np.where(u==B)
        asum += int(count[posB]) * (C-B)
        print(u)
        print(count)
        if not (C in u):
            u[posB] = C
        else :
            posC = np.where(u==C)
            count[posC] += count[posB]
            u = np.delete(u,posB)
            count = np.delete(count,posB)
    # print(asum)
    # print(u)
    print(count)



import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
L = np.zeros(10**5,int)
for i in range(len(u)):
    L[u[i]-1] = count[i]
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    asum += L[B-1] * (C-B)
    L[C-1] += L[B-1]
    L[B-1] = 0
    print(asum)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import bisect

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

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b 
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())
B_len = len(B)
ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  jg = False
  for j in range(B_len):
    if X[j][0] == y:
      jg = True
      jgx = j
  for j in range(B_len):
    if X[j][0] == x:
      X[j][0] = y
      if jg == True:
        X[j][1] += X[jgx][1]
        X[jgx][0] = 0
        B_len = B_len-1
  for j in range(B_len):
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)
  



import bisect

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

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b 
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  jg = False
  for j in range(len(B)):
    if X[j][0] == y:
      jg = True
      jgx = j
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
      if jg = True:
        X[j][1] += X[jgx][1]
        X[jgx][0] = 0
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)
  



N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  del dic[B[q]]
  print(ans)



N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int,input().split()) for _ in range(Q)]
B, C = [i for i in zip(*BC)]
#print(B,C)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1

ans = sum(A)
for q in range(Q):
  bc = C(q) - B(q)
  ans = ans + bc * dic.get(B(q),0)
  dic[C(q)] = dic.get(C(q),0) + dic.get(B(q),0)
  dic[B(q)] = 0
  print(ans)


N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int,input().split()) for _ in range(Q)]
B, C = [i for i in zip(*BC)]
#print(B,C)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1

ans = sum(A)
for q in range(Q):
  bc = C[q] - B[q]
  ans = ans + bc * dic.get(B[q],0)
  dic[C[q]] = dic.get(C[q],0) + dic.get(B[q],0)
  dic[B[q]] = 0
  print(ans)


Verdicts: Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
import sys

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

import re
def solver(N,A,Q,BC):

    for b,c in BC:
        #print(A,':',b,'->',c)
        bef = '[\s]*'+str(b)
        aft = ' '+str(c)
        #print("bef,aft=",bef,aft)
        A = re.sub(bef, aft, A)
        #A = A.replace(' '+str(b), ' '+str(c))
        #print(A)
        print(sum(map(int,A.split())))


def main():
    N = I()
    #A = LI()
    A = L()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    print("N,Q=",N,Q)
    print("A=",A)
    print("BC=",BC)

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()


import sys

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

import collections

def solver(N,A,Q,BC):
    T = sum(map(int,A))
    #print(T)

    Nums = collections.Counter(A)
    #print(Nums)
    for b,c in BC:
        diff = c - b

        T += Nums[b] * diff
        print(T)

        Nums[c] += Nums[b]
        Nums[b] = 0


def main():
    N = I()
    A = LI()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())+1
a = list(map(int, input().split()))
cnt = []

for i in range(100005):
    cnt.append(0)
sum = 0
for i in range(n):
    cnt[i] = a.count(i)
    sum += i * cnt[i]

q = int(input())
ans = []

for i in range(q):
    b, c = map(int, input().split())
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    ans.append(sum)

n = int(input())+1
a = list(map(int, input().split()))
cnt = []
for i in range(100005):
    cnt.append(0)
sum = 0



for i in a:
    cnt[i] += 1
for i in range(100005):
    sum += i * cnt[i]

q = int(input())
ans = []
for i in range(q):
    b, c = map(int, input().split())
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    ans.append(sum)

for i in ans:
    print(i)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
from collections import defaultdict
from collections import Counter
from numba import jit

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit('void(i8, i8[:], i8[:], i8, i8[:])')
def solve(n, a, arr, q, bc):
    for k, v in Counter(a).items():
        arr[k] = v

    q = int(input())

    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))


solve(n, a, arr, q, bc)

from collections import defaultdict
from collections import Counter
from numba import jit

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit('void(i8, i8[:], i8[:], i8, i8[:])')
def solve(n, a, arr, q, bc):
    for k, v in Counter(a).items():
        arr[k] = v

    q = int(input())

    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))

from collections import defaultdict
from collections import Counter
from numba import jit

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))


@jit
def solve(n, a, arr, q, bc):
    for k, v in Counter(a).items():
        arr[k] = v

    q = int(input())

    for b, c in bc:
        arr[c] += arr[b]
        arr[b] = 0
        print(sum([x * y for x, y in arr.items()]))


solve(n, a, arr, q, bc)

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:
    arr[c] += arr[b]
    tmp = arr[b]
    arr[b] = 0
    whole += (c - b) * tmp
    print(whole)

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

for i in A:
    data[i] += 1

sum_val = my_sum(data)

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

    sum_val = sum_val - (data[B] * B) + ((data[C] + data[B]) * C)

    data[C] += data[B]
    data[B] = 0

    print(sum_val)


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

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

for i in A:
    data[i] += 1

sum_val = my_sum(data)

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

    sum_val = sum_val - (data[B] * B) + (data[B] * C)

    data[C] += data[B]
    data[B] = 0

    print(sum_val)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
from collections import Counter

n = int(input())
count = Counter(input().split())
ans = 0
for v, c in count.items():
    ans += int(v) * c
for i in range(int(input())):
    b, c = input().split()
    if b in count:
        ans -= count.get(b, 0) * int(b)
        count[c] = count[b] + count.get(c, 0)
        count[b] = 0
        ans += count.get(c, 0) * int(c)
    print(ans)

from collections import Counter

n = int(input())
count = Counter(input().split())
for i in range(int(input())):
    b, c = input().split()
    if b in count:
        count[c] = count[b] + count.get(c, 0)
        count[b] = 0
ans = 0
for v, c in count.items():
    ans += int(v) * c
print(ans)

from collections import Counter

n = int(input())
count = Counter(input().split())
ans = 0
for v, c in count.items():
    ans += int(v) * c
for i in range(int(input())):
    b, c = input().split()
    if b in count:
        ans -= count.get(b, 0) * int(b)
        prev = count.get(c, 0)
        count[c] = count[b] + count.get(c, 0)
        count[b] = 0
        ans += (count.get(c, 0) - prev) * int(c)
    print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
a = list(map(int,input().split()))
ans = sum(a)

import collections
cca = collections.Counter(a)

for i in range(int(input())):
    b,c = map(int,input().split())
    num = cca[b]
    cca[b] = 0
    cca[c] += num 
    
    print(ans - num*(b-c))

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

import collections
cca = collections.Counter(a)

for i in range(int(input())):
    b,c = map(int,input().split())
    num = cca[b]
    cca[b] = 0
    cca[c] += num 
    ans -= num*(b-c)
    
    print(ans)

Verdicts: Wrong Answer

Accepted