collections.Counter()

  • + 0 comments
    from collections import Counter
    X = int(input())
    size = list(map(int, input().split(' ')))
    csize = Counter(size)
    N = int(input())
    sum = 0
    for i in range(N):
        s,price = list(map(int, input().split(' ')))
        if s in csize.keys():
            if csize[s]>0:
                sum += price
                csize[s] -= 1
    

    print(sum)