#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    if (n == 2)
    {
        cout << a[1] << " " << a[0];
        return 0;
    }
    int maks = 1;
    int k = 1;
    while (a[k] <= a[k - 1])
        k++;
    for (int i = k; i < n; i++)
    {
        if (a[i] > a[maks])
            maks = i;
    }
    int l = 0;
    bool ok = false;
    int g;
    while (not ok)
    {
        if (l >= n)
        {
            g = a[n - 1];
            a[n - 1] = a[n - 2];
            a[n - 2] = g;
            ok = true;
        }
        else
        {
            if (maks >= l)
            {
                g = a[maks];
                a[maks] = a[l];
                a[l] = g;
                ok = true;
            }
            else
                l++;

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