#P2639. Bessie's Diet Plan

    ID: 15906 Type: Default 1000ms 256MiB

Bessie's Diet Plan

Bessie's Diet Plan

Bessie, like many of her sisters, has gained excess weight from enjoying too much of Farmer John's delicious grass. To help her lose weight, Farmer John imposes a strict diet: she can eat at most $H$ kilograms of dry hay per day. Bessie can only eat an entire bale of hay at once; once she starts a bale, she must finish it completely. She has a list of $N$ hay bales available for dinner, where the weight of each bale is given by $S_i$. Each bale can only be eaten once (even if the same weight appears more than once, those represent different bales).

The task is to determine the maximum total weight of hay that Bessie can consume without exceeding her daily limit. Note that Bessie either consumes an entire bale or not at all.

Constraints:

  • $5 \leq H \leq 45000$
  • $1 \leq N \leq 500$
  • $1 \leq S_i \leq H$

inputFormat

The first line contains two integers $H$ and $N$, representing the maximum kilograms of hay Bessie can eat and the number of hay bales available, respectively. The second line contains $N$ integers, where each integer denotes the weight of a bale of hay.

outputFormat

Output a single integer representing the maximum total weight of hay Bessie can eat without exceeding the limit $H$.

sample

50 3
10 20 30
50