#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

long long int maxim(long long int n) {
    string s = to_string(n);
    sort(s.begin(),s.end());
    reverse(s.begin(),s.end());
    return stoi(s);
}

string largestNum(int num)
{
    long long int max_digit = -1;
    long long int max_digit_indx = -1;
    long long int l_indx = -1;
    long long int r_indx = -1;

    string num_in_str = to_string(num);
    for (long long int i = num_in_str.size() - 1; i >= 0; i--) {

        if (num_in_str[i] > max_digit) {
            max_digit = num_in_str[i];
            max_digit_indx = i;
            continue;
        }

        if (num_in_str[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
        }
    }

    if (l_indx == -1)
        return to_string(num);

    swap(num_in_str[l_indx], num_in_str[r_indx]);

    return num_in_str;
}

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

    long long int n,ns;
    string s;
    cin >> n;

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

    ns = stoi(s);

    if(ns != maxim(ns)) {
        string str = largestNum(stoi(s));
        for(long long int i = 0; i < n; ++i) {
            cout << str[i] << " ";
        }
        return 0;
    } else {
        swap(s[n-1], s[n-2]);
        for(long long int i = 0; i < n; ++i) {
            cout << s[i] << " ";
        }
        return 0;
    }

    return 0;
}