Arranging Heaps
Time limit2sMemory limit128 MB
Given N heaps at increasing positions with weights, merge them into exactly K heaps where each heap moves only downriver, minimizing total weight times distance moved.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Divide and conquer, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
A mining company extracts a rare metal from river sand at mining points along the Long River. Each mining point is identified by its distance from the river's source, and produces one heap of mineral ore.
To collect the ore, the company regroups the heaps into a smaller number of heaps, each located at one of the original mining points. A very large barge does the moving: it starts at the source and can only travel downriver, so a heap produced at mining point may be carried to mining point only if . Each heap is either moved in full to another mining point or left in place. Moving a heap of weight from mining point to mining point costs ; a heap left in place adds nothing to the cost. The total regrouping cost is the sum of the costs of all heap movements.
Given , , the positions of the mining points, and the weight produced at each, compute the minimum total cost to regroup the heaps into exactly heaps.
Input
The input contains several test cases; process test cases until the end of input.
Each test case begins with a line containing two integers and (): the number of initial heaps and the desired number of heaps after regrouping. Each of the next lines contains two integers and (), meaning that a heap of weight was produced at mining point . Within a test case the heaps are listed in strictly increasing order of their positions .
Output
For each test case, output a single line containing one integer: the minimum total cost to regroup the heaps into heaps.