NUMPATH
Let be a set consisting of (distinct in pairs) natural numbers, each of which has exactly digits. Therefore, for each element , it holds , and let's assume that numbers less than have exactly digits, with leading zeros.
Suppose we start with any of these numbers and apply the following process: we select a digit and replace it with any smaller decimal digit we want, as long as we obtain a number that belongs to the set . We repeat this process as many times as we want (and can), let's say .
Given , , and , find the maximum possible that can result from this process.
Input
The input will consist of two lines. The first line will contain two natural numbers and , separated by a single space. The second line will contain the integers , separated by a single space each. Assume that each of these consists of exactly decimal digits.
Output
The output must consist of a single line containing only one natural number: the maximum possible .
Constraints
Execution time limit: 1 sec
Memory limit: 64 MB
Attention: The numbers may be greater than .
Examples of Input - Output
1st
STDIN (numpath.in)
9 3
123 991 323 321 329 121 921 125 999
STDOUT (numpath.out)
4
2nd
STDIN (numpath.in)
4 2
17 23 09 42
STDOUT (numpath.out)
0
Explanation of the Examples: In the 1st example, we can start from the number 999 and move as follows: 999 991 921 321 121 making a total of four digit replacements (the replaced digits are underlined). There is no other larger sequence of replacements possible. In the 2nd example, no matter which number we start from, no replacements can be made.
Comments