#P6740. Minimum Starting Number

    ID: 19948 Type: Default 1000ms 256MiB

Minimum Starting Number

Minimum Starting Number

Given a positive integer \(K\) and a string of \(K\) digits, we have a sequence of \(K\) consecutive positive integers \(N, N+1, \dots, N+K-1\). For each integer in the sequence, only one digit is visible. The \(i\)-th digit in the input string must appear in the decimal representation of the integer \(N+i-1\) (using 1-indexing in the description). Find the smallest possible starting number \(N\) that satisfies these conditions.

Note: The visible digit can be located anywhere within the number.

Example: For \(K=3\) and the digit sequence 123, we require that:

  • The number \(N\) contains the digit '1'
  • The number \(N+1\) contains the digit '2'
  • The number \(N+2\) contains the digit '3'

One valid solution is \(N=1\) since 1 contains '1', 2 contains '2', and 3 contains '3'.

inputFormat

The input consists of two lines:

  1. The first line contains a positive integer \(K\) representing the number of consecutive integers.
  2. The second line contains a string of length \(K\). Each character of the string is a digit (0-9) which is the visible digit for the corresponding integer in the sequence.

outputFormat

Output the smallest possible starting number \(N\) such that for every \(i\) (where \(0 \le i < K\)), the decimal representation of \(N+i\) contains the \((i+1)\)th digit from the input string.

sample

3
123
1