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=list(map(int,input().split()))
S=0
B=[0]*10**5
for i in A:
    S+=i
    B[i]+=1  #Bは要素数管理
Q=int(input())
C=[list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
    S=S+(C[i][1]-C[i][0])*B[C[i][0]]
    B[C[i][1]]=B[C[i][0]]+B[C[i][1]]
    B[C[i][0]]=0
    print(S)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
S=0
B=[0]*10**5
for i in A:
    S+=i
    B[i]=A.count(i)  #Bは要素数管理
Q=int(input())
C=[list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
    S=S+(C[i][1]-C[i][0])*B[C[i][0]]
    B[C[i][1]]=B[C[i][0]]+B[C[i][1]]
    B[C[i][0]]=0
    print(S)

Submission status: Runtime Error

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

sam = float(sum(A))
baketu = [0]*(10**5)
for a in A:
    baketu[a] += 1

for q in range(Q):
    sam = sam + baketu[B[q]] * (C[q] - B[q])
    baketu[C[q]] += baketu[B[q]]
    baketu[B[q]] = 0
    print(sam)

Submission status: Runtime Error

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

sam = sum(A)
baketu = [0]*(10**5)
for a in A:
    baketu[a] += 1

for q in range(Q):
    sam = sam + baketu[B[q]] * (C[q] - B[q])
    baketu[C[q]] += baketu[B[q]]
    baketu[B[q]] = 0
    print(sam)

Submission status: Runtime Error

Solution code:
from sys import stdin
input=stdin.readline
input()
a=list(map(int,input().split()))
ans=sum(a)
b=[0]*(10**5)
for i in a:
  b[i]+=1
c=int(input())
for i in range(c):
  n,m=map(int,input().split())
  if b[n]>0:
    x=b[n]
    if x>0:
      b[m]+=x
      b[n]=0
      ans+=(m-n)*x
  print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
input=stdin.readline
input()
a=list(map(int,input().split()))
ans=sum(a)
b=[0]*(max(a)+1)
for i in a:
  b[i]+=1
c=int(input())
for i in range(c):
  n,m=map(int,input().split())
  x=b[n]
  if x>0:
    b[m]+=x
    b[n]=0
    ans+=(m-n)*x
  print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
input=stdin.readline
input()
a=input().strip()
b=int(input())
for i in range(b):
  n,m=map(int,input().split())
  a.replace(str(n),str(m))
  print(sum(a))

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 l in range(q)]
cnt=[0]*100000
Sum=0
for i in range(n):
  cnt[a[i]]+=1
  Sum+=a[i]
for i in range(q):
  Sum+=(bc[i][1]-bc[i][0])*cnt[bc[i][0]]                           
  cnt[bc[i][1]]+=cnt[bc[i][0]]
  cnt[bc[i][0]]=0
  print(Sum)

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 l in range(q)]
cnt=[0]*(n+1)
Sum=0
for i in range(n):
  cnt[a[i]]+=1
  Sum+=a[i]
for i in range(q):
  Sum+=(bc[i][1]-bc[i][0])*cnt[bc[i][1]]                           
  cnt[bc[i][1]]+=cnt[bc[i][0]]
  cnt[bc[i][0]]=0
  print(Sum)

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 = map(int, input().split())
    B.append(b)
    C.append(c)
data = {}
SUM = sum(A)
for i in range(N):
    if A[i] in data:
        data[A[i]] += 1
    else:
        data[A[i]] = 1
for i in range(Q):
    if B[i] in data:
        SUM += data[B[i]]*(C[i]-B[i])
        data[C[i]] += data[B[i]]
        data[B[i]] = 0
    print(SUM)

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 = map(int, input().split())
    B.append(b)
    C.append(c)
data = {}
SUM = sum(A)a
for i in range(N):
    if A[i] in data:
        data[A[i]] += 1
    else:
        data[A[i]] = 1
for i in range(Q):
    if B[i] in data:
        SUM += data[B[i]]*(C[i]-B[i])
        data[C[i]] += data[B[i]]
        data[B[i]] = 0
    print(SUM)

Submission status: Runtime Error

Solution code:
        aList[b - 1] = 0

Submission status: Runtime Error

Solution code:
for i in range(n):
  key = lst[i]
  if key not in dic:
    dic[key] = 0
  dic[key] += 1
#print(dic)
sum = 0
for key, value in dic.items():
  sum += key * value

m = int(input())
for _ in range(m):
  lst_s = list(map(int, input().split()))
  number = dic.get(lst_s[0])
  if number is not None:
    if lst_s[1] in dic:
      dic[lst_s[1]] += number
    else:
      dic[lst_s[1]] = number
    sum += lst_s[1] * number
    sum -= lst_s[0] * number
    dic.pop(lst_s[0])
  print(sum)

Submission status: Runtime Error

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

NumList=[0]*(10**5)

AllSum=sum(A)

for a in A:
  NumList[a]+=1

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

Submission status: Runtime Error

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

NumList=[0]*(10**5)

AllSum=sum(A)

for a in A:
  NumList[a]+=1

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

Submission status: Runtime Error

Solution code:

def sum_dict(d):
    num = 0
    for k, v in d.items():
        num += k * v
    return num


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = []
    for i in range(Q):
        BC.append(tuple(map(int, input().split())))

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

    for b, c in BC:
        d[c] += d[b]
        d[b] = 0
        print(sum_dict(d))

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

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

    sum_num = sum(A)

    count_A = dict() 
    setA = set(A)
    for s in  setA:
        count_A[s] = A.count(s) 

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        if not(C[i] in count_A):
            count_A[C[i]] = 0
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    sum_num = sum(A)
    maxv = max(A)

    count_A = [0]*20000

    setA = set(A)
    for s in  setA:
        count_A[s] = A.count(s) 

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    sum_num = sum(A)
    maxv = max(A)

    count_A = [0]*100000

    for a in  A:
        count_A[a] += 1

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    sum_num = sum(A)
    maxv = max(A)

    count_A = [0]*(1000)

    for a in  A:
        count_A[a] += 1

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    C = []

    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    sum_num = sum(A)
    maxv = max(A)

    count_A = [0]*(100000)

    for a in  A:
        count_A[a] += 1

    for i in range(Q):
        inc = (C[i]-B[i])*count_A[B[i]]
        count_A[C[i]] += count_A[B[i]]
        sum_num += inc
        print(sum_num)

Submission status: Runtime Error

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

a_cnt=[0]*(10**5)

for i in range(len(a)):
	a_cnt[a[i]]+=1

a_goukei=sum(a)

for gyou in range(q):
	a_goukei+=(bc[gyou][1]-bc[gyou][0])*a_cnt[bc[gyou][0]]
	print(a_goukei)
	a_cnt[bc[gyou][1]]+=a_cnt[bc[gyou][0]]
	a_cnt[bc[gyou][0]]=0

Submission status: Runtime Error

Solution code:
from collections import Counter

# print('input >>')
N = input()
As = list(map(int,(input().split())))
Q = int(input())
BCs = []
for _ in range(Q):
    BCs.append(tuple(map(int,(input().split()))))

counter = dict(Counter(As))
sum_ = sum(As)

for BC in BCs:
    sa = BC[1] - BC[0]
    sum_ += counter[BC[0]] * sa
    if not BC[1] in counter:
        counter[BC[1]] = 0
    counter[BC[1]] = counter[BC[1]] + counter[BC[0]]
    counter[BC[0]] = 0
    # print(counter)
    print(sum_)

Submission status: Runtime Error

Solution code:
import collections

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

CNT = collections.Counter(A)
ANS = []
ans = sum(A)

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

for i in ANS:
    print(i)

Submission status: Runtime Error

Solution code:
import collections

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

cnt = collections.Counter(A)
ANS = []
base = sum(A)

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

for i in ANS:
    print(i)

Submission status: Runtime Error

Solution code:
import collections
n = map(int, input().split())
A = list(map(int, input().split()))
q = map(int, input().split())
 
cnt = collections.Counter(A)
ANS = []
base = sum(A)

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

for i in ANS:
  print(i)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
ANS = []
 
for i in range(N):
    base += A[i]

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

for i in ANS:
  print(i)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
ANS = []
 
for i in range(N):
    base += A[i]

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

print(ANS)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

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

print(Ans)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

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

print(Ans)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

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

print(Ans)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)
 
for i in range(N):
    base += A[i]
    
Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])
 
for i in range(Q-1):
  b ,c = map(int, input().split())
  Ans.append(Ans[i] + (c-b) * cnt[b])
  cnt[c] += cnt[b]
  cnt[b] = 0

print(Ans)

Submission status: Runtime Error

Solution code:
import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
B = list(map(int, input().split()))
C = list(map(int, input().split()))
 
base = 0
cnt = collections.Counter(A)
 
for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])
 
for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

Submission status: Runtime Error

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

base = 0
cnt = collections.Counter(A)

for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])

for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

Submission status: Runtime Error

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

base = 0
cnt = collections.Counter(A)

for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])

for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
seq = list(map(int,input().split()))
count = {}
total = sum(seq)
for item in seq:
  count[item] = seq.count(item)

q = int(input())

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

Submission status: Runtime Error

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

As = sorted(list(set(A)))
# 何が何個あるかdict
As_cnt = {}
for a in As:
    As_cnt[a] = A.count(a)

for i in range(Q):
    if B[i] in As_cnt.keys():
        t = As_cnt[B[i]]
        sum_org -= t*diff_BC[i]
    if C[i] in As_cnt.keys():
        As_cnt[C[i]] += t
    else:
        As_cnt[C[i]] = t
    print(sum_org)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int N;
    cin >> N;
    ll ans = 0;
    map<ll, ll> A;
    for (int i = 0; i < N; i++) {
        ll a;
        cin >> a;
        A[a]++;
        ans += a;
    }

    int Q;
    cin >> Q;
    while(Q--) {
        ll b, c;
        cin >> b >> c;
        ll Ab = A[b];
        ll Ac = A[c];

        A[c] += Ab;
        A[b] = 0;

        ans -= Ab * b;
        ans += Ab * c;

        cout << ans << endl;
    }
}

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 i in range(q)]
sum=sum(a)
for i in range(q):
    bc[i][0]=x
    bc[i][1]=y
    c=a.count(x)
    if x in a:
      ind=[j for j,m in enumerate(a) if m == x]
      sum+=(y-x)*c
      for j in ind:
        a[j]=bc[i][1]
    else:
      pass
    print(sum)

Submission status: Runtime Error

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
List=[i for i in range(10**5+1)]
Uf=UnionFind(10**5)
for i in range(Q):
    B,C=map(int,input().split())
    List[B]=C
    Uf.union(B,C)
    for i in Uf.members(B):
        index=i
        while index!=List[index]:
            index=List[index]
            if index==i:
                break
            # print(index)
            # print(List)
        List[i]=index
    ChangedA=[List[a] for a in A]
    print(sum(ChangedA))

Submission status: Runtime Error

Solution code:
n = int(input())
l= list(map(int,input().split()))
c = [0] * 10001
for i in l:
  c[i] += 1
n = int(input())
s = sum(c)
for i in range(n):
  be,af = map(int,input().split())
  s -= be * c[be]
  s += af * c[be]
  c[af] += c[be]
  c[be] = 0
  print(s)

Submission status: Runtime Error

Solution code:
def csum(c):
  r = 0
  for index, item in enumerate(c):
    r += index * item
  return r

n = int(input())
l= list(map(int,input().split()))
c = [0] * 10001
for i in l:
  c[i] += 1
n = int(input())
s = csum(c)
for i in range(0, n):
  be,af = map(int,input().split())
  s -= be * c[be]
  s += af * c[be]
  c[af] += c[be]
  c[be] = 0
  print(s)

Submission status: Runtime Error

Solution code:
def csum(c):
  r = 0
  for index, item in enumerate(c):
    r += index * item
  return r

n = int(input())
l= list(map(int,input().split()))
c = [0] * 10001
for i in l:
  c[i] += 1
n = int(input())
for i in range(0, n):
  be,af = map(int,input().split())
  c[af] += c[be]
  c[be] = 0
  print(csum(c))

Submission status: Runtime Error

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

val = []
heavy = []

for x in A:
    if x in heavy:
        val[heavy.index(x)] += 1
    else:
        heavy.append(x)
        val.append(1)

for i in range(Q):
    B, C = input().split()
    B = int(B)
    C = int(C)
    if C not in heavy:
        heavy.append(C)
        val.append(0)
    val[heavy.index(C)] += val[heavy.index(B)]
    val[heavy.index(B)] = 0
    sum = 0
    for x, y in zip(val, heavy):
        sum += x * y
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
lis=list(map(int,input().split()))
hoge=[0 for i in range(10+1)]
hogesum=[0 for i in range(10+1)]
for i in range(n):
  hoge[lis[i]]+=1
  hogesum[lis[i]]+=lis[i]
m=int(input())
for i in range(m):
  a,b=map(int,input().split())
  hoge[b]+=hoge[a]
  hoge[a]=0
  hogesum[a]=0
  hogesum[hoge[b]]=b*hoge[b]
  print(sum(hogesum))

Submission status: Runtime Error

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())
#print(B,C)
#print(A)
#a = 0
#for i in range(N):
    #a += A[i]
#print(a)
ls = []
for i in range(1,10**5):#10**5):
    ls.append(A.count(i))
#print(ls)
sumA = sum(A)
#print("sumA",sumA)
for j in range(Q):
    #print(B[j]-1)
    #print(ls)
    sumA += (C[j]-B[j])*ls[B[j]-1]
    #print("j","C[j]","B[j]","ls[B[j]]",j,C[j],B[j],ls[B[j]-1])
    ls[C[j]-1] += ls[B[j]-1]
    ls[B[j]-1] = 0
    print(sumA)

Submission status: Runtime Error

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())
#print(B,C)
#print(A)
#a = 0
#for i in range(N):
    #a += A[i]
#print(a)
ls = []
for i in range(1,5):#10**5):
    ls.append(A.count(i))
#print(ls)
sumA = sum(A)
#print("sumA",sumA)
for j in range(Q):
    #print(B[j]-1)
    #print(ls)
    sumA += (C[j]-B[j])*ls[B[j]-1]
    #print("j","C[j]","B[j]","ls[B[j]]",j,C[j],B[j],ls[B[j]-1])
    ls[C[j]-1] += ls[B[j]-1]
    ls[B[j]-1] = 0
    print(sumA)

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 i in range(q)]

sumA = 0
num = [0]*100000
for aa in a:
    num[aa] += 1
    sumA += aa

for b, c in bc:
    if b < 100001 and c <100001:
        sumA = sumA + ((c - b) * num[b])
        num[c] += num[b]
        num[b] = 0
    print(sumA)

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 i in range(q)]

sumA = 0
num = [0]*100000
for aa in a:
    num[aa] += 1
    sumA += aa

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
ma=max(a)
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)

Submission status: Runtime Error

Solution code:
n=int(input())
s=""
l=list(map(int,input().split()))
q=int(input())
d={}
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
    
for i in range(q):
    j,k=map(int,input().split())
    if k in d:
        d[k]+=d[j]
    else:
        d[k]=d[j]
    t=0
    d[j]=0
    for val,key in d.items():
        t+=val*key
    print(t)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
bc = [[0 for i in range(2)] for j in range(Q)]
for i in range(Q):
  bc[i] = list(map(int,input().split()))

count = [0 for i in range(100000)]
for i in range(N):
  count[A[i]] += 1

sum_ = sum(A)
for i in range(Q):
  b,c = bc[i]
  n = count[b]
  sum_ -= b*n
  sum_ += c*n
  count[c] += n
  print(sum_)

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 i in range(1,Q):
    BC.append(list(map(int, input().split())))

num = [0]*10**5

#Aの要素の数を管理するリストnumを作成
for k in range(len(A)):
    num[A[k]+1] += 1

counter = 0 #Biと一致した要素の数
s = sum(A) #合計を初期化

for q in range(Q):
    counter = num[BC[q][0]+1]
    num[BC[q][1]+1] += counter #Ciの数に加える
    num[BC[q][0]+1]=0 #Biと等しい値を0個に
    s += (BC[q][1]- BC[q][0])*counter
    print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def main():
    N = int(readline())
    A = [0] *(10**5)
    total = 0
    for a in map(int, readline().split()):
        total += a
        A[a] += 1
 
 
    Q = int(readline())
    for _ in range(Q):
        b, c = map(int, readline().split())
        total += A[b]*(c-b)
        print(total)
        A[c] += A[b]
        A[b] = 0
 
 
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input(""))
ain=input("").split(" ")
a={1:0,}
c=[]
for i in range(n):
    c+=[int(ain[i])]
s=sum(c)
for i in range(n):
    if (c[i] in a):
        a[c[i]]+=1
    else:
        a[c[i]]=1
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c=+[input("").split(" ")]
    listb+=[int(c[i][0])]
    listc+=[int(c[i][1])]
    if (listb[i] in a and a[listb[i]]!=0):
        s+=(listc[i]-listb[i])*a[listb[i]]
        if (listc[i] in a):
            a[listc[i]]+=a[listb[i]]
        else:
            a[listc[i]]=a[listb[i]]
    
    print(s)

Submission status: Runtime Error

Solution code:
n=int(input(""))
ain=input("").split(" ")
a=[]
for i in range(n):
    a+=[int(ain[i])]
q=int(input(""))
c=[]
listb=[]
listc=[]
for i in range(q):
    c+=[input("").split(" ")]
    listb+=[int(c[i][0])]
    if (listb[i] in a):
        listc+=[int(c[i][1])]
        for k in range(n):
            if (a[k]==listb[i]):
                a[k]=listc[i]
    print(sum(a))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()
#define INF (int)1e9
#define MOD 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
int a[mxN];

int sum(vector<int> v)
{
    int s = 0;
    for (int i = 0; i < v.size(); ++i)
    {
        s += v[i];
    }
    return s;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    vi v(n);
    for (auto &x : v)
        cin >> x;

    int t;
    cin >> t;

    int s = sum(v);
    map<int, int> m;
    for (auto x : v)
        if (m.find(x) != m.end())
            m[x] += 1;
        else
            m[x] = 1;
    while (t--)
    {
        int b, c;
        cin >> b >> c;
        if (m.find(c) != m.end())
            m[c] += m[b];
        else
            m[c] = m[b];
        s += ((c - b) * m[b]);
        m[b] = 0;
        cout << s << "\n";
    }

    return 0;
}

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(str, input().split())
    if int(b) in A:
        A = [int(str(a).replace(b, c)) for a in A]
    print(sum(A))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
numlista=list(map(int,input().split()))
#aの合計を求める(suma)
suma=0
for i in range(n):
  suma+=numlista[i]
#1~10000がそれぞれいくつあるのか調べる(numkosuu)
numkosuu=[]
for i in range(100000):
  numkosuu.append(numlista.count(i+1))
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
#変化量を求める
  k=c-b
  o=numkosuu[b-1]
#変化したところのみ計算する
  suma+=k*o
#個数を更新する
  numkosuu[b]+=numkosuu[b-1]
  numkosuu[b-1]=0
  print(suma)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
numlista=map(int,input().split())
#aの合計を求める
suma=0
for i in range(n):
  suma+=numlista[i]
#
#q=int(input())
#for i in range(q):
#  b,c=int(input().split())
#  k=c-b
print(numlista)
print(suma)

Submission status: Runtime Error

Solution code:
import sys
import time
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

ans = 0
data = [0]*10
for i in a:
  data[i] += 1
  ans += i

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