collections.Counter()

  • + 0 comments
    from collections import Counter
    x = int(input())
    n = list(map(int,input().split()))
    m = int(input())
    sh =[]
    for i in range(m):
        sh.append(  list( map(int,input().split())) )
    
    dic = Counter(n)
    total =0
    
    for cust, price in sh:
        if dic[cust] > 0:  # Check if the requested size is available
            total += price
            dic[cust] -= 1  # Decrease the count for the size
            if dic[cust] == 0:  # If count becomes zero, remove the size
                del dic[cust]
    
    print(total)