#include <bits/stdc++.h>

using namespace std;

int main()
{
    long n;
    scanf("%ld",&n);
    long a[n];
    for (long i=1;i<n+1;i++)
        scanf("%ld",&a[i]);


    if (n==2)
    {
        long t;
        t=a[1];
        a[1]=a[2];
        a[2]=t;
        printf("%ld %ld",a[1],a[2]);
    }
    if (n>2)
    {
        long maks[n];
        long poz[n];
        maks[n-1]=a[n];
        poz[n-1]=n;
        for (long i=n-2;i>0;i--)
        {
            if (maks[i+1]>a[i+1]){maks[i]=maks[i+1];poz[i]=poz[maks[i+1]];}
            if (maks[i+1]<=a[i+1]){maks[i]=a[i+1];poz[i]=i+1;}
        }
        for (long i=1;i<n;i++)
        {
            if (a[i]<maks[i])
            {
                long t;
                t=a[i];
                a[i]=a[poz[i]];
                a[poz[i]]=t;
                break;
            }

            if (i==n-1)
            {
               long t;
               t=a[i];
               a[i]=a[n];
               a[n]=t;
            }
        }
        for (long i=1;i<n+1;i++)
            printf("%ld ",a[i]);

    }
    return 0;
}