#include <iostream>
#include <vector>

using namespace std;
typedef long unsigned int lui;
int main()
{
    std::ios::sync_with_stdio(false);
    lui n;
    cin >> n;
    vector<lui> cifre;
    for (lui i = 0; i < n; i++) {
        unsigned int k;
        cin >> k;
        cifre.push_back(k);
    }
    bool found = false;
    lui bignigga = 0;
    lui bignigga1 = n-2;
    lui bignigga2 = n-1;
    bool postoji9 = false;
    bool kopija = false;
    for (lui i = 0; i < n-1; i++) {
            //cout << "loopcina" << endl;
        for (long long int k = n-1; k > i; k--) {
            //cout << "loopic, i, k: " << i << " " << k << endl;
            if (cifre[i]==9) {
                    postoji9=true;
                    break;
            }
            //cout << "yeet" << endl;
            if (!kopija) {
                    //cout << "nema kopije" << endl;
                if (cifre[i] == cifre[k] || (cifre[k] == 9 && postoji9)) kopija=true;
            }
            if (cifre[k] > cifre[i]) {
                    //cout << "yay" << endl;
                found = true;
                //cout << "cifre[k], bignigga: " << en
                if (cifre[k] > bignigga) {
                    //cout << "big yay" << endl;
                    bignigga = cifre[k];
                    bignigga2 = k;
                    bignigga1 = i;
                    if (bignigga == 9) break;
                }

            }
        }
        if (found) break;
    }
    //cout << "kopija: " << kopija << endl;
    //cout << "FOUND!!" << endl;
    //cout << "biggnigga1,2: " << bignigga1 << " " << bignigga2 << endl;
    if (!(!found && kopija)) {
        unsigned int u = cifre[bignigga1];
        cifre[bignigga1] = cifre[bignigga2];
        cifre[bignigga2] = u;
    }
    for (lui i = 0; i <n; i++) cout << cifre[i] << " ";

    return 0;
}