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: Runtime Error

Solution code:
N = int(input())
A = [i for i in map(int, input().split())]
Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
    if B in A:
        B, C = map(int, input().split())
        A2 = [i for i, x in enumerate(A) if x == B]
        A = [C if i == B else i for i in A]
        print(sum(A))
    else:
        print(sum(A))

Submission status: Runtime Error

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

B = []
C = []

l = {}

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

    B.append(b)
    C.append(c)

res = 0
for i in A:
    if i in l:
        l[i] += 1
    else:
        l[i] = 1

    res += i

t = 0
for i in range(Q):
    t = l[B[i]]

    res += (C[i] - B[i]) * t

    l[B[i]] = 0
    l[C[i]] += t

    print(res)

Submission status: Runtime Error

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 for i in A]
    print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
for i in Q:
  b,c = map(int, input().split())
  cnt = 0
  while b in A:
    A.remove(b)
    A.append(c)
    cnt += 1
  ans += (c-b) * cnt
  
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l={}
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    if i in l:
        l[i]+=1
    else:
        l[i]=1
sumflag=False
for (f,t) in move:
    if f in l:
        if t in l:
            l[t]+=l[f]
            l[f]=0
        else:
            l[t]=l[f]
            l[f]=0
        sum = 0
        for k,v in l.items():
            sum += k*v
        print(sum)
        sumflag=True
    else:
        if sumflag:
            print(sum)
        else:
            for k,v in l.items():
                sum += k*v
                print(sum)
                sumflag=True

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
for _ in range(Q):
    B,C = map(int, input().split())
A.sort()
ans = 0
for a in range(Q) :
    x=C if A=B else 0
    ans = sum(A)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

num_A = [A.count(i) for i in range(10**5)]

for i in range(Q):
    b, c = map(int, input().split())
    num_A[c] += num_A[b]
    num_A[b] = 0
    print(sum(np.array(num_A)*np.array([j for j in range(10**5)])))

Submission status: Runtime Error

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

data = [0] * 100000

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

    if data[B] != 0:
        data[C] += data[B]
        data[B] = 0
 
    print(my_sum(data))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import collections
N = int(input())
A = list(map(int, input().split()))
A_amount = collections.Counter(A)

result = sum(A)

Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
	B_elem = A_amount.get(B, None)
    if B_elem is not None:
        C_elem = A_amount.get(C, None)
        if C_elem is not None:
            A_amount.pop(B_elem)
            A_amount[C] = (C, B_elem[1]+C_elem[1])
        else:
            A_amount.pop(B_elem)
            A_amount[C] = (C, B_elem[1])

        result += (C-B)*B_elem[1]
        
    print(result)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
AA = {}
t = 0
for a in A:
	t += a
	if a in AA:
		AA[a] += 1
	else:
		AA[a] = 1

Q = int(input())
ans = []
for i in range(Q):
	b, c = list(map(int, input().split()))
	if b in AA:
		if c not in AA:
			AA[c] = 0
		AA[c] += AA[b]
		t += (c - b) * AA[b]
		ans += [t]
		del AA[b]
	else:
		ans += [ans[-1]]
[print(a) for a in ans]

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
for _ in range(M):
    B,C = map(int, input().split())
A.sort()
ans = 0
for a in range(Q) :
    x=C if A=B else 0
    ans = sum(A)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)] 
ans = [0]*(10**5)
for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    ans[BC[i][0]] = 0 

    S = 0
    for i in range(maxlen+1):
        S += ans [i]*i
    print(S)

Submission status: Runtime Error

Solution code:
import numpy as np

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

num_A = [A.count(i) for i in range(10)]

for i in range(Q):
    b, c = map(int, input().split())
    num_A[c] += num_A[b]
    num_A[b] = 0
    print(sum(np.array(num_A)*np.array([j for j in range(10)])))

Submission status: Runtime Error

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

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

a_dict = dict()
for num in a:
    if num in a_dict.keys():
        a_dict[num] += 1
    else:
        a_dict[num] = 1

a_key = list(a_dict.keys())
a_value = list(a_dict.values())
for k in range(len(a_dict)):
    ans += a_key[k] * a_value[k]

for j in range(q):
    if info[j][0] in a_dict.keys():
        if info[j][1] in a_dict.keys():
            ans -= info[j][0] * a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            a_dict[info[j][1]] += a_dict[info[j][0]]
            del a_dict[info[j][0]]
        else:
            ans -= info[j][0] * a_dict[info[j][0]]
            a_dict[info[j][1]] = a_dict[info[j][0]]
            ans += info[j][1] * a_dict[info[j][1]]
            del a_dict[info[j][0]]
    ans = 0

    print(ans)

Submission status: Runtime Error

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

operations = []

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for op in operations:
    target, goto = op

    a.sort()

    try:
        index = a.index(target)
        
        while:
            if a[index] != target:
                break;

            a[index] = target
            index += 1

        print(sum(a))

    except:
        print(sum(a))

Submission status: Runtime Error

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

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

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

int_list = {}

for i in a:
    int_list[i] = 0

for i in a:
    int_list[i] += 1

ans = 0
for k, v in int_list.items():
    ans += k*v # 3


for i in range(q):
    ans = 0
    p = int_list[b[i]] # 1 1
    ans = 0
    int_list[b[i]] = 0
    try:
        int_list[c[i]] += p
    except KeyError:
        int_list[c[i]] = p
    for k, v in int_list.items():
        ans += k*v
    
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
for i in range(q):
  a.sort()
  su = 0
  b,c = map(int,input().split())
  if( b in list):
    for j in range(n):
      if(a[j] > b ):
        break
      if(a[j] == b):
        a[j] = c
  print(sum(a))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
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 c not in dic:
        dic.setdefault(c,dic[b])
    else:
        dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    print(answer)

Submission status: Runtime Error

Solution code:
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

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)

for B,C in pala:
    res = 0
    count[C] += count.pop(B)

    for k , v in count.items():
        res += (k * v)
    
    print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
list = input().split()
for a in range(int(input())):
    b, c = input().split()
    list = map((lambda x: replace(b, c)), list)
    list_int = map((lambda x: int(x)), list)
    print(sum(list_int))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
alist = set()
adic = {}
for i in a:
    if int(i) in alist:
        adic[int(i)] = adic[int(i)] + 1
    else:
        alist.add(int(i))
        adic[int(i)] = 1

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

for i in range(q):
    s = input().split()
    b = int(s[0])
    c = int(s[1])
    if b in alist:
        if not(c in alist):
            adic[c] = 0
            alist.add(c)
        adic[c] = adic[c] + adic[b]
        adic[b] = 0
        su = 0
        for j in adic:
            su += j * adic[j]
    ans.append(su)
for i in range(len(ans)):
    print(ans[i])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
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
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

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
s = sum(arr)
d = {}
for i in arr:
    d[i] = arr.count(i)
for _ in range(k):
    v = 0
    a,b = map(int, input().split())
    if a in d:
        d[b] = d[a]
        del d[a]
    v = d[b]
    s = s - (a*v) + (b*v)
    print(s)

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)

for B,C in pala:
    res = 0
    count[C] += count.pop(B)

    for k , v in count.items():
        res += (k * v)
    
    print(res)

Submission status: Runtime Error

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)
Q = int(input())
BC = []
counter = [0]*10**5
uq = set()

for i in range(Q):
    b, c = map(int, input().split())
    uq.add(b)
    uq.add(c)
    BC.append([b, c])

for i in uq:
    counter[i] = A.count(i)
    
for b, c in BC:
    A_sum += (c-b)*counter[b]
    counter[c] += counter[b]
    counter[b] = 0
    print(A_sum)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline


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


sumA = sum(alist)
adict = Counter(alist)

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

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Submission status: Runtime Error

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

if __name__ == '__main__':
  n=int(input())
  A_str=input().strip("\n")
  A=A_str.split()
  q=int(input())

  C={}
  for a in A:
    if a in C:
      C[a]+=1
    else:
      C[a]=1
  
  def re(b,c):
    if c in C:
      C[c]+=C[b]
    else:
      C[c]=C[b] 
    C[b]=0
    #print(b,c,C)

  ans=''
  b,c=input().strip("\n").split()
  tmp=eval(A_str.replace(b,c).replace(" ","+"))
  ans+=str(tmp)
  ans+="\n"
  if b in C:
    re(b,c)
  for i in range(1,q-1):
    b,c=input().strip("\n").split()
    if b in C:
      tmp+=(int(c)-int(b))*C[b]
      re(b,c)
    ans+=str(tmp)+"\n"
  b,c=input().strip("\n").split()
  if b in C:
    tmp+=(int(c)-int(b))*C[b]
  ans+=str(tmp)
  print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    q = int(input())
    key = [i for i in range(10**5+1)]
    val = [0]*(10**5+1)
    dict = dict(zip(key,val))
    dict.update(collections.Counter(a))
    ans = sum(a)

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
alist = set()
adic = {}
for i in a:
    if int(i) in alist:
        adic[int(i)] = adic[int(i)] + 1
    else:
        alist.add(int(i))
        adic[int(i)] = 1

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

for i in range(q):
    s = input().split()
    b = int(s[0])
    c = int(s[1])
    if b in alist:
        if not(c in alist):
            adic[c] = 0
            alist.add(c)
        adic[c] = adic[c] + adic[b]
        adic[b] = 0
        su = 0
        for j in adic:
            su += j * adic[j]
    ans.append(su)

print(ans)

Submission status: Runtime Error

Solution code:

import collections


if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    q = int(input())
    key = [i for i in range(10**5)]
    val = [0]*(10**5)
    dict = dict(zip(key,val))
    dict.update(collections.Counter(a))
    ans = sum(a)

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

Submission status: Runtime Error

Solution code:
n = int(input())

freq = {}
A = list(map(int,input().split()))
for i in range(len(A)):
    freq[A[i]] = freq.get(A[i],0) + 1

ans = 0

q = int(input())

for i in range(q):
    ans = 0
    b,c = map(int,input().split())
    freq[c] += freq[b]
    freq[b] = 0
    for keys,values in freq.items():
        ans += keys*values
    print(ans)

Submission status: Runtime Error

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

numlist = [0] * 10**5
for x in A:
    numlist[x] +=1


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

    numlist[C] += numlist[B]
    numlist[B] = 0

    for j in range(10**5):
        ans = numlist[j] * j + ans

    print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=''
A+=input().split()
Q=int(input())
B=list(input().split() for i in range(Q))
for j in  range(Q):
    A.replace(B[j][0],B[j][1])
    for k in A:
        c=0
        c +=int(k)
        print(c)

Submission status: Runtime Error

Solution code:
la = list(map(int,input().split()))
q = int(input())
lb = [0]*q
lc = [0]*q
ld = [0]*q
for i in range(q):
  lb[i],lc[i] = map(int,input().split())
  ld[i] = lc[i] - lb[i]
s = sum(la)
for i in range(q):
  m = la.count(lb[i])
  s = s + ld[i]*m
  print(s)
  if m!=0:
    la.remove(lb[i])
    for j in range(m):
      la.append(lc[i])

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
alist = set()
adic = {}
for i in a:
    if int(i) in alist:
        adic[int(i)] = adic[int(i)] + 1
    else:
        alist.add(int(i))
        adic[int(i)] = 1

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

for i in range(q):
    s = input().split()
    b = int(s[0])
    c = int(s[1])
    if b in alist:
        if not(c in alist):
            adic[c] = 0
            alist.add(c)
        adic[c] = adic[c] + adic[b]
        adic[b] = 0
        su = 0
        for j in adic:
            su += j * adic[j]
    #ans.append(sum(adic.values()))
    ans.append(su)
    #tt.append(adic)
    #print(adic)

print(ans)
#print(tt)

Submission status: Runtime Error

Solution code:

import collections


if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    q = int(input())
    key = [i for i in range(10**5)]
    val = [0]*(10**5)
    dict = dict(zip(key,val))
    dict.update(collections.Counter(a))
    ans = sum(a)

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

Submission status: Runtime Error

Solution code:
#d
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0] * Q
C = [0] * Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
D = [0] * (10**5)
ans=sum(A)
for i in A:
    D[i]+=1
for x in range(Q):
    D[C[x]]+=D[B[x]]
    ans+=D[B[x]]*(C[x]-B[x])
    print(ans)
    D[B[x]]=0

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)] 
ans = [0]*(10**5)
for i in range (len(A)):
    ans[A[i]] += 1

for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    ans[BC[i][0]] = 0 

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

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))	
Q = int(input())
B = []*Q
C = []*Q
A = np.array(A)

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

for i in range(Q):
    A[A==B[i]]=C[i]
    print(np.sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
changes = []
for i in range(Q):
    changes.append(tuple(map(int, input().split())))

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

for change in changes:
    if change[1] in num:
        num[change[1]] += num[change[0]]
    else:
        num[change[1]] = num[change[0]]
    num[change[0]] = 0
    s = sum([i * c for i, c in num.items()])
    print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(input().split())
Q=int(input())
anlist=[]
def index_Multi(List,liter):
    #Listはリスト本体・literは検索したい文字
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L
BC=[input().split() for i in range(Q)]
for i in range(Q):
  while True:
    A[A.index(BC[i][0])]=BC[i][1]
    if BC[i][0] not in A:
      break
  A=list(map(int,A))
  print(sum(A))
  A=list(map(str,A))

Submission status: Runtime Error

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
q=int(input())
bc=np.array([list(map(int,input().split())) for _ in range(q)])
a.sort()
def find(i):#iは参照番号
  j=0
  while a[j]<bc[i][0]:
    if j==q-1:
      break
    j+=1
  while a[j]==bc[i][0]:
    a[j]=bc[i][1]
    j+=1
    if j==len(a):
      break
  a.sort()
  
for i in range(q):
  find(i)
  print(np.sum(a))

Submission status: Runtime Error

Solution code:
def getCalc(NUM):
    sum = 0
    for i in range(len(NUM)):
        sum += i*NUM[i]


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

max_num = max(max(A), max(B), max(C))

NUM = [0]*max_num + 1

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

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

    print(getCalc(NUM))

Submission status: Runtime Error

Solution code:

import numpy as np

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

npdata = np.arange(10**5+1)

kosuu=np.zeros(10)
kosuumoto=kosuu
for i in range(N):
    a=A[i]
    kosuu[a]+=1
SUM=sum(A)

BC_list=[]

for i in range (Q):
    bc=list(map(int,input().split()))
    BC_list.append(bc)
S=SUM

for i in range(Q):
    b=BC_list[i][0]
    c=BC_list[i][1]
    minus=b*kosuu[b]
    kosuu[c]=kosuu[c]+kosuu[b]
    plus=c*kosuu[c]
    kosuu[b]=0
    S=S-minus+plus

    print(int(S))

Submission status: Runtime Error

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])
        sum+=(u[i][1]-u[i][0])*rev
        print(sum)
    except KeyError:
        print(sum)
    if u[i][1] in z.keys():
        z[u[i][1]]+=rev
    else:
        z[u[i][1]]=rev

Submission status: Runtime Error

Solution code:
#d
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0] * Q
C = [0] * Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
D = [0] * (10**4)
ans=sum(A)
for i in A:
    D[i]+=1
for x in range(Q):
    D[C[x]]+=D[B[x]]
    tmp=D[B[x]]
    ans=ans+tmp*(C[x]-B[x])
    print(ans)
    D[B[x]]=0

Submission status: Runtime Error

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

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

for _ in range(q):
    a = [n.replace(b[i],c[i]) for n in a]
    print(sum(list(map(int, a))))
    i += 1

Submission status: Runtime Error

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)]
dic_A=dict()
sum_A=sum(A)
ans=list()
for i in A:
    if i in dic_A:
        dic_A[i]+=1
    else:
        dic_A[i]=1
for j in range(Q):
    if B[j][0] in dic_A:
        tmp=sum_A+ dic_A[B[j][0]]*(B[j][1]-B[j][0])
        ans.append(tmp)
        sum_A=tmp
        if B[j][1] not in dic_A:
            dic_A[B[j][1]]=dic_A[B[j][0]]
        else:
            dic_A[B[j][1]]+=dic_A[B[j][0]]
        dic_A[B[j][0]]=0
    else:
        ans.append(tmp)
for k in ans:
    print(k)

Submission status: Runtime Error

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

B = []
C = []

l = []

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

    B.append(b)
    C.append(c)

t = 0

for i in A:
    if i > t:
        t = i

for i in range(t + 1):
    l.append(i)
    l[i] = 0

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

for i in range(Q):
    t = l[B[i]]
    l[B[i]] -= t
    l[C[i]] += t

    res = 0
    for num in range(len(l)):
        res += num * l[num]

    print(res)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def find(tuple_list, num, top_idx):
    length = len(tuple_list)

    if length == 0:
        return top_idx, False

    if length == 1:
        hikaku = tuple_list[0][0]
        if num == hikaku:
            return top_idx, True
        elif num < hikaku:
            return top_idx-1, False
        elif num > hikaku:
            return top_idx, False
    
    if length == 2:
        hikaku1 = tuple_list[0][0]
        hikaku2 = tuple_list[1][0]
        if num == hikaku1:
            return top_idx, True
        elif num == hikaku2:
            return top_idx+1, True
        elif num < hikaku1:
            return top_idx-1, False
        elif hikaku1 < num < hikaku2:
            return top_idx, False
        elif hikaku2 < num:
            return top_idx+1, False

    place = int(length/2)
    hikaku = tuple_list[place][0]
    if num == hikaku:
        return top_idx+place, True
    elif num < hikaku:
        return find(tuple_list[:place], num, top_idx)
    elif num > hikaku:
        return find(tuple_list[place+1:], num, top_idx+place+1)


N = int(input())
A = list(map(int, input().split()))
A_amount = list(collections.Counter(A).items())
A_amount = list(sorted(A_amount, key=lambda x:x[0]))

result = sum(A)

Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
    B_idx, is_B_exist = find(A_amount, B, 0)
    if is_B_exist:
        old_tuple = A_amount[B_idx]
        C_idx, is_C_exist = find(A_amount, C, 0)
        if is_C_exist:
            new_tuple = (C, A_amount[B_idx][1]+A_amount[C_idx][1])
            A_amount[C_idx] = new_tuple
            A_amount_before = A_amount[:B_idx]
            A_amount_after = A_amount[B_idx+1:]
            A_amount_before.extend(A_amount_after)
            A_amount = A_amount_before
        else:
            new_tuple = (C, A_amount[B_idx][1])
            if B_idx < C_idx:
                A_amount_1 = A_amount[:B_idx]
                A_amount_2 = A_amount[B_idx+1:C_idx+1]
                A_amount_3 = A_amount[C_idx+1:]
                A_amount_1.extend(A_amount_2)
                A_amount_1.append(new_tuple)
                A_amount_1.extend(A_amount_3)
            else:
                A_amount_1 = A_amount[:C_idx+1]
                A_amount_2 = A_amount[C_idx+1:B_idx]
                A_amount_3 = A_amount[B_idx+1:]
                A_amount_1.append(new_tuple)
                A_amount_1.extend(A_amount_2)
                A_amount_1.extend(A_amount_3)
            A_amount = A_amount_1
    
        result += (C-B)*old_tuple[1]
    
    print(result)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = map(int, input().split())
a = np.array(A)

Q = int(input())
for i in range(Q):
    B,C = map(int, input().split())
    a = np.where(a == B, C)
    print(sum(a))

Submission status: Runtime Error

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l={}
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    if i in l:
        l[i]+=1
    else:
        l[i]=1
for (f,t) in move:
    if t in l:
        l[t]+=l[f]
    else:
        l[t]=l[f]
    l[f]=0
    sum = 0
    for k,v in l.items():
        sum += k*v
    print(sum)

Submission status: Runtime Error

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


def cal_xor(xor_list, rm_ind):
    ans = None
    for i, xi in enumerate(xor_list):
        if i!=rm_ind:
            if ans is not None:
                ans = ans^xi
            else:
                ans = xi
    return ans

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

ans_str = ''

if N==2:
    print("{} {}".format(a[1], a[0]))
else:

    xors = []
    for i in range(N//2):
        xors.append(a[2*i]^a[2*i+1])

    for i, ai in enumerate(a):
        sum_xor = cal_xor(xors, i//2)
        if i%2==0:
            ind = i+1
        else:
            ind = i-1

        ans = sum_xor^a[ind]
        ans_str += str(ans)+' '
    print(ans_str[:-1])