collections.Counter()

  • + 0 comments

    Here is the most optimised and cleaner code (without constraint) for this problem:-

    from collections import Counter
    no_of_shoes = int(input())
    size_list = map(int, input().split(" "))
    total_customers = int(input())
    
    size_dict = Counter(size_list)
    
    earning = 0
    for j in range(total_customers):
        i = list(map(int, input().split(" ")))
        if size_dict[i[0]]:
            earning += i[1]
            size_dict[i[0]] -= 1
    
    print(earning)
    

    `