AtCoder Beginner Contest 171
Use collections.defauldict (int)
to count the number of each number
import collections
N = int(input())
A = sorted(map(int, input().split()))
cnt = collections.defaultdict(int)
for a in A:
cnt[a] += 1
ans = sum(A)
Q = int(input())
for _ in range(Q):
B, C = map(int, input().split())
ans += (C - B) * cnt[B]
cnt[C] += cnt[B]
cnt[B] = 0
Will be added later
Recommended Posts