collections.Counter()

  • + 0 comments
    from collections import Counter
    int(input()) # X, the number of shoes, is unused
    shoe_sizes = Counter(map(int, input().split()))
    money = 0
    for _ in range(int(input())):
        buyer = list(map(int, input().split()))
        buyer_size = buyer[0]
        if buyer_size in shoe_sizes:
            shoe_sizes -= Counter([buyer_size])
            money += buyer[1]
    print(money)