#include <bits/stdc++.h>

using namespace std;

int maxIn(int a[], int n, int j)
{
    int maxi=a[j], p = j;
    for(int i=j+1; i<n; i++)
        if(a[i]>maxi){
            maxi=a[i];
            p=i;
        }
    return p;
}

int main()
{
    int n, i, p;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }

    if(n==2)
    {
        p=a[0];
        a[0] = a[1];
        a[1] = p;
    }
    else
    {
        i=0;
        int l=0;
        while(a[i]>=a[i+1]&&i<n) i++;
        while(a[l]>a[l+1]) l++;
        if(i == n-1)
        {
            if(a[n-1]<a[n-2])
            {
                p=a[n-1];
                a[n-1] = a[n-2];
                a[n-2] = p;
            }
            else
            {
                p=a[n-1];
                a[n-1] = a[l];
                a[l] = p;
            }
        }
        else
        {
            int j = maxIn(a,n,i+1);
            p=a[i];
            a[i] = a[j];
            a[j] = p;
        }
    }
        for(i=0;i<n;i++)
            printf("%d ",a[i]);
    }