#P11592. Circular Chairs Rearrangement

    ID: 13686 Type: Default 1000ms 256MiB

Circular Chairs Rearrangement

Circular Chairs Rearrangement

In a game there are n players and n chairs arranged in a circle. Each chair is labeled with an integer \(s_i\) (with \(1 \le s_i \le n\)). When a bell rings, the player sitting on a chair must move \(s_i\) steps clockwise. An arrangement of chairs is considered legal if after each player moves according to his/her chair’s number, every chair is occupied by exactly one player (i.e. the mapping \(f(i)= (i+s_i) \mod n\) yields a permutation of \(\{0,1,2,\dots,n-1\}\)).

You are given the numbers on the chairs. You can reorder them arbitrarily. Determine if it is possible to rearrange the numbers such that the resulting arrangement is legal. If it is possible, output one valid rearrangement.

Note: For every chair, treat the number \(n\) as representing a value whose remainder modulo \(n\) is 0 (i.e. if \(s_i=n\) then \(s_i\equiv0 \pmod{n}\)). A necessary (and in this problem, sufficient) condition for a legal arrangement is that \(\sum_{i=1}^{n} s_i \equiv 0 \pmod{n}\).

The original order of numbers is irrelevant; you only need to use exactly the given multiset of numbers.

inputFormat

The first line contains a single integer \(n\) (the number of players and chairs).

The second line contains \(n\) integers \(s_1, s_2, \dots, s_n\) where \(1 \le s_i \le n\) representing the numbers on the chairs.

outputFormat

If a legal rearrangement exists, output YES on the first line, and on the second line output the rearranged \(n\) numbers (separated by spaces) representing one valid scheme. In the final output, if an assigned value is 0 it corresponds to \(n\) (since \(n \equiv 0 \pmod{n}\)).

If no legal rearrangement exists, output NO.

sample

4
1 1 3 3
YES

1 3 1 3

</p>