MMAXPER - Rectangles Perimeter

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.

Input

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

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

SAMPLE INPUT:
5
2 5
3 8
1 10
7 14
2 5

SAMPLE OUTPUT:
68

Explanation

A configuration, that yields the maximum length of the upper envelopline, is presented on the picture. The upper envelop line consists of segments DC, CG, GF, FJ, JI, IM, ML, LP, and PO. The total length is 5 + 6 + 3 + 7 + 10 + 13 + 7 + 12 + 5 = 68

Problem for kid - Please, think like kid.


Được gửi lên bởi:psetter
Ngày:2009-02-17
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:BOI For Kid 08

hide comments
2021-05-27 18:01:58
Tham khảo: https://vnspoj.github.io/problems/MMAXPER
2021-04-26 15:19:54
voil

Last edit: 2021-04-26 15:20:48
2017-07-09 16:52:40
for kid :)) chuẩn luôn mình nghĩ phức tạp vl :)
2016-02-17 14:31:13
@@@ !!! @@@ :v :v :v
t in ra thử 2 mảng QHD xem coi Đ 0 mà quên xóa, nộp sai 2 lần @@@ !!! @@@
bài này QHD dễ :v 2 mãng O(n)
2015-08-13 05:53:42 Sơn Tùng M-TP
haizzz. quy hoạch động trạng thái. hehehe. :v
2015-02-06 14:34:13 Con Bò Huyền Thoại
http://dangminhtien.name.vn/blog/2015/02/06/mmaxper-spoj-rectangles-perimeter/
2014-09-04 18:27:49 Messi_CBH
Hãy suy nghĩ như một đứa trẻ...:)
2014-07-11 04:32:35 Lê Nguyên Khôi
1 đấm 0,00 giây
2014-07-03 13:40:45 Lương Ðức Tuấn Ðạt
For Kid? Yup :))
2014-06-21 18:27:26 ­
đậu má khởi tạo nhầm a[1] thành a[i] ngồi từ 7h đến 11h, bực mình thật
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.