#K4346. Road Trip Itinerary

    ID: 27314 Type: Default 1000ms 256MiB

Road Trip Itinerary

Road Trip Itinerary

You are given n cities arranged in a circle. Each city has a fun rating given in a list. The task is to determine the sequence of city indices to visit on a road trip. The journey always starts at the city with the highest fun rating (if there are multiple cities with the same max rating, choose the one with the smallest index). Depending on the direction specified, the order of visiting the cities will be computed as follows:

If the direction d is 1 (clockwise), the cities are visited in increasing order modulo $n$ starting from the chosen city.

If d is 2 (counter-clockwise), the cities are visited in the reverse order, again using modulo arithmetic.

The modulo arithmetic can be formally described as: if the starting city index is s, then the sequence for clockwise direction is given by $$s, \; ((s+1-1) \bmod n)+1, \; ((s+2-1) \bmod n)+1, \; \ldots, \; ((s+n-1-1) \bmod n)+1,$$ and for counter-clockwise direction by reversing the increments.

Your output should be a single line of n space-separated integers representing the order in which the cities are visited.

inputFormat

The input is provided via standard input and consists of three lines:

  1. The first line contains an integer n, the number of cities.
  2. The second line contains n space-separated integers representing the fun ratings of the cities.
  3. The third line contains an integer d which is either 1 (for clockwise) or 2 (for counter-clockwise).

outputFormat

Output to the standard output a single line with n space-separated integers, representing the sequence of city indices in the order the friends will visit them.

## sample
5
3 2 4 1 5
1
5 1 2 3 4