#include <iostream>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int N ;
    cin >> N;
    int arr[N];
    for(int i = 0 ; i < N ; i++)
    {
        cin >> arr[i];
    }

    if(N == 2)
    {
        swap(arr[0] , arr[1]);
    } else
    {
        int p =0;
        int value = arr[0];
        for(int i = 1 ; i<N ;i++)
        {
            if (value >= arr[i])
            {
                value = arr[i];
                p++;
            } else {
                break;
            }

        }

        int max1 = arr[0];
        int positionmax1 = 0;

        for(int i = 0 ; i < N ; i++ )
        {

            if( max1 <= arr[i])
            {
                max1 = arr[i];
                positionmax1 = i;
            }
        }
        if(arr[0] == max1)
        {
            if(p == N-1)
            {
            int max2 = arr[1];
            int positionmax2 = 1;

            for(int i = 1 ; i < N ; i++ )
            {

                if( max2 <= arr[i])
                {
                    max2 = arr[i];
                    positionmax2 = i;
                }
            }

            swap(arr[1] , arr[positionmax2]);
            } else {

            int max3 = arr[p];
            int positionmax3 = p;

            for(int i = p ; i < N ; i++ )
            {

                if( max3 <= arr[i])
                {
                    max3 = arr[i];
                    positionmax3 = i;
                }
            }
                swap(arr[p] , arr[positionmax3]);
            }

        } else {
            swap(arr[0] , arr[positionmax1]);
        }





    }

    for(int i = 0 ; i < N ; i++)
    {
        cout << arr[i] << " " ;
    }

}