Arranging Heaps

Divide N ordered mining points into K groups, each group merging into one heap at its last point, minimizing total weighted distance moved.

Medium7Dynamic programmingDivide and conquerPrefix sumGreedyInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

A mining company extracts terbium, a rare metal used to build lightweight magnets, from river sand. It mines the Long River at NN 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 NN heaps into a smaller number of KK 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 XX can be taken to a mining point YY only if Y>XY > X. Each heap is moved whole to another mining point, or it is left where it is. Moving a heap of weight WW from point XX to point YY costs W×(YX)W \times (Y - X). 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 NN, KK, the NN mining points, and the weight of the heap produced at each point, write a program that computes the minimum total cost of regrouping the NN initial heaps into KK heaps.

Input

The first line contains two integers NN and KK, the number of initial heaps and the number of heaps left after the regrouping (1K<N10001 \le K < N \le 1000).

Each of the next NN lines describes one initial heap with two integers XX and WW, meaning that mining point XX produced a heap of weight WW (1X,W1061 \le X, W \le 10^6). 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 NN initial heaps into KK heaps.