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

/*
+========================= ULTRA OPTIMAL ==========================================+
= idea:                                                                            =
= 1) go back, find biggest local num (bigger index = better)                       =
= 2) otherwise find smallest pairee to swap (smaller index = better)               =
= 3) special cases (no best solution, swap least significant digits)               =
=                                                                                  =
= Unironically better than using quicksort / selection sort, this runs in O(n)     =
= which is kinda neccessary for 100 points LMAO LMAO BAKI MOJ PA STA JE OVO        =
=                                                                                  =
= molim te Boze daj mi 100 poena ubicu se ako ne bude 100 :(                       =
=                                                                                  =
+==================================================================================+
*/

string cringe(string num) {
    int maxi = -1;
    unsigned long long maxiIndex = -1;
    unsigned long long pairee1 = -1;
    unsigned long long pairee2 = -1;

    // Go backwards. This is better because it makes sense to swap
    // the least significant digit whenever possible.
    for (unsigned long long i = num.size() - 1; i >= 0; i--) {
        // 1) Find local largest digit

        if (num[i] > maxi) {
            maxi = num[i];
            maxiIndex = i;
            continue;
        }

        // 2) Optimal swap pairee

        if (num[i] <= maxi) {
            // 8 8 2 -> swap 8 and 8
            // 8 2 8 -> sure, foolish niggas could swap 8 and 8 but before the 1st '8' is a '2'
            // which is smaller than 8, so it would produce a better solution.
            if (num[pairee2] == num[i] && num[pairee1] < num[i]) {
                continue;
            }

            pairee1 = i;
            pairee2 = maxiIndex;
        }
    }

    if (pairee1 == -1) {
        // 3) If no swapping could produce a number which is larger/equal to original (first pairee index is -1 which means it doesn't exist
        // Swap the last 2 digits (smallest change)
        unsigned long long len = num.size();
        swap(num[len - 1], num[len - 2]);
        return num;
    } else { // Regular swap
        swap(num[pairee1], num[pairee2]);
        return num;
    }
}

int main() {
    unsigned long long n;
    cin >> n;

    // Input as string

    string num;
    for (unsigned long long i = 0; i < n; i++) {
        int x;
        cin >> x;
        num += to_string(x);
    }

    // Algo

    num = cringe(num);

    // Output

    for (unsigned long long i = 0; i < num.size(); i++) {
        cout << num[i] << ' ';
    }

    return 0;
}