# Long bus trip
## Subtask 1
Try all $2^M$ subsets of passengers that we want to keep in the bus. Once this is fixed, the problem becomes greedy.
## Subtask 2/3
First, we need to realize that the problem is about whether to kick off each passenger or not.
Consider two adjacent water stations. The only way we can kick off passengers is by removing those closest to the second one, in order. (By getting too little water, a suffix of people will get off).
This still isn't enough for a polynomial solution. The key is the chauffeur- we must always give him water, otherwise we lose. This means that we are even more restricted in removing passengers. For every water station, we may remove passengers between it and the last time the chauffeur needed to drink.
This means that we can basically view the whole problem mod $T$, because at every time that is $0$ mod $T$, the bus driver must drink.
Another important insight is that if we want to kick off someone, we will kick them off as early as possible. What we can do is compute for each person $min[i]=$ the earliest water station that is directly after person i (in the sense that there is no other person between i and the water station mod $T$) ($min[i]$ will hold non-modded earliest, since we want to use it to compute the cost after we kick someone off). Another small detail is that we have to pretend that there is a water station at time $X$ (since we can kick off people before the ending).
After having computed $min[i]$, we can formulate our DP. We will process people in increasing order of $D_j$. We will define $dp[i]=$min cost if we consider people $[0,i]$. We will compute each state by looking at earlier states (where we could've come from). Suppose we want to compute $dp[i]$. There are two transitions:
* Do not kick off person i. Pay the price for them to stay for the whole ride, plus $dp[i-1]$.
* If $min[i]$ is defined (there is a water station directly after i): for some $j<i$, kick off all people in $[i,j]$. Then, all of these jumped off at time $min[i]$. Don't forget to pay for the price of them jumping off, and also pay $dp[j-1]$.
## Subtask 4
The only slow part in the described solution is the transition of type 2. We can realize that the cost is a linear function and use convex hull trick.