Drazil is playing a math game with Varda. Let's define for positive integer x as a product of factorials of its digits. For example, . First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying following two conditions: 1. x doesn't contain neither digit 0 nor digit 1. 2. = f(x) = f(a) Help friends find such number. Input The first line contains an integer n (1 ≤ n ≤ 15) — the number of digits in a. The second line contains n digits of a. There is at least one digit in a that is larger than 1. Number a may possibly contain leading zeroes. Output Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation. Examples input 4 1234 output 33222 input 3 555 output 555 |
Proof: We can observe that our answer won't contain digits 4,6,8,9, because we can always transform digits 4,6,8,9 to more digits as in the conclusion, and it makes the number larger. Then, how can we make sure that the result is the largest after this transformation? We can prove the following lemma: For any positive integer x, if it can be written as the form (2!)c2 * (3!)c3 * (5!)c5 * (7!)c7, there will be only one unique way. Suppose that there exists two ways to write down x in this form, we can assume that the two ways are (2!)a2 * (3!)a3 * (5!)a5 * (7!)a7 and (2!)b2 * (3!)b3 * (5!)b5 * (7!)b7. We find the largest i such that ai ≠ bi, Then we know there exists at least one prime number whose factor is different in the two ways. But according to the Fundamental Theorem of Arithmetic, there is only one prime factorization of each integer. So we get a contradiction. After getting the result, we don't need to worry about other numbers being larger than ours. Time Complexity: O(n). |