#P6740. [BalticOI 2014] Sequence (Day1)

[BalticOI 2014] Sequence (Day1)

Description

There are now KK consecutive positive integers. For each integer, you can only see one of its digits. Find the minimum possible value of the first number NN.

Input Format

The first line contains an integer KK, representing the length of the sequence.
The second line contains KK integers, representing the digits you can see.

Output Format

Output one integer in a single line, representing the minimum possible value of NN.

6
7 8 9 5 1 2
47

Hint

Sample Explanation

For sample 11, the sequence that makes NN minimal is 47,48,49,50,51,5247,48,49,50,51,52, and the minimum value of NN is 4747.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (9 pts): N,K1000N,K \le 1000.
  • Subtask 2 (33 pts): K1000K \le 1000.
  • Subtask 3 (25 pts): all given digits are equal.
  • Subtask 4 (33 pts): no special constraints.

For 100%100\% of the data, 1K1051 \le K \le 10^5.

This problem enforces O2 optimization.

Notes

Translated from BalticOI 2014 Day1 C Sequence

Translated by ChatGPT 5