#P2639. [USACO09OCT] Bessie's Weight Problem G

[USACO09OCT] Bessie's Weight Problem G

Description

Like many of her sisters, Bessie has put on too much weight from eating too much delicious grass from Farmer John’s pasture. So Farmer John has put her on a very strict diet. Each day, she may eat no more than H(5H45,000)H (5 \le H \le 45,000) kilograms of hay. Bessie can eat only whole hay bales; once she starts a bale, she cannot stop until she finishes it. She has a complete list of N(1N500)N (1 \le N \le 500) bales that can be served to her for dinner. Naturally, she wants to eat as much hay as possible. Of course, each bale can be eaten at most once (even if the same weight appears 22 times in the list, that refers to two distinct bales, and each bale can be eaten at most once). Given a list of bale weights Si(1SiH)S_i (1 \le S_i \le H), determine the maximum total amount of hay Bessie can eat without exceeding the diet limit (note that once she starts a bale, she must finish that entire bale).

Input Format

The first line contains two space-separated integers HH and NN.

Lines 22 through N+1N+1, where line i+1i+1 contains a single integer, give the weight SiS_i of the ii-th bale.

Output Format

Output a single integer, the maximum total kilograms of hay Bessie can eat without exceeding the limit.

56 4
15
19
20
21
56

Hint

Input Explanation

There are four bales with weights 15,19,2015, 19, 20, and 2121. Bessie wants to eat as much as she can within the 5656 kilogram limit.

Output Explanation

Bessie can eat 33 bales (with weights 15,20,2115, 20, 21), reaching exactly her 5656 kilogram limit.

Translated by ChatGPT 5