Under construction

Still being built. Things may change

Morty and the Password

Interview

Time limit2sMemory limit1024 MB

Summary
Given a permutation where each cup can be touched at most twice by adjacent swaps, output the lexicographically largest permutation reachable.
Level

Medium6 of 10

Topics
Greedy, Array
Solved
No attempts yet

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 nn juice cups in a row in front of Morty. Each cup has a number from 11 to nn written on it. The cup at position ii from the left has the number aia_i. All numbers are distinct, so they form a permutation of 11 to nn.

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 nn (1≤n≤100 0001 \le n \le 100\,000), the number of cups.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n), the numbers on the cups at the start of the experiment. All aia_i are distinct.

Output

Print nn integers separated by spaces: the password.

Hint

A permutation aa of length nn is lexicographically greater than a permutation bb of length nn if there exists an xx such that ai=bia_i = b_i for all ii from 11 to x−1x - 1 and ax>bxa_x > b_x.

Examples2

  1. Example 1

    Input
    5
    5 4 3 2 1
    
    Expected output
    5 4 3 2 1
    
  2. Example 2

    Input
    7
    7 1 2 3 4 5 6
    
    Expected output
    7 3 4 1 2 6 5