In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of April 14th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #06 exercise sheet]
In this problem you should submit a complete program with the main function, reading with functions such as scanf and printing with functions such as printf.
Phoebe Buffay is on a mission to create the perfect playlist one that’s just the right length for her mood, without repeating songs. She has a list of available songs (including Smelly Cat, of course), and she wants to obey he following rules:
Imagine for instance that Phoebe has the following 9 songs available:
# | Performer | Music | Duration (seconds) |
---|---|---|---|
01 | Phoebe Buffay | Smelly Cat | 202 |
02 | Yann Tiersen | Porz Goret | 243 |
03 | God is an Astronaut | All Is Violent, All Is Bright | 254 |
04 | Explosions In The Sky | Your Hand In Mine | 502 |
05 | Opeth | Harvest | 385 |
06 | Green Carnation | 9-25-045 | 942 |
07 | Porcupine Tree | Lazarus | 237 |
08 | Metallica | Fade to Black | 721 |
09 | Tuna Javardemica | Ciencito Aluno | 192 |
Consider that Phoebe wants a play list with duration D = 1300 seconds. If she starts adding songs while the duration is not is not exceeded, she obtains the following playlist:
Phoebe then tried in increasing order of duration, starting with the smallest one, obtaining the following playlist, which is worse than the previous one:
Phoebe tried many other strategies (such as starting with the longest music) but quickly discovered none of these strategies would always give the best possible solution. In particular, for this case, the best playlist is the following (with only 2 seconds less than the desired duration):
Can you help Phoebe find the best playlist for a general case?
Given a duration D and a set of songs (specified by their duration), you have to calculate the duration of the optimal playlist, i.e. the subset of songs such that the sum of their durations is as large as possible and at the same time this sum is less than or equal to D.
The first line of input contains two integers D (the desired playlist duration) and N (the number of songs available).
N lines follow, each one with am integer Si indicating the duration of the i-th song. It is guaranteed that at least one song as a duration smaller or equal than D.
The output should be a line containing the total duration of the best possible playlist, that is, the choice of a subset of songs that maximizes ∑si such that ∑Si≤D
The following limits are guaranteed in all the test cases that will be given to your program:
1 ≤ D ≤ 10 000 | Desired duration | |
1 ≤ N ≤ 20 | Number of available songs | |
1 ≤ Si ≤ 10 000 | Duration of each song |
Example Input | Example Output |
1300 9 202 243 254 502 385 942 237 721 192 |
1298 |