#K94502. Restore the Original Sequence
Restore the Original Sequence
Restore the Original Sequence
Petya had a sequence of n integers, but he accidentally shuffled it. Fortunately, he remembers that the sum of the original sequence satisfied \(\sum_{i=1}^{n} a_i = s\) and that it contained exactly m unique integers. Given the shuffled sequence, your task is to restore any valid ordering of the sequence that meets both conditions, or report that it is impossible.
For example, if n = 5
, m = 3
, the shuffled sequence is [1, 2, 2, 3, 2]
, and s = 10
, one valid restored sequence is [1, 2, 2, 3, 2]
(note that any permutation of the input which satisfies the conditions is accepted). If no such permutation exists, output -1
.
Note: The sequence's sum and its multiset remain unchanged under permutation. Therefore, the necessary and sufficient conditions for a valid sequence are:
\(\sum_{i=1}^{n} a_i = s \quad \text{and} \quad |\{a_1,a_2,\dots,a_n\}| = m\).
inputFormat
The first line contains three space-separated integers n
, m
, and s
, where n
is the length of the sequence, m
is the number of distinct integers in the original sequence, and s
is the sum of the sequence.
The second line contains n
space-separated integers representing the shuffled sequence.
outputFormat
If a valid permutation exists, print the sequence as n
space-separated integers in one line. Otherwise, print -1
.
5 3 10
1 2 2 3 2
1 2 2 3 2
</p>