#P10311. Constructing a Weighted Integer Average Sequence
Constructing a Weighted Integer Average Sequence
Constructing a Weighted Integer Average Sequence
You are given a sequence \(a = [a_1, a_2, \dots, a_n]\) of length \(n\) and an integer \(m\). It is guaranteed that each element of \(a\) is a distinct positive integer and does not exceed \(m\). Your task is to construct a sequence \(b = [b_1, b_2, \dots, b_n]\) such that:
- Each \(b_i\) is a positive integer not exceeding \(m\);
- The weighted average \[ \frac{\sum_{i=1}^{n} a_i \cdot b_i}{\sum_{i=1}^{n} b_i} \] is an integer; that is, with weights \(b_i\) the average of \(a\) is an integer;
- No three indices form an ordered triple \((i,j,k)\) with \(1 \le i < j < k \le n\) such that \(b_i=b_j=b_k\) (i.e. any value appears at most twice in \(b\)).
If such a sequence \(b\) exists, output it. Otherwise, report that no solution exists.
Note: It is implicitly assumed that \(n \leq 2\,m\) so that a solution is possible if the weighted average condition can be met.
inputFormat
The first line contains two integers \(n\) and \(m\) separated by a space.
The second line contains \(n\) distinct positive integers \(a_1, a_2, \dots, a_n\) (each not exceeding \(m\)).
outputFormat
If a valid sequence \(b\) exists, output "YES" on the first line, and on the second line output \(n\) space‐separated integers representing \(b_1, b_2, \dots, b_n\).
If no such sequence exists, output a single line containing "NO".
sample
3 3
2 1 3
YES
2 1 1
</p>