#P3358. 最长k可重区间集问题

最长k可重区间集问题

Description

Given a set I\mathbf{I} of nn open intervals on the real line L\text{L} and a positive integer kk, design an algorithm to select a set of open intervals SI\mathbf{S} \subseteq \mathbf{I} such that at any point xx on L\text{L}, the number of intervals in S\mathbf{S} that contain xx is at most kk, and zSz\sum_{z \in \text{S}} \lvert z \rvert is maximized (z\lvert z \rvert denotes the length of the open interval zz).

Such a set S\mathbf{S} is called a longest kk-overlap interval set of I\mathbf{I}. The value zSz\sum_{z \in \text{S}} \lvert z \rvert is called the length of a longest kk-overlap interval set.

Given the set of open intervals I\mathbf{I} and the positive integer kk, compute the length of a longest kk-overlap interval set of I\mathbf{I}.

Input Format

The first line contains two positive integers nn and kk, denoting the number of open intervals and the allowed maximum number of overlaps. Each of the next nn lines contains two integers, the left and right endpoints l,rl, r of an open interval. It is guaranteed that l<rl < r.

Output Format

Output the length of a longest kk-overlap interval set.

4 2
1 7
6 8
7 10
9 13 
15

Hint

For 100%100\% of the testdata, 1n5001 \le n \le 500, 1k31 \le k \le 3, 1l<r1051 \le l < r \le 10^5.

Translated by ChatGPT 5