#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 maks = 0, indmaks = 0, brmaks = 0;
    for (int i = 0; i<n; i++)
    {
        if (a[i] == maks )
        {
            brmaks++;
            indmaks = i;
        }
        else if (a[i]>maks)
        {
            maks = a[i];
            brmaks = 1;
            indmaks = i;
        }
    }
    if (a[0]!=maks)
    {
        int temp = a[0];
        a[0] = a[indmaks];
        a[indmaks] = temp;
    }
    else
    {
        if (brmaks==1)
        {

            if(n-2==0)
            {
                int temp = a[0];
                a[0] = a[1];
                a[1] = temp;
            }
            else
            {
                maks = 0;
                indmaks = 0;
                for (int i = 2; i<n; i++)
                {
                    if (a[i]>maks)
                    {
                        maks = a[i];
                        indmaks = i;
                    }
                    else if (a[i] == maks) indmaks = i;
                }
                int temp = a[1];
                a[1] = a[indmaks];
                a[indmaks] = temp;
            }
        }
    }
    for (int i = 0; i<n; i++)
        cout <<a[i] <<" ";
    return 0;
}