#include <bits/stdc++.h>

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long n;
    cin>>n;
    long long a[n],a1[n];
    for(long long i=0;i<n;i++)
    {
        cin>>a[i];
        a1[i]=a[i];
    }
    sort(a1,a1+n,greater<>());
    /*for(long long i=0;i<n;i++)
        cout<<a1[i]<<" ";
        cout<<"\n";*/
    long long swapn=-1;
    long long swapi=-1;
    bool ds=false;
    for(long long i=0;i<n;i++)
    {
        if(i>0&&a[i]==a[i-1])
            ds=true;
        if(a1[i]!=a[i])
        {
            swapn=a1[i];
            swapi=i;
            break;
        }
    }
    //cout<<swapi<<" "<<swapn<<"\n";
    if(swapi==-1)
        {if(ds==false)
        swap(a[n-1],a[n-2]);}
    else
        for(long long i=n-1;i>=0;i--)
    {
        if(a[i]==swapn)
        {
            //cout<<i<<" "<<a[i]<<"\n";
            swap(a[i],a[swapi]);
            break;
        }
    }
    for(long long i=0;i<n;i++)
        cout<<a[i]<<" ";
}