2020-2021 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred) |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

The problem statement has recently been changed. View the changes.

×
H. Hard Optimization

time limit per test

3 secondsmemory limit per test

512 megabytesinput

standard inputoutput

standard outputYou are given a set of $$$n$$$ segments on a line $$$[L_i; R_i]$$$. All $$$2n$$$ segment endpoints are pairwise distinct integers.

The set is laminar — any two segments are either disjoint or one of them contains the other.

Choose a non-empty subsegment $$$[l_i, r_i]$$$ with integer endpoints in each segment ($$$L_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 ($$$\sum_{i=1}^n r_i - l_i$$$) is maximized.

Input

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

The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$0 \le L_i < R_i \le 10^9$$$) — the endpoints of the $$$i$$$-th segment.

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

Output

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

On the $$$i$$$-th of the next $$$n$$$ lines, output two integers $$$l_i$$$ and $$$r_i$$$ ($$$L_i \le l_i < r_i \le R_i$$$), denoting the chosen subsegment of the $$$i$$$-th segment.

Example

Input

4 1 10 2 3 5 9 6 7

Output

7 3 6 2 3 7 9 6 7

Note

The example input and the example output are illustrated below.

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/01/2021 20:05:22 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|