#P9427. Largest 2023 Multiple from 2s and 3s
Largest 2023 Multiple from 2s and 3s
Largest 2023 Multiple from 2s and 3s
Blue wants to form an integer with exactly N copies of the digit \(2\) and M copies of the digit \(3\), for a total of \(N+M\) digits. The task is to arrange these digits in some order so that the resulting number is as large as possible while being a multiple of \(2023\). If no such arrangement exists, output -1.
Note: The number is allowed to have leading digits \(2\) or \(3\) (there are no zeros). A valid solution is an arrangement (permutation) of exactly N digits \(2\) and M digits \(3\) whose numerical value is divisible by \(2023\), and among all such numbers the lexicographically largest (i.e. numerically maximum) should be found.
Mathematically, if the permutation is represented as \(d_1d_2\dots d_{N+M}\) with each \(d_i\in\{2,3\}\), we require:
[ \sum_{i=1}^{N+M} d_i \times 10^{N+M-i} \equiv 0 \pmod{2023}. ]
inputFormat
The input consists of a single line containing two space‐separated integers \(N\) and \(M\) \((0 \leq N, M \leq 50)\), representing the number of digit \(2\) and digit \(3\) available respectively.
outputFormat
Output the largest \(2023\)-multiple that can be formed using exactly \(N\) digits \(2\) and \(M\) digits \(3\). If no such arrangement exists, output -1.
sample
1 1
-1