#K82862. Maximum Artifact Weight Under Limit
Maximum Artifact Weight Under Limit
Maximum Artifact Weight Under Limit
You are given a collection of artifacts with their respective weights and a storage facility with a maximum weight capacity. Your task is to select a subset of these artifacts such that the total weight is as large as possible without exceeding the capacity. This is a typical subset sum problem often described by the formula: [ \sum_{i \in S} w_i \leq W] where (S) is the set of selected artifacts, (w_i) are the weights, and (W) is the capacity. In this problem, a greedy strategy – sorting the weights in descending order and picking them one by one if they fit – is sufficient to pass the given test cases.
Note: Although the greedy method is not optimal for every subset sum problem, the input constraints and test cases guarantee that this strategy works for the cases you will encounter.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains two space-separated integers: n (the number of artifacts) and W (the maximum storage weight).
- The second line contains n space-separated integers representing the weights of the artifacts. If n is zero, this line will be empty.
outputFormat
Output a single integer to standard output (stdout) which is the maximum total weight of a subset of artifacts that does not exceed W.
## sample5 10
3 4 5 6 7
10
</p>