#P2314. [HNOI2005] 木梳
[HNOI2005] 木梳
Description
Ai Yi has loved art since childhood and dreams of becoming a great artist. Recently, he got a nice wooden board whose bottom side is a straight segment of length , evenly divided into segments, numbered from left to right. The top side is a sawtooth shape with integer heights: the height of the -th segment is , where (see the figure below).
*
* * *
* * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * *
4 4 6 5 4 2 3 3 5
It would be a pity to waste such good material, so Ai Yi decides to craft it into a piece of art. But he is not a pure artist; he believes every artwork should be practical (otherwise it is merely flashy). Practicality is his design philosophy.
Inspired by the sawtooth shape of the board, Ai Yi thought of an everyday item used after getting up: “Yes, let’s make it into a comb!” His idea is to chisel away some cells at the top ends of columns (if a cell is removed, then all cells above it must also be removed, but you cannot remove all cells in any column), so that the remaining board forms a “regular sawtooth shape” (which is suitable for combing).
For example, for the figure above, if you remove the top cell from columns , , and , and remove the top cells from column , the remaining region forms a “regular sawtooth shape” (as shown below).
.
+---------+ +--->
| * * | | *
------------+ | |
* * * * | . | *
| |
* * * * | . . . | *
+-------------------------+
* * * * * * * * *
* * * * * * * * *
4 4 5 5 2 2 2 2 5
A sawtooth shape is called “regular” if and only if the turning sequence of its boundary (the red curve in the right figure) does not contain 010 or 101. In the right figure, the curve’s turning sequence is 011001, where 0 denotes a left turn and 1 denotes a right turn. Walking along the curve from the leftmost end to the right, you first turn left, then right, then right, then left, then left, and finally right.
To minimize waste, Ai Yi wants the comb’s area to be as large as possible.
Input Format
The first line contains an integer , the length of the bottom straight segment of the board.
The second line contains positive integers , the heights of each segment.
Output Format
Output a single integer: the number of cells that must be removed from the board so that the comb’s area is maximized.
9
4 4 6 5 4 2 3 3 5
3
Hint
For of the testdata, .
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号