Heroes against villains
M heroes each start with health H; N villains stand in a line with healths V[1..N]. The heroes fight the villains in order, one hero at a time. If the hero's health is more than the villain's, the villain is defeated and the hero loses that much health. If it is less, the hero is out and the villain stays. If they are equal, both are out. Remove the fewest villains from the front so that every remaining villain is defeated.
Reported in: Infosys (SP / DSE · earlier papers) (memory-based)
Input
First line: N M H. Second line: N villain healths.
Output
The fewest villains to remove from the front.
Constraints
1 ≤ N, M ≤ 10^5; 1 ≤ H, V[i] ≤ 10^9.
Example 1
Input:
4 2 10
5 7 3 4
Output:
1Example 2
Input:
3 1 5
10 1 1
Output:
1Example 3
Input:
2 3 4
1 2
Output:
0Hint: Removing more villains from the front never hurts, so binary-search the number removed and simulate the fights for each guess. That is O(N log N) in all.