#K8771. Maximum Boxes Shipping

    ID: 37147 Type: Default 1000ms 256MiB

Maximum Boxes Shipping

Maximum Boxes Shipping

You are given a delivery problem where a truck has a weight limit \(W\) and there are \(n\) boxes each with a weight \(w_i\). Your goal is to load as many boxes as possible without exceeding the weight limit. In other words, choose a subset of boxes such that the sum of their weights is at most \(W\), and the number of boxes chosen is maximized.

Hint: Sorting the box weights in ascending order and then summing them until the limit is reached is a common strategy to obtain the optimal solution.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains two integers, \(W\) (the weight limit) and \(n\) (the number of boxes).
  2. The second line contains \(n\) space-separated integers, each representing the weight \(w_i\) of a box.

outputFormat

The output is a single integer printed to standard output (stdout), representing the maximum number of boxes that can be loaded without exceeding the weight limit.

## sample
50
5
10 20 30 40 50
2