#P2925. Hay Bale Purchase

    ID: 16183 Type: Default 1000ms 256MiB

Hay Bale Purchase

Hay Bale Purchase

Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his hay inventory, leaving him with nothing to feed his cows. To remedy his situation, he hitched up his wagon with capacity \(C\) (\(1 \leq C \leq 50000\)) cubic units and headed to Farmer Don's, who had \(H\) (\(1 \leq H \leq 5000\)) hay bales for sale. Each hay bale has its own volume \(V_i\) (\(1 \leq V_i \leq C\)). Being the practical man he is, Farmer John can only purchase whole bales. 

Your task is to determine the maximum volume of hay that Farmer John can purchase without exceeding the capacity of his wagon. In other words, given the wagon capacity \(C\) and a list of hay bale volumes, find the maximum sum of volumes that is less than or equal to \(C\) using a subset of the hay bales.

inputFormat

The first line of input contains two integers \(C\) and \(H\), representing the capacity of the wagon and the number of hay bales available, respectively. Each of the following \(H\) lines contains a single integer \(V_i\) indicating the volume of a hay bale.

\(\textbf{Input Format:}\)\newline \(C\) \(H\)\newline \(V_1\)\newline \(V_2\)\newline ...\newline \(V_H\)

outputFormat

Output a single integer representing the maximum volume of hay that Farmer John can purchase without exceeding his wagon's capacity.

\(\textbf{Output Format:}\)\newline A single integer representing the maximum achievable volume.

sample

10 3
3
5
3
8