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

const int N = 1e6 + 10;
int a[N];
int digits[10];

int f(int n, int i, int x) {
    for(int j = n - 1 ; j > i ; j--) 
    if(a[j] == x) return j;
    return -1;
}

int main () {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0 ; i < n ; i++) {
        cin >> a[i];
        digits[a[i]]++;
    }

    int ind1 = -1, ind2 = -1;
    int nr = 0;

    for(int i = 9 ; i >= 0 ; i--) {
        for(int j = nr ; j < nr + digits[i] ; j++) {
            if (a[j] < i) {
                ind1 = j;
                ind2 = f(n, j, i);
                break;
            }
        } 
        if(ind1 != -1 && ind2 != -1) break;
        nr += digits[i];
    }

    if(ind1 == -1 || ind2 == -1) {
        ind1 = n - 1;
        ind2 = n - 2;
    }

    swap(a[ind1], a[ind2]);

    for(int i = 0 ; i < n ; i++) cout << a[i] << " ";
    cout << "\n";

    return 0;
}