Chocolate Buying
InterviewTime limit1sMemory limit128 MB
Given N chocolate types with a cost per piece and a number of cows wanting each, spend a budget B to satisfy as many cows as possible.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Array, Implementation
- Solved
- No attempts yet
Problem
Bessie and the herd love chocolate, so Farmer John is buying them some.
The chocolate store carries () kinds of chocolate, each available in essentially unlimited quantity. Each type costs () per piece, and there are () cows that want that type.
Farmer John has a budget of () to spend on chocolate for the cows. What is the maximum number of cows he can satisfy? Every cow wants exactly one type of chocolate and is satisfied only by that type.
For example, suppose Farmer John has 50 to spend across 5 types of chocolate. Eleven cows in total have the following preferences:
Farmer John cannot buy chocolate type 5, because he does not have enough money. Even if it cost only 50, it would be a wasteful purchase, since it would satisfy just one cow.
Starting from the cheapest chocolate, he can buy 1 piece of type 2 for , leaving ; then 3 pieces of type 1 for , leaving ; then 2 pieces of type 4 for , leaving ; then 2 pieces of type 3 for , leaving .
He therefore satisfies cows.
Input
- Line 1: Two space-separated integers, and .
- Lines 2 to : Line contains two space-separated integers describing chocolate type : and .
Output
- Line 1: A single integer, the maximum number of cows that Farmer John can satisfy.