collections.Counter()

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