//Failure will never overtake me if my determination to succeed is strong enough
#include <bits/stdc++.h>

using namespace std;

int a[1000005], b[1000005];

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

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

    sort(b, b+n, greater<int>());

    bool pomerio = false;
    for(int i = 0; i < n; i++)
    {
        if(a[i] != b[i])
        {
            int j;
            for(j = n-1; a[j] != b[i]; j--);
            int tmp = a[j];
            a[j] = a[i];
            a[i] = tmp;
            pomerio = true;
            break;
        }
    }

    if(!pomerio)
    {
        int tmp = a[n-1];
        a[n-1] = a[n-2];
        a[n-2] = tmp;
    }

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

}