Arranging Heaps
InterviewTime limit2sMemory limit512 MB
Divide N ordered mining points into K groups, each group merging into one heap at its last point, minimizing total weighted distance moved.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Divide and conquer, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
A mining company extracts terbium, a rare metal used to build lightweight magnets, from river sand. It mines the Long River at mining points, and each point is identified by its distance from the river source. Every mining point yields one heap of ore that is small in volume and high in value.
To collect the ore, the company regroups the heaps into a smaller number of heaps. Every new heap sits at one of the original mining points, and trucks pick the new heaps up from there.
The regrouping uses a barge. The barge is very large, so it can carry any amount of ore. It starts at the river source and travels downriver only, so the heap produced at mining point can be taken to a mining point only if . Each heap is moved whole to another mining point, or it is left where it is. Moving a heap of weight from point to point costs . The total cost of the regrouping is the sum of the costs of the individual moves, and a heap that is not moved adds nothing to it.
Given , , the mining points, and the weight of the heap produced at each point, write a program that computes the minimum total cost of regrouping the initial heaps into heaps.
Input
The first line contains two integers and , the number of initial heaps and the number of heaps left after the regrouping ().
Each of the next lines describes one initial heap with two integers and , meaning that mining point produced a heap of weight (). The heaps are given in strictly increasing order of their mining points.
Output
Print one integer on a single line, the minimum total cost of regrouping the initial heaps into heaps.