#include <bits/stdc++.h>

using namespace std;

int ar[1000000];

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

    int n;
    cin >> n;

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

    vector<int> v;
    sort(v.begin(), v.end(), greater<>());

    bool brP = false;
    int ind = -1, br = 0;
    for(int i = 0; i < n; i++)
    {
        if(v[i] != ar[i])
        {
            brP = true;
            ind = i;
            br = v[i];
            break;
        }
    }

    if(!brP)
    {
        swap(ar[n-2], ar[n-1]);
    }
    else
    {
        for(int i = n-1; i >= 0; i--)
        {
            if(ar[i] == br)
            {
                swap(ar[ind], ar[i]);
                break;
            }
        }
    }

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