The largest rectangle that uses all of pile P will extend through all piles which have heights >= the height of P.
Using this, you can form 'N' rectangles (where N is the number of piles), each rectangle has a height of height(P) and extends as far right as possible.
You then take the biggest rectangle of that subset.
Example:
1 2 3 4 5 6 7
|
2 4 4 6 1
*
*
* * *
* * *
* * * *
* * * * *
|
P=0: height=2, extends to P=3 (inclusive). Size=8
- current best = 8
P=1: height=4, extends to P=3. Size = 12
- current best = 12
P=2: height=4, extends to P=3. Size = 8
- current best = 12
P=3: height=6, extends to P=3. Size = 6
- best = 12
P=4: height=1, extends to P=4. Size = 1
- best = 12
Solution is 12.
EDIT: actually this wouldn't quite work as shown by extending right only. You'd have to extend left AND right.
Also, as an optimization, you can skip piles where height(P) == height(P-1)