#C5722. Maximum Hay Bale Storage

    ID: 49403 Type: Default 1000ms 256MiB

Maximum Hay Bale Storage

Maximum Hay Bale Storage

You are given a weight limit \(W\) (in arbitrary units) and \(n\) hay bales with individual weights. Your task is to determine the maximum number of hay bales that can be placed into a barn without exceeding the weight limit \(W\).

Detailed Description:

  • Each hay bale has a specific weight.
  • You must select a subset of these hay bales such that the total weight is less than or equal to \(W\).
  • The objective is to maximize the number of hay bales stored.

Hint: A greedy strategy works here. Sort the hay bales by weight and then add them until adding the next bale would exceed the weight limit.

inputFormat

The input is read from standard input and consists of two lines:

  • The first line contains two integers \(W\) and \(n\), separated by a space, where \(W\) is the maximum allowed weight and \(n\) is the number of hay bales.
  • The second line contains \(n\) space-separated integers representing the weights of the hay bales.

outputFormat

Output a single integer representing the maximum number of hay bales that can be stored without the total weight exceeding \(W\). The output should be written to standard output.

## sample
50 5
10 20 30 40 50
2

</p>