QOJ.ac

QOJ

时间限制: 3 s 内存限制: 256 MB 总分: 100 可 Hack ✓

#3296. Interval

统计

There are $n$ closed intervals $[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]$ on the number line. You need to select $m$ intervals from these such that these $m$ intervals share at least one common position. In other words, there must exist an $x$ such that for every selected interval $[l_i, r_i]$, we have $l_i \le x \le r_i$.

For a valid selection, the cost is defined as the length of the longest selected interval minus the length of the shortest selected interval. The length of an interval $[l_i, r_i]$ is defined as $r_i - l_i$, which is the value of its right endpoint minus the value of its left endpoint.

Find the minimum cost among all valid selection schemes. If no valid scheme exists, output $-1$.

Input

The first line contains two positive integers $n$ and $m$, separated by a space, as described above. It is guaranteed that $1 \le m \le n$.

The next $n$ lines each represent an interval, containing two integers $l_i$ and $r_i$ separated by a space, representing the left and right endpoints of the interval.

Output

Output a single line containing a single integer, the minimum cost.

Examples

Input 1

6 3
3 5
1 2
3 4
2 2
1 5
1 4

Output 1

2

Note

As shown in the figure, when $n = 6$ and $m = 3$, the scheme with the minimum cost is to select the three intervals $[3, 5]$, $[3, 4]$, and $[1, 4]$. They all contain the position $4$, so it is valid. The longest interval is $[1, 4]$ and the shortest is $[3, 4]$, so the cost is $(4 - 1) - (4 - 3) = 2$.

Input 2

See interval/interval2.in in the contestant directory.

Output 2

See interval/interval2.ans in the contestant directory.

Input 3

See interval/interval3.in in the contestant directory.

Output 3

See interval/interval3.ans in the contestant directory.

Subtasks

The ranges and characteristics of all test data are shown in the table below:

Test Case ID $n$ $m$ $l_i, r_i$
1 20 9 $0 \le l_i \le r_i \le 100$
2 20 10 $0 \le l_i \le r_i \le 100$
3 199 3 $0 \le l_i \le r_i \le 100000$
4 200 3 $0 \le l_i \le r_i \le 100000$
5 1000 2 $0 \le l_i \le r_i \le 100000$
6 2000 2 $0 \le l_i \le r_i \le 100000$
7 199 60 $0 \le l_i \le r_i \le 5000$
8 200 50 $0 \le l_i \le r_i \le 5000$
9 200 50 $0 \le l_i \le r_i \le 10^9$
10 1999 500 $0 \le l_i \le r_i \le 5000$
11 2000 400 $0 \le l_i \le r_i \le 5000$
12 2000 500 $0 \le l_i \le r_i \le 10^9$
13 30000 2000 $0 \le l_i \le r_i \le 100000$
14 40000 1000 $0 \le l_i \le r_i \le 100000$
15 50000 15000 $0 \le l_i \le r_i \le 100000$
16 100000 20000 $0 \le l_i \le r_i \le 100000$
17 200000 20000 $0 \le l_i \le r_i \le 10^9$
18 300000 50000 $0 \le l_i \le r_i \le 10^9$
19 400000 90000 $0 \le l_i \le r_i \le 10^9$
20 500000 200000 $0 \le l_i \le r_i \le 10^9$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.