SP3878 MMAXPER - Rectangles Perimeter

Description

[English](/problems/MMAXPER/en/) [Vietnamese](/problems/MMAXPER/vn/) Given are n rectangles, numbered from 1 to n. We place them tightly on the axis OX, from left to right, according to rectangles' numbers. Each rectangle stays on the axis OX either by its shorter or by its longer side (see the picture below). Compute the length of the upper envelop line, i.e. perimeter's length of the obtained figure minus the length of the left, right and bottom straight line segments of the picture. Write program to find the maximum possible length of the upper envelop line. [![Image and video hosting by TinyPic](https://cdn.luogu.com.cn/upload/vjudge_pic/SP3878/e2b5da2de1143437443e702e2bc68101b323da4a.png)](http://tinypic.com)

Input Format

On the first line of the standard input, the value of n is written. On each of the next n lines, two integers are given – a\_i and b\_i – the side lengths of the i\_th rectangle. Constraints: 0 < n < 1000; 0 < a\_i < b\_i < 1000, for each i = 1, 2, …, n.

Output Format

On a line of the standard output, your program should write the result as a positive integer.