#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
    {
        int a = s[i], t;
        vector<int> pos;

        for (int j = i; j < n; j++)
        {
            if (ssor[j] == a)
            {
                t = j;
                break;
            }
        }

        for (int j = t; j > i; j--)
        {
            if (s[j] == ssor[i])
            {
                pos.push_back(j);
            }
        }
        for (int j = t; j < n; j++)
        {
            if (s[j] == ssor[i])
            {
                pos.push_back(j);
            }
        }

        string maks = s;

        for (int h = 0; h < (int)pos.size(); h++)
        {
            string str2 = s;

            swap(str2[i], str2[pos[h]]);
            if (str2 > maks) maks = str2;
        }

        for (int c = 0; c < n; c++)
        {
            cout << maks[c] << " ";
        }
        cout << endl;
    }

    return 0;
}

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