#P5504. [JSOI2011] 柠檬
[JSOI2011] 柠檬
Description
likes lemons very much. It prepared a string of shells threaded onto a branch and plans to use a kind of magic to turn the shells into lemons. There are shells in total, threaded on the branch in order. For convenience, we number the shells from left to right as . The sizes of the shells are not necessarily the same; the size of shell is .
The lemon-making magic requires that: each time, removes a short consecutive segment of shells from one end of the branch, and chooses a shell size . If there are shells of size in this segment, then the magic can turn this segment into lemons. may remove shells any number of times until all shells on the branch have been removed. For different segments, the chosen shell size may be different. The final number of lemons gets is the sum of the lemons obtained from all segments.
wants to know the maximum number of lemons that can be produced from this string of shells. Please help solve this problem.
Input Format
Line : an integer .
Lines : each line contains one integer; line gives .
Output Format
A single integer, meaning the maximum number of lemons can obtain.
5
2
2
5
2
3
21
Hint
first removes shells from the left end, with sizes . Choose . Then there are shells of size in this segment, so the magic yields lemons. Then remove the last shell from the right end, and the magic yields lemons. In total, you can get lemons. There is no better plan than this.
Translated by ChatGPT 5
京公网安备 11011102002149号