Steel Slicing
Time limit2.5sMemory limit512 MB
Given each slab's heights above and below the x-axis, find the largest-area axis-aligned rectangle that fits inside the histogon.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Array
- Solved
- No attempts yet
Problem
ISCO (ICPC Steel Company) buys steel sheets of a certain shape, cuts them into pieces, and sells the pieces to the industry market. Every steel sheet ISCO buys is a polygon without holes, and each side is either horizontal or vertical with respect to the -axis. The length of each side is a positive integer. We call such a polygon a histogon. Figure 1(a) shows a histogon.

Figure 1. (a) A steel sheet that is a histogon. (b) An axis-aligned rectangle (red) with maximum area contained in the histogon. (c) The histogon is subdivided along vertical lines into 8 rectangular slabs with width 1.
A piece sells for a much higher price when it is a rectangle with a larger area, so ISCO wants to cut each sheet to get a rectangle of maximum area. The task is to find the axis-aligned rectangle with the largest area that is contained in the steel sheet. The rectangle must have every side horizontal or vertical with respect to the -axis. Figure 1(b) shows the maximum-area rectangle for the histogon in Figure 1(a).
A histogon with width can be subdivided along vertical lines into rectangular slabs with width 1. Figure 1(c) shows the histogon of Figure 1(a) split into 8 slabs. The slabs are numbered from 1 to from left to right, so the leftmost slab is number 1. Assume the -axis intersects every slab. Slab is described by two values, and , where is the vertical length of slab above the -axis and is the vertical length of slab below the -axis.
Given a histogon with width , output the maximum area among the axis-aligned rectangles contained in the histogon.
Input
The first line contains one integer (), the width of the histogon. The slabs are numbered from 1 to such that the leftmost slab is number 1. Each of the following lines contains two nonnegative integers and () for slab .
Output
Print exactly one line containing the maximum area among the axis-aligned rectangles contained in the given histogon.