#P12907. [NERC 2020] Hard Optimization

    ID: 12724 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2020各省省选2022福建Special JudgeICPCNERC/NEERC

[NERC 2020] Hard Optimization

Description

You are given a set of nn segments on a line [Li;Ri][L_i; R_i]. All 2n2n segment endpoints are pairwise distinct integers.

The set is laminar\emph{laminar} --- any two segments are either disjoint or one of them contains the other.

Choose a non-empty subsegment [li,ri][l_i, r_i] with integer endpoints in each segment (Lili<riRiL_i \le l_i < r_i \le R_i) in such a way that no two subsegments intersect (they are allowed to have common endpoints though) and the sum of their lengths (i=1nrili\sum_{i=1}^n r_i - l_i) is maximized.

Input Format

The first line contains a single integer nn (1n21031 \le n \le 2 \cdot 10^3) --- the number of segments.

The ii-th of the next nn lines contains two integers LiL_i and RiR_i (0Li<Ri1090 \le L_i < R_i \le 10^9) --- the endpoints of the ii-th segment.

All the given 2n2n segment endpoints are distinct. The set of segments is laminar.

Output Format

On the first line, output the maximum possible sum of subsegment lengths.

On the ii-th of the next nn lines, output two integers lil_i and rir_i (Lili<riRiL_i \le l_i < r_i \le R_i), denoting the chosen subsegment of the ii-th segment.

4
1 10
2 3
5 9
6 7
7
3 6
2 3
7 9
6 7

Hint

The example input and the example output are illustrated below.