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.


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.


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

2 5
3 8
1 10
7 14
2 5


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.

hide comments
aryan29: 2019-10-14 21:50:37

Easy peasy

a_coder_hack: 2019-08-01 20:39:41

AC in one go(Bottom-up approach)
Good DP question.

atiaditya3: 2019-07-24 00:23:59

Century on SPOJ :) !

dendrite5460: 2019-01-18 13:03:44

bottom up dp works :)

ankitpriyarup: 2018-12-22 17:04:56

AC in one go :) Simple

ankit1cool: 2018-06-21 12:58:04

I solved it using top-down but i don't understand what is the meaning of "Problem for kid - Please, think like kid." does it mean that it has a non dp solution

akash13s: 2018-05-23 18:43:30

Nice problem:)

vivek_rathi: 2018-04-01 10:14:02

"Problem for kid - Please, think like kid." Mind these words

sahil070197: 2018-03-10 10:35:47

@mohit_aggarwal I support you! pagal bna rha hai @infernodragon

mohit_aggarwal: 2018-02-28 12:44:41

Values are well within the range !!
Don't mislead Others !!
BTW AC in One go ;-)

Added by:~!(*(@*!@^&
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:BOI For Kid 08