#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    stringstream ss;
    string s, ssor;
    cin >> n;

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

        ss << a;
    }

    s = ss.str();
    ssor = s;
    sort(ssor.begin(), ssor.end(), greater<char>());

    int i = 0;

    while (ssor[i] == s[i] && i < n)
    {
        i++;
    }

    if (i == n)
    {
        bool ima = false;
        for (int j = 1; j < n; j++)
        {
            if (ssor[j] == ssor[j-1]) ima = true;
        }

        if (ima)
        {
            for (int t = 0; t < n; t++)
            {
                cout << s[t] << " ";
            }
            cout << endl;
        }
        else
        {
            swap(s[n-1], s[n-2]);
            for (int t = 0; t < n; t++)
            {
                cout << s[t] << " ";
            }
            cout << endl;
        }
    }
    else
    {
        for (int j = n-1; j > i; j--)
        {
            if (s[j] == ssor[i])
            {
                swap(s[i], s[j]);
                break;
            }
        }
        for (int t = 0; t < n; t++)
        {
            cout << s[t] << " ";
        }
        cout << endl;
    }

    return 0;
}

/*
10
8 8 6 5 4 8 1 2 8 3
*/