Farmer John is making the difficult transition from raising mountain goats to raising cows. His farm, while ideal for mountain goats, is far too mountainous for cattle and thus needs to be flattened out a bit. Since flattening is an expensive operation, he wants to remove the smallest amount of earth possible.
The farm is long and narrow and is described in a sort of two-dimensional profile by a single array of N (1 <= N <= 1000) integer elevations (range 1..1,000,000) like this:
1 2 3 3 3 2 1 3 2 2 1 2,
* * * *
* * * * * * * * *
* * * * * * * * * * * *
1 2 3 3 3 2 1 3 2 2 1 2
* * * -
* * * * * - - - -
* * * * * * * * * * * *
1 2 3 3 3 2 1 1 1 1 1 1
* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Each line contains a single integer elevation. Line i+1 contains the elevation for index i.
* Line 1: The minimum volume of earth that must be removed to reduce the number of peaks to K.
12 1 1 2 3 3 3 2 1 3 2 2 1 2
5