#K47507. Truck Package Loading Optimization

    ID: 28213 Type: Default 1000ms 256MiB

Truck Package Loading Optimization

Truck Package Loading Optimization

You are given a truck with a maximum weight limit \(W\) and \(n\) packages with specified weights. Your task is to determine the maximum number of packages that can be loaded onto the truck such that the total weight does not exceed \(W\). The strategy is to load the lightest packages first. Thus, by sorting the package weights in non-decreasing order and then adding them one by one until the weight limit is reached, you can determine the maximum count of packages that can be fit.

For example, if \(W = 50\) and the package weights are \([10, 20, 30, 40, 50]\), then the maximum number of packages you can load is 2.

inputFormat

The input consists of two lines:

  • The first line contains two integers \(W\) and \(n\): the weight limit of the truck and the number of packages.
  • The second line contains \(n\) space-separated integers representing the weights of the packages. If \(n = 0\), this line will be empty.

outputFormat

Output a single integer representing the maximum number of packages that can be loaded onto the truck without exceeding the weight limit \(W\).

## sample
50 5
10 20 30 40 50
2