#K83507. Rearranging Robots

    ID: 36212 Type: Default 1000ms 256MiB

Rearranging Robots

Rearranging Robots

You are given an integer ( N ) representing the number of robots. You are also given two arrays of length ( N ): one array ( R ) containing robot identifiers and another array ( P ) containing the priorities assigned to each position. Your task is to rearrange the robots such that for every position ( j ), the robot assigned to that position comes from an original position whose priority does not equal ( P[j] ). Specifically, consider sorting the priorities along with their original indices to get a sorted list ( S ). Then, for each index ( i ) (using 0-indexing), assign the robot from the original index ( S[i].index ) to the position ( (i+1) \bmod N ). If for any ( i ) the condition ( S[i].priority = P[(i+1) \bmod N] ) holds then the rearrangement is not possible. In that case, print IMPOSSIBLE. Otherwise, print the rearranged list as a sequence of space-separated integers.

Note: All formulas are given in LaTeX format.

inputFormat

The input is given via standard input (stdin) with the following format:

  1. The first line contains an integer ( N ), representing the number of robots.
  2. The second line contains ( N ) space-separated integers representing the robot identifiers.
  3. The third line contains ( N ) space-separated integers representing the priorities associated with each position.

outputFormat

The output should be written to standard output (stdout).

If a valid rearrangement exists, output one valid rearranged ordering of the robots as a space-separated list on one line. Otherwise, output the string IMPOSSIBLE.## sample

5
3 1 4 1 5
10 20 30 40 50
5 3 1 4 1