#B4090. Maximizing Assistance

    ID: 11747 Type: Default 1000ms 256MiB

Maximizing Assistance

Maximizing Assistance

Thief Ah-Fei unexpectedly came into possession of \(w\) units of money and wishes to use it to help as many people as possible. There are \(n\) people in need, and the \(i\)-th person requires \(x_i\) units of money (with \(i = 0, 1, 2, \dots, n-1\)). Determine the maximum number of people Ah-Fei can help without exceeding his budget.

Note: The optimal strategy is to help the people who require the smallest amounts first. Thus, by sorting the required amounts in increasing order and then accumulating them until the sum exceeds \(w\), we obtain the maximum number of people who can be helped.

inputFormat

The input consists of two lines:

  1. The first line contains two integers \(w\) and \(n\) (\(0 \leq w \leq 10^9\), \(1 \leq n \leq 10^5\)), representing the total available money and the number of people in need.
  2. The second line contains \(n\) space-separated integers \(x_0, x_1, \dots, x_{n-1}\) (\(1 \leq x_i \leq 10^9\)), where each \(x_i\) denotes the amount of money required by the \(i\)-th person.

outputFormat

Output a single integer, the maximum number of people that can be helped without exceeding \(w\) units of money.

sample

10 4
4 3 2 1
4