#include <iostream>
#include <vector>

using namespace std;

int n;
int holdVal = 0;
int currentVal = 0;
int matchFound = 0;
vector<int> book;
vector<int> first(10, -1);
vector<int> last(10, -1);

int main()
{
    cin >> n;
    book.resize(n);

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

    for (int i = 0; i < n; i++) {
        if (first[book[i]] == -1) {
            first[book[i]] = i;
        }
       
        last[book[i]] = i;

    }


    for (int i = 0; i < n; i++) {
        currentVal = book[i];
        for (int k = 9; k > currentVal; k--) {
            if (last[k] > i) {
                matchFound = last[k];
               
                holdVal = book[i];
                book[i] = book[matchFound];
                book[matchFound] = holdVal;
                holdVal = 0;
                break;
            } 
        }

        if (matchFound != 0) {
            break;
        }

    }

    if (matchFound == 0) {
        for (int i = 0; i < 10; i++) {
            if (first[i] != last[i]) {
                matchFound = 1;
                break;
            }

        }

        if (matchFound == 0) {
            holdVal = book[n-1];
            book[n-1] = book[n-2];
            book[n-2] = holdVal;
            holdVal = 0;
        }
    }



    for (int i = 0; i < n; i++) {
        cout << book[i] << " ";

    }
}