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

using namespace std;

int a[1000006], b[1000006];
map<int, int> m;

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)
    {
        for(int i = 0; i < n; i++)
        {
            m[a[i]]++;
        }

        bool moze = false;
        for(auto i: m)
        {
            if(i.second > 1)
                moze = true;
        }

        if(!moze)
            swap(a[n-1], a[n-2]);
    }

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

}