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

const int N = 1e6 + 10;
int a[N];
int main () {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0 ; i < n ; i++) cin >> a[i];

    int ind1 = 0, ind2 = 1;
    int mx = 0;
    for (int i = 0 ; i < n - 1 ; i++) {
        if(a[i] < a[i + 1]) {
            ind1 = i;
            ind2 = ind1 + 1;
            break;
        }
    }

    for (int i = ind1 + 1 ; i < n ; i++) {
        if (a[i] > a[ind2]) ind2 = i;
    }

    // cerr << ind1 << " " << ind2 << "\n";
    swap(a[ind1], a[ind2]);

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

    return 0;
}