#C649. Maximum Songs Playlist
Maximum Songs Playlist
Maximum Songs Playlist
You are given an integer ( n ) representing the number of songs, an integer ( D ) representing the maximum allowed total duration (in minutes), and a list of ( n ) integers representing the individual durations of the songs. Your task is to determine the maximum number of songs that can be added to a playlist without the total duration exceeding ( D ).
Details:
For each test case, you will be provided with the value of ( n ), the upper limit ( D ), and a list of song durations. You should choose songs in such a way that the sum of their durations remains ( \leq D ). A greedy strategy by selecting songs in ascending order of their durations is optimal.
Mathematical Formulation:
Let ( durations = [d_1, d_2, \dots, d_n] ) be the sorted list of song durations in non-decreasing order. Find the largest integer ( k ) such that ( \sum_{i=1}^{k} d_i \leq D ).
inputFormat
The first line contains two integers ( n ) and ( D ) separated by a space. The second line contains ( n ) space-separated integers representing the durations of the songs.
outputFormat
Output a single integer indicating the maximum number of songs that can be included in the playlist without exceeding the total duration ( D ).## sample
5 60
10 20 30 40 50
3
</p>