#P9080. [PA 2018] Nowy kontrakt

[PA 2018] Nowy kontrakt

Problem Description

This problem is translated from PA 2018 Round 2 Nowy kontrakt.

You are given a sequence aa of NN positive integers. You need to make the sequence strictly increasing by appending digits to the end of numbers in the sequence. Your goal is to minimize the number of appended digits.

Appending a digit tt to a number aa means:

aa×10+ta \gets a \times 10 + t, where t{0,1,2,3,4,5,6,7,8,9}t \in \{0,1,2,3,4,5,6,7,8,9\}.

Input Format

The first line contains an integer NN, the length of the sequence.

Lines 22 to N+1N + 1 each contain one number. Line ii contains the (i1)(i - 1)-th number in the sequence.

Output Format

Print one integer: the minimum number of operations.

3
8
5
13
2

Hint

Sample 1 Explanation

Append one digit to the 2nd number and one digit to the 3rd number, i.e., a2a2×10+7a_2 \gets a_2 \times 10 + 7, a3a3×10+3a_3 \gets a_3 \times 10 + 3. The new sequence becomes (8,57,133)(8,57,133), which is strictly increasing. The number of operations is 22. There are other ways with 22 operations, but there is no way with only 11 operation.


Constraints

This problem uses bundled tests.

For 100%100\% of the testdata:

  • 1N2000001 \le N \le 200000.
  • 1ai1091 \le a_i \le 10^9.

Translated by ChatGPT 5