#P7514. [省选联考 2021 A/B 卷] 卡牌游戏

[省选联考 2021 A/B 卷] 卡牌游戏

Description

Alice has nn cards. On the front of the ii-th card (1in1 \le i \le n) there is a number aia_i, and on the back there is a number bib_i. Initially, all cards are face up.

Now Alice may flip at most mm cards, changing them from face up to face down. Alice’s goal is to make the range (the difference between the maximum and minimum) of the nn numbers showing in the end as small as possible. Please help Alice compute the minimum possible range.

Input Format

The first line contains two positive integers n,mn, m, representing the number of cards and the maximum number of cards that may be flipped.
The second line contains nn positive integers; the ii-th number is aia_i.
The third line contains nn positive integers; the ii-th number is bib_i.

The testdata guarantees that all 2n2 n numbers on the cards are pairwise distinct, and the cards are given in increasing order of aia_i.

Output Format

Only one line, an integer, indicating the answer.

6 3
8 11 13 14 16 19
10 18 2 3 6 7

8

见附件中的 card/card2.in。
见附件中的 card/card2.ans。
见附件中的 card/card3.in。
见附件中的 card/card3.ans。

Hint

[Sample #1 Explanation]

One optimal plan: flip cards 1,5,61, 5, 6. The numbers showing in the end are, in order, 10,11,13,14,6,710, 11, 13, 14, 6, 7, and the range is 146=814 - 6 = 8.


[Constraints]

For all testdata: 3n1063 \le n \le {10}^6, 1m<n1 \le m < n, 1ai,bi1091 \le a_i, b_i \le {10}^9.

The specific limits for each test point are shown in the table below:

Test Point ID nn \le Special Constraint
121 \sim 2 1010 None.
343 \sim 4 500500
565 \sim 6 5×1055 \times {10}^5 m1000m \le 1000.
77 105{10}^5 None.
88 4×1054 \times {10}^5
99 7×1057 \times {10}^5
1010 106{10}^6

Translated by ChatGPT 5