#K5936. Arrangement of Paintings

    ID: 30847 Type: Default 1000ms 256MiB

Arrangement of Paintings

Arrangement of Paintings

You are given n paintings, each with an associated importance level, and an integer k. Your task is to determine an arrangement of these paintings such that for every pair of adjacent paintings, the sum of their importance levels is not divisible by k. If such an arrangement is impossible, output -1.

Definition:

  • Let the paintings be represented by an array A of n integers where A[i] is the importance level of the i-th painting.
  • An arrangement is valid if for all 1 ≤ i < n, (A[i] + A[i+1]) mod k ≠ 0.

If more than one valid arrangement exists, you may output any one of them. It is guaranteed that n is small enough for a brute-force solution to work.

inputFormat

The input is given from standard input in the following format:

n k
A1 A2 ... An

Where:

  • n (integer): The number of paintings.
  • k (integer): The divisor used to check the sum condition.
  • A1, A2, ..., An (integers): The importance levels of each painting.

outputFormat

Output a valid arrangement of the paintings (a sequence of n integers separated by a space) from standard output if one exists. If no valid arrangement exists, output -1.

## sample
5 3
5 7 9 12 14
5 9 7 12 14

</p>