#P1103. 书本整理

书本整理

Description

Frank is a neat person. He has a large pile of books and a bookshelf, and he wants to put the books on the shelf. The shelf can hold all the books, so Frank first arranges the books on the shelf in order of height. However, Frank finds that since many books have different widths, the shelf still looks messy. He decides to remove kk books so that the shelf looks tidier.

The messiness of the shelf is defined as the sum of the absolute differences of the widths between each pair of adjacent books (after ordering by height). For example, there are 44 books:

1×21 \times 2
5×35 \times 3
2×42 \times 4
3×13 \times 1

After arranging them by height, Frank gets:

1×21 \times 2
2×42 \times 4
3×13 \times 1
5×35 \times 3

The messiness is 2+3+2=72+3+2=7.

Each book has a distinct height. Please find the minimum possible messiness after removing kk books.

Input Format

The first line contains two integers nn and kk, the number of books and the number to remove (1n1001 \le n \le 100, 1k<n1 \le k < n).

Each of the next nn lines contains two integers, the height and width of a book, both no more than 200200.

Heights are guaranteed to be distinct.

Output Format

Output a single integer: the minimum messiness of the shelf.

4 1
1 2
2 4
3 1
5 3

3

Hint

Translated by ChatGPT 5