This page is still under construction.

Parts of this page are still being built. What you see may change.

Hossa (Bull Market)

Time limit1sMemory limit128 MB

Summary
Given a hossa permutation, output the next hossa in the defined recursive order.
Level

Hard8 of 10

Topics
Combinatorics, Recursion
Solved
No attempts yet

Problem

Consider a sequence a1,a2,…,ana_1, a_2, \ldots, a_n of nn distinct natural numbers. We call this sequence a hossa (a bull market) if for every three positions i<j<ki < j < k the following holds: whenever ai<aja_i < a_j, we have ak>aia_k > a_i.

Intuitively, if between two days ii and jj the price rose from aia_i to aja_j (that is, ai<aja_i < a_j), then on no later day does the price ever fall back to aia_i or below.

For example, the 14 hossas made of the elements 1,2,3,41, 2, 3, 4 are:

(1,2,3,4), (2,1,3,4), (1,3,2,4), (3,1,2,4), (3,2,1,4), (1,2,4,3), (2,1,4,3), (1,4,2,3), (1,4,3,2), (4,1,2,3), (4,2,1,3), (4,1,3,2), (4,3,1,2), (4,3,2,1)(1,2,3,4),\ (2,1,3,4),\ (1,3,2,4),\ (3,1,2,4),\ (3,2,1,4),\ (1,2,4,3),\ (2,1,4,3),\ (1,4,2,3),\ (1,4,3,2),\ (4,1,2,3),\ (4,2,1,3),\ (4,1,3,2),\ (4,3,1,2),\ (4,3,2,1)

By contrast, (3,2,4,1)(3,2,4,1) is not a hossa, because the price rose from 33 to 44 and later fell to 11.

Every hossa can be written as L m PL\,m\,P, where mm is the largest value, LL is the elements to the left of mm, and PP is the elements to the right of mm. For instance, in the hossa (1,2,4,3)(1,2,4,3) we have m=4m = 4, L=(1,2)L = (1,2), and P=(3)P = (3). Both LL and PP are themselves hossas made of fewer elements.

For two different hossas H1=L1 m P1H_1 = L_1\,m\,P_1 and H2=L2 m P2H_2 = L_2\,m\,P_2 built from the same numbers (one is a permutation of the other), we define H1<H2H_1 < H_2 as follows:

  1. If P1P_1 has fewer elements than P2P_2, then H1<H2H_1 < H_2.
  2. If they have the same number of elements, compare whether the hossa P1P_1 is smaller than the hossa P2P_2 (that is, P1<P2P_1 < P_2).
  3. If P1P_1 and P2P_2 are exactly the same sequence, compare whether L1<L2L_1 < L_2.

Sorting the hossas made of 1,2,3,41, 2, 3, 4 by this order gives the list shown above. Some comparisons:

  • (1,2,3,4)<(2,1,3,4)(1,2,3,4) < (2,1,3,4): by rule 3 it reduces to (1,2,3)<(2,1,3)(1,2,3) < (2,1,3), which by rule 3 reduces to (1,2)<(2,1)(1,2) < (2,1), which holds by rule 1.
  • (1,4,2,3)<(1,4,3,2)(1,4,2,3) < (1,4,3,2): by rule 2 it reduces to (2,3)<(3,2)(2,3) < (3,2).

Given a hossa HH, output its immediate successor XX: the unique hossa XX such that

  • H<XH < X, and
  • X<H′X < H' for every other hossa H′H' with H<H′H < H'.

You may assume that such an XX always exists for the given input. For example, the immediate successor of (1,2,3,4)(1,2,3,4) is (2,1,3,4)(2,1,3,4), the successor of (2,1,3,4)(2,1,3,4) is (1,3,2,4)(1,3,2,4), and the next one is (3,1,2,4)(3,1,2,4).

Input

The first line contains an integer nn (2≤n≤1062 \le n \le 10^6). The second line contains nn distinct natural numbers, separated by single spaces, each at most 10610^6; these are the elements of some hossa HH.

Output

Print the nn natural numbers of the immediate successor of the hossa HH on one line, separated by single spaces.

Examples3

  1. Example 1

    Input
    4
    20 10 30 40
    
    Expected output
    10 30 20 40
    
  2. Example 2

    Input
    10
    3 2 1 10 9 8 7 6 5 4
    
    Expected output
    1 2 10 3 4 5 6 7 8 9
    
  3. Example 3

    Input
    10
    3 2 1 10 4 5 7 6 9 8
    
    Expected output
    1 2 3 10 5 4 7 6 9 8