#P7666. [JOI 2018 Final] 寒冬暖炉 / Stove
[JOI 2018 Final] 寒冬暖炉 / Stove
Description
There is a stove in JOI's room. Since JOI is used to cold temperatures, when he is alone in his room, he does not need to turn on the stove. However, when there are guests, he needs to turn on the stove.
One day, guests will visit JOI. The -th guest () will arrive at time and leave at time . At any time, at most one guest visits JOI. JOI can turn the stove on or off at any time.
JOI uses matches to light the stove. JOI has only matches, so he can turn on the stove at most times. At the beginning of the day, the stove is off. When the stove is on, it consumes fuel. Therefore, JOI controls when to turn the stove on or off, and he wants to minimize the total running time of the stove.
Given the guest visit information and the number of matches JOI has, write a program to compute the minimum possible total running time of the stove.
Input Format
The first line contains two space-separated integers and . This means that guests will visit JOI, and JOI has matches.
Each of the next lines contains an integer on the -th line. This means that the -th guest will arrive at time and leave at time .
Output Format
The only line should contain one integer: the minimum total running time of the stove.
3 2
1
3
6
4
3 1
1
2
6
6
3 3
1
3
6
3
10 5
1
2
5
6
8
11
13
15
16
20
12
Hint
Constraints
For of the testdata, , , (), ().
- Subtask ( points): .
- Subtask ( points): .
- Subtask ( points): No additional constraints.
Sample Explanation
For Sample : Three guests will visit JOI. If he turns the stove on and off in the following way, the stove is on when guests visit, he turns on the stove twice, and the total running time is .
- When the first guest arrives, he turns on the stove at time .
- When the second guest leaves, he turns off the stove at time .
- When the third guest arrives, he turns on the stove at time .
- When the third guest leaves, he turns off the stove at time .
Since the total running time cannot be smaller than , output .
For Sample : JOI can turn on the stove only once. Therefore, he turns on the stove at time when the first guest arrives, and he turns it off at time when the third guest leaves.
Note that a guest's leaving time may be the same as the next guest's arrival time.
For Sample : JOI turns on the stove when each guest arrives, and turns it off when each guest leaves.
Problem Note
This problem comes from T1: Stove of The 17th Japanese Olympiad in Informatics (JOI 2017/2018) Final Round: T1: Stove.
Translated and整理 (zhengli) by @求学的企鹅.
Translated by ChatGPT 5
京公网安备 11011102002149号