#P3146. Bessie’s Merge Game

    ID: 16404 Type: Default 1000ms 256MiB

Bessie’s Merge Game

Bessie’s Merge Game

Bessie loves downloading games on her cell phone, even though she finds the small touch screen awkward to use with her large hooves. In her current game, she is given a sequence of \(N\) positive integers where \(2 \leq N \leq 248\) and each number is in the range \(1\ldots40\). In one move, Bessie can take two adjacent numbers that are equal and replace them with a single number equal to that number plus one. For example, two adjacent 7's can be merged into an 8.

The goal is to maximize the value of the largest number present in the sequence at the end of the game. Help Bessie achieve the highest score possible!

Note: All formulas are written in \(\LaTeX\) format.

inputFormat

The input consists of two lines. The first line contains an integer \(N\). The second line contains \(N\) space-separated integers representing the initial sequence.

\(2 \leq N \leq 248\) and each integer is between 1 and 40 inclusive.

outputFormat

Output a single integer: the maximum value that can appear in the sequence after performing zero or more merging operations optimally.

sample

4
1 1 1 2
3

</p>