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):
# -*- 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)

# -*- 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)

# -*- 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)

# -*- coding: utf-8 -*-
import collections
N = int(input())
A = list(map(int, input().split()))
A_amount = dict(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)
            A_amount[C] = B_elem+C_elem
        else:
            A_amount.pop(B)
            A_amount[C] = B_elem

        result += (C-B)*B_elem
    print(A_amount)

    print(result)

# -*- coding: utf-8 -*-
import collections
N = int(input())
A = list(map(int, input().split()))
A_amount = dict(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)
            A_amount[C] = B_elem+C_elem
        else:
            A_amount.pop(B)
            A_amount[C] = B_elem

        result += (C-B)*B_elem

    print(result)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted