#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n;
    cin>>n;
    if(n==2)
    {
        ll x,y;
        cin>>x>>y;
        cout<<y<<" "<<x;
    }
    else
    {
        ll a[n];
        for(ll i=0;i<n;i++)
        {
            cin>>a[i];
        }
        ll nv=-1;
        ll id=0,id1=0,id2=0;
        for(ll i=n-1;i>=0;i--)
        {
            if(a[i]>nv){nv=a[i];id=i;}
            if((a[i]<nv) || (a[i]==nv && id2<i && id2>id)){id1=id;id2=i;}
        }
        if(id1 ==0 && id2 ==0)
        {
            ll pom=a[n-1];
            a[n-1]=a[n-2];
            a[n-2]=pom;
        }
        else
        {
            ll pom=a[id1];
            a[id1]=a[id2];
            a[id2]=pom;
        }
        for(ll i=0;i<n;i++)
        {
            cout<<a[i]<<" ";
        }
    }


}