Open Olympiad in Design
Time limit1sMemory limit1024 MB
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 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 must have exactly 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 words of the given lengths that are distinct and sorted in lexicographical order.
Input
The first line contains a single integer (), the number of problems.
The second line contains integers (), 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 is lexicographically smaller than a string if either the first position where they differ has , 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.