#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    if(n==2)
    {
        ll x,y;
        cin>>x>>y;
        cout<<y<<" "<<x;
    }
    else
    {
        ll a[n],opad=1,isti=0;
        cin>>a[0];
        for(ll i=1;i<n;i++)
        {
            cin>>a[i];
            if(a[i]>a[i-1]){opad=0;}
            if(a[i]==a[i-1]){isti=1;}

        }
        if(opad==1)
        {
            if(isti==1)
            {
                for(ll i=0;i<n;i++)
                {
                    cout<<a[i]<<" ";
                }
            }
            else
            {
                ll pom=a[n-1];
                a[n-1]=a[n-2];
                a[n-2]=pom;
                for(ll i=0;i<n;i++)
                {
                    cout<<a[i]<<" ";
                }
            }

        }
        else
        {
            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;}
                else{
                if((a[i]<nv) ){id1=id;id2=i;}
                }
            }
            ll pom=a[id1];
            a[id1]=a[id2];
            a[id2]=pom;
            for(ll i=0;i<n;i++)
            {
                cout<<a[i]<<" ";
            }
        }
    }


}