Problem 666
Maximum Ice Cream Bars
Medium · 20 pts · Array, Greedy, Sorting
Problem
A shop sells N ice cream bars, and bar i costs COSTS[i] coins. A boy has COINS coins and wants to buy as many bars as possible, in any order, buying each bar at most once. Find the maximum number of bars he can buy. Buying the cheapest bars first is always best, so sort the prices (or count them, since prices are small) and buy until the next bar is too expensive.
Input
The first line contains N and COINS. The second line contains the N costs.
Output
Print the maximum number of ice cream bars.
Constraints
1 <= N <= 100000; 1 <= COSTS[i] <= 100000; 1 <= COINS <= 10^8
Example input
5 7 1 3 2 4 1
Example output
4
Explanation
With costs 1 3 2 4 1 and 7 coins, buying the bars costing 1, 1, 2 and 3 uses exactly 7 coins. A fifth bar would cost 4 more, so the answer is 4.