#include <bits/stdc++.h>

using namespace std;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    int cnt[10], pos[10];
    for(int i = 0; i < 10; i++)
        cnt[i] = 0, pos[i] = -1;

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

    int index = -1;
    for(int i = 0; i < n; i++) {
        int mx = 0;
        for(int i = 9; i >= 0; i--) {
            if(cnt[i] > 0) {
                mx = i;
                break;
            }
        }
        if(arr[i] < mx) {
            index = i;
            swap(arr[i], arr[pos[mx]]);
            break;
        }
        cnt[mx]--;
    }

    if(index == -1) {
        for(int i = 0; i < n - 2; i++)
            cout << arr[i] << " ";
        cout << arr[n - 1] << " " << arr[n - 2] << endl;
        return 0;
    }

    for(int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
    return 0;
}