#P7174. Maximum Multiple of $30$
Maximum Multiple of $30$
Maximum Multiple of
Mirko discovered a positive integer n. Since he loves the number $30$, he wants to know the maximum multiple of $30$ that can be formed by rearranging the digits of n. In other words, by using the digits of n exactly once each, determine the largest number that is a multiple of $30$. If no such number exists, output -1.
Note: A number is a multiple of $30$ if and only if it is divisible by both $10$ and $3$. Therefore, the number must contain at least one 0
(to be divisible by $10$) and the sum of its digits must be divisible by $3$.
inputFormat
The input consists of a single line containing a positive integer n
. The number n
can have up to 105 digits.
outputFormat
Output the largest number (formed by rearranging the digits of n
) that is a multiple of $30$. If no such number exists, output -1.
sample
30
30