The 248 Game

Merge adjacent equal numbers into a number one larger to maximize the largest value left.

Medium6Dynamic programmingIntervalsNo attempts yetTime limit2sMemory limit512 MB

Problem

Bessie has big hands and finds a small touchscreen awkward, yet she still likes playing games on her phone.

One game has her attention right now. It starts with NN integers between 1 and 40 (2N2482 \le N \le 248). Bessie may replace two adjacent numbers of equal value with a single number that is 1 larger. For example, two adjacent 7s become one 8. The game ends once no adjacent pair of equal numbers is left. The goal is to make the largest number remaining in the sequence at that moment as large as possible. Find the highest score Bessie can reach.

Input

The first line contains NN.

Each of the next NN lines contains one of the starting numbers, in order. Every number is an integer between 1 and 40.

Output

Print the largest number Bessie can build.

Hint

Take the sequence 1 1 1 2. Merging the second and the third 1 gives 1 2 2, and merging the two 2s gives 1 3, so the answer is 3. Merging the first and the second 1 first does not lead to the best result.