Candy
InterviewTime limit3sMemory limit1024 MB
Find the fewest adjacent swaps needed to make the first F boxes hold at least T candy in total, or report NO if that is impossible.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
In the ancient city of Ica, there is said to be a palace with wealth beyond imagination. Inside, there is a corridor with boxes of candy from all over the world. Travellers passing by can take as much candy as they want, provided that they pay its weight in gold.
The boxes are numbered to from left to right. Box holds units of candy, where is a non-negative integer.
As the guardian of the palace, you want to move the boxes so that boxes with a lot of candy end up closer to the entrance.
You are given the array , along with the numbers and . In one operation, you may swap two adjacent elements of the array. What is the minimum number of operations needed so that the first elements sum to at least ?
Input
The first line contains three integers , , and .
The second line contains integers .
Output
If it is impossible to reach the goal using the operations, print NO. Otherwise, print a single integer, the minimum number of operations.
Constraints
- .
- .
- .
- for .
The input numbers may not fit in a 32-bit integer, so watch for overflow.
Hint
In the first sample, the first two elements should sum to at least . A single swap of the and the achieves this: the array becomes 10 20 4 6 3 3, and the first two elements sum to .
In the second sample, the must move all the way to the end of the array, which takes three swaps.
In the third sample, the first two elements cannot reach . The best possible is .