#C14322. Optimize Container Loading
Optimize Container Loading
Optimize Container Loading
You are given a truck with a maximum weight capacity of M and a collection of containers, each with a specific weight. Your task is to load as many containers as possible onto the truck without exceeding the weight limit.
Formally, given an array of container weights W = [w_1, w_2, \ldots, w_n] and a maximum weight limit \(M\), determine the maximum number of containers that can be loaded such that the sum of the weights of the selected containers does not exceed \(M\). A natural strategy is to load containers in increasing order of weight to maximize the number loaded, i.e., find the largest k such that \(\sum_{i=1}^{k} w_{(i)} \le M\), where \(w_{(i)}\) represents the \(i\)th smallest weight.
inputFormat
The input is provided via standard input (stdin) and consists of two lines:
- The first line contains two integers:
n
(the number of containers) andmax_weight
(the truck's maximum weight capacity). - The second line contains
n
space-separated integers representing the weights of the containers.
For example:
5 10 2 3 7 1 4
outputFormat
Output a single integer, which is the maximum number of containers that can be loaded without exceeding the weight capacity, printed to standard output (stdout).
## sample5 10
2 3 7 1 4
4