#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[n];

    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }


    int ind1=-1,ind2=0,max1=-1,max2=-1;
       do
       {
            ind1++;
            ind2=-1;
            for(int j=ind1+1;j<n;j++)
            {
                if(a[j]>=a[ind1] && a[j]>a[ind2])
                    ind2=j;

            }
            if(a[ind1]==a[ind2])
            {
                max1=ind1;
                max2=ind2;
            }
       }
       while(ind1<n-2 && (ind2==-1 || a[ind1]==a[ind2]));

        if(ind2==-1)
        {
            if(max1==-1)
                swap(a[n-1],a[n-2]);

        }
        else
            swap(a[ind1],a[ind2]);

        for(int i=0;i<n;i++)
            cout<<a[i]<<" ";





       
    return 0;
}