Lifts

아직 제출이 없습니다시간 제한2.5초메모리 제한64 MB

문제

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 i 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.

입력

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

출력

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

제한

  • 1n10,0001 ≤ n ≤ 10\\,000
  • 1kmin(30,n)1 ≤ k ≤ \min{(30, n)}
  • 1l,r1091 ≤ l, r ≤ 10^9

힌트

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.