Under construction

Still being built. Things may change

Open Olympiad in Design

Time limit1sMemory limit1024 MB

Summary
Given n required lengths, find the fewest letters needed to write n distinct strings of those lengths in strictly increasing lexicographic order.
Level

Medium7 of 10

Topics
Binary search, Greedy, Stack, String
Solved
No attempts yet

Statement

Many people who prepare programming competitions used to be competitors themselves. This is good, because former competitors know the competition details and can write statements, run the judging system, and handle many other tasks. What would the competition look like if designers prepared it instead?

In an imaginary world, the Open Olympiad in Design takes place. There are nn problems. Each problem has its own statement designer, and there is one designer for problem names and one font designer. Each statement designer has already finished the work and left a fixed space for the problem name. The name of problem ii must have exactly lil_i characters.

Problem names must be made of Unicode characters, be distinct, and appear in lexicographical order. The font designer wants the names designer to satisfy all conditions while using as few distinct letters as possible. The order of the problems cannot be changed.

Find the minimum number of distinct letters needed to build nn words of the given lengths that are distinct and sorted in lexicographical order.

Input

The first line contains a single integer nn (1≤n≤100 0001 \leq n \leq 100\,000), the number of problems.

The second line contains nn integers l1,l2,…,lnl_1, l_2, \ldots, l_n (1≤li≤1091 \leq l_i \leq 10^9), the lengths of the problem names.

Output

Print one integer, the minimum number of distinct letters the font designer has to paint.

Notes

A string x1x2…xax_1 x_2 \ldots x_a is lexicographically smaller than a string y1y2…yby_1 y_2 \ldots y_b if either the first position ii where they differ has xi<yix_i < y_i, or the first string is a strict prefix of the second string. A sequence of distinct words is sorted if each word is smaller than the next one.

Examples2

  1. Example 1

    Input
    5
    2 2 2 2 2
    
    Expected output
    3
    
  2. Example 2

    Input
    4
    3 1 2 2
    
    Expected output
    2