#include <iostream>

using namespace std;

int a[1000000];

void zameni( int n )
{
    if( n == 2 )
    {
        swap(a[0], a[1]);
        return;
    }

    int maks = -1, maksind = -1, l = -1, r = -1;

    for( int i = n-1; i >= 0; i-- )
    {
        if( a[i] > maks )
        {
            maks = a[i];
            maksind = i;
        }
        else if( a[i] < maks )
        {
            l = i;
            r = maksind;
        }
    }

    swap( a[l], a[r] );
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;

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

    zameni(n);

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

    return 0;
}