TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.


Figure 1.


If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.

Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.


Now let’s look at the picture below:



Figure 2.


The first one is OPTIMAL.


Find the highest score you can get, given an initial state of this game.

Input Format

There are multiple test cases in the input file. Each case contains two lines.
The first line contains an integer n(1<=n<=200), the number of boxes.
The second line contains n integers, representing the colors of each box.
The integers are in the range 1~n.


n=0 terminates the input.

Output Format

For each test case, print the highest possible score.

Sample Input 1

9
1 2 2 2 2 3 3 3 1
1
1
0

Sample Output 1

29
1

Hints

全消!

Try to analyse the time/space complexity to your algorithm carefully.

Problem Source

Migrated from old NTUJ.

UVa 10559

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 20000 65536 200