#include <bits/stdc++.h>

using namespace std;

string func1(string num)
{
    int max_digit = -1;
    int max_digit_index = -1;
    int l_index = -1;
    int r_index = -1;

    for(long long int i = num.size() - 1; i >= 0; i--)
    {
        if(num[i] > max_digit)
        {
            max_digit = num[i];
            max_digit_index = i;
            continue;
        }

        if(num[i] < max_digit)
        {
            l_index = i;
            r_index = max_digit_index;
        }

    }

    if(l_index == -1)
    {
        int index1 = num.size() - 1;
        int index2 = num.size() - 2;
        swap(num[index1], num[index2]);
        return num;
    }

    swap(num[l_index], num[r_index]);

    return num;
}


int main()
{
    long long int n;
    cin >> n;
    string num = "";
    string ai;
    for(long long int i = 0; i < n; i++)
    {
        cin >> ai;
        num = num + ai;
    }

    string sol;
    sol = func1(num);


    for(long long int i = 0; i < sol.size(); i++)
    cout << sol[i] << " ";



    return 0;
}