#K58942. Max Stackable Goods
Max Stackable Goods
Max Stackable Goods
You are given n goods, where each good has a certain height. Your task is to determine the maximum number of goods that can be stacked such that the total height does not exceed a given limit H. More formally, let the heights be represented as \(h_1, h_2, \dots, h_n\) and the condition for a valid selection of goods is:
\(\sum_{i=1}^{k} h_{i} \leq H\)
You need to select as many goods as possible under this constraint. A greedy approach by sorting the heights in ascending order proves to be optimal. This problem tests your ability to implement greedy algorithms and handle basic input-output processing.
inputFormat
The input is given via standard input. The first line contains two integers, n (the number of goods) and H (the maximum allowed height), separated by a space. The second line contains n integers representing the heights of the goods.
outputFormat
Output a single integer on standard output, which is the maximum number of goods that can be stacked without exceeding the height limit H.## sample
5 15
3 7 5 6 9
3
</p>