#K73382. Maximum Items in a Box
Maximum Items in a Box
Maximum Items in a Box
You are given a box that can carry a maximum weight of \( W \). You also have \( n \) items, where the \( i\)-th item has a weight of \( w_i \). Your task is to choose a subset of items to pack into the box such that the total weight does not exceed \( W \) while maximizing the number of items packed. Formally, if you choose \( k \) items with weights \( w_{1}, w_{2}, \dots, w_{k} \), they must satisfy:
\( \sum_{i=1}^{k} w_{i} \leq W \)
A greedy strategy that sorts the items in increasing order of weight and then picks the items one by one until adding the next item would exceed \( W \) is an optimal solution for this problem.
inputFormat
The first line of input contains two integers \( n \) and \( W \): the number of items and the maximum allowed weight for the box, respectively. The second line contains \( n \) space-separated integers representing the weights \( w_1, w_2, \dots, w_n \) of the items.
outputFormat
Output a single integer, which is the maximum number of items that can be packed into the box without exceeding the weight limit \( W \).
## sample5 50
10 20 30 15 25
3