collections.Counter()

  • + 0 comments
    from collections import Counter
    '''
    # method 1 without Counter:
    def total_money_earned(N,shoes_sizes):
        total = 0
        shoe_size_price = []
        for _ in range(N):
            shoe_size_price = list(map(int,input().split()))
            if shoe_size_price[0] in shoe_sizes:
                total += shoe_size_price[1]
                shoe_sizes.remove(shoe_size_price[0])
                shoe_size_price.clear()
        return total
    '''
    # method 2 with Counter:
    def total_money_earned(N,shoes_sizes):
        total = 0
        shoes= Counter(shoes_sizes)
        shoe_size_price = []
        for _ in range(N):
            size, price = map(int, input().split())
            if shoes[size]: 
                total += price
                shoes[size] -= 1
        return total
    
    if __name__ == "__main__":
        X = int(input())
        shoe_sizes = list(map(int, input().split()))
        N = int(input())
        print(total_money_earned(N,shoe_sizes))