luogu#P16424. [IATI 2022] Lifts

[IATI 2022] Lifts

Problem Description

You have been hired to build a lift system for a hotel!

The hotel has kk lifts, and initially you can choose which floors they are on. During the day there are nn requests, each characterized by a pair (l,r)(l, r). This means someone is on the ll-th floor and wants to go to the rr-th floor.

People don’t like waiting around, so we have the requirement to complete the requests sequentially. More formally, request number ii must be completed before request number i+1i + 1. The lifts are small and must be empty or carrying exactly one passenger at any given time. Also, every request can be completed by any of the lifts.

We want to build a smart system, so we want to minimize the total number of floors when lifts are travelling empty. More precisely, if a lift travels from floor pp to floor qq without a passenger, then we consider that it has travelled pq|p - q| floors empty. We should note that the lifts can wait at the same floor for any amount of time, without incurring any additional cost.

Unfortunately, the hotel is old and the machines only have 64 MB of memory! However, we know you’re a great programmer, so this should be no issue for you.

Write a program lifts that computes the optimal schedule that minimizes the total number of floors that the lifts are travelling empty.

Input Format

The first line of the standard input contains the numbers nn and kk. The next nn lines contain 2 numbers each - (l,r)(l, r) for the respective request.

Output Format

On the single line of the standard output, print the minimum sum of floors where the lifts travel empty.

3 2
5 20
8 100
2 80
12

Hint

Explanation of the examples

Lift 11 is initially on floor 55 and lift 22 is on floor 88.

Lift 11 travels 00 floors empty and carries the passenger to the 2020th floor.

Lift 11 travels 1212 floors empty and carries the passenger to the 100100th floor.

Lift 22 travels 00 floors empty and carries the passenger to the 8080th floor.

The total number of floors where the lifts travel empty is 0+12+0=120 + 12 + 0 = 12.

Constraints

  • 1n100001 \le n \le 10\,000
  • 1kmin(30,n)1 \le k \le \min(30, n)
  • 1l,r1091 \le l, r \le 10^9

Subtasks

No nn Points
11 22\leq 22 55
22 250\leq 250 2020
33 600\leq 600 1010
44 1250\leq 1250 1515
55 2500\leq 2500 2020
66 - 3030