Morty and the Password
InterviewTime limit2sMemory limit1024 MB
Given a permutation where each cup can be touched at most twice by adjacent swaps, output the lexicographically largest permutation reachable.
Problem
Rick wanted to test how resourceful his grandson Morty is. Morty's beloved Jessica is locked in a room behind a code lock, and Morty must pass Rick's experiment to save her.
Rick placed juice cups in a row in front of Morty. Each cup has a number from to written on it. The cup at position from the left has the number . All numbers are distinct, so they form a permutation of to .
Morty may swap two adjacent cups as many times as he likes. His hands shake, so each swap spills some juice from both cups. Rick allows him to touch each cup at most two times. The safe password is the lexicographically largest permutation, read from left to right, that can result from the experiment.
Help Morty find the password and save Jessica!
Input
The first line contains the integer (), the number of cups.
The second line contains integers (), the numbers on the cups at the start of the experiment. All are distinct.
Output
Print integers separated by spaces: the password.
Hint
A permutation of length is lexicographically greater than a permutation of length if there exists an such that for all from to and .