Reverse and Rejoin
Time limit3sMemory limit512 MB
Split a fixed sequence into two non-empty parts, reverse each part, and print the lexicographically smallest result among all split positions.
- Level
Medium7 of 10
- Topics
- Array, String matching, Sorting, Implementation
- Solved
- No attempts yet
Problem
You are given a sequence of numbers. The same number can appear several times, and every number is a natural number smaller than .
Split the sequence into two parts, each of non-zero length, reverse the order inside each part, then join the two parts back in their original front to back order. Different split positions give different sequences. Print the sequence that comes first in lexicographic order among the sequences you can build this way.
In lexicographic order the sequence with the smaller first number comes first. If the first numbers are equal, the one with the smaller second number comes first, and if the second numbers are equal too, the third numbers decide, and so on. All sequences compared here have the same length.
Input
The first line has an integer (). The second line has the numbers of the sequence in order, separated by spaces. Each number is a natural number smaller than .
Output
On the first line, print the sequence that comes first in lexicographic order among the sequences you can build. Print the numbers on one line, separated by single spaces.