> This problem can be solved with help of two pointers. We will store two sets — set with songs with full time and set with songs with partly time. How to move left and right pointers and recalculate current answer? Let left end of the current segment is*l*and right end of the segment is*r*. Let the set of songs with partly time is*half*and with full time —*full*. In this sets we will store pairs — time of listening each song and it number.
>
> Right end we will move in the following way: if we can add partly song and we have enough time to listen it — we take it (also add this song to*half*) and add to time$(t_r+1)/2$and add to the answer$a_r$. In the other case we have two cases. First — we add current song as full song, second — we add current song as partly song. Here we need to choose case with less with the total time. Also here we need to correctly add song to sets and update total time and the answer. If total time became more than*k*we are not allowed to move*r*. Now we need to update global answer with the current value of answer.
>
> Left end we will move in the following way: if we took song as full song we delete it from*full*, subtract from total time length of this song. In the other case we delete it from*half*and subtract from the total time the$(t_r+1)/2$. After that we try to take some song from*full*. If the size of*full*is 0, we can not do that. If we done it we need to change total time on the songs on the current segment.