#K55962. Largest Even Product Permutation

    ID: 30092 Type: Default 1000ms 256MiB

Largest Even Product Permutation

Largest Even Product Permutation

You are given an integer n and a list of n digits. Your task is to arrange these digits to form the largest possible number such that the product of its digits is even. A product is even if and only if at least one digit is even. If it is impossible to form such a permutation (i.e. none of the digits is even), you should output -1.

Note: The list may contain duplicate digits and even the digit 0.

Mathematical Formulation:

Let \( d_1,d_2,\dots,d_n \) be the digits provided. You need to find a permutation \( (p_1,p_2,\dots,p_n) \) such that: \[ \text{value} = p_1p_2\dots p_n \quad \text{(as a concatenated number)} \] and \[ \prod_{i=1}^{n}p_i \equiv 0 \pmod{2}. \] If no valid permutation exists, output \(-1\).

inputFormat

Input is given via standard input (stdin). The first line contains an integer n representing the number of digits. The second line contains n space-separated integers, each being a single digit.

outputFormat

Output a single number on standard output (stdout): the largest permutation (when the digits are concatenated) that yields an even product. If no valid permutation exists, output -1.## sample

4
1 3 5 2
5321