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

typedef uint64_t ll;

int main(){
    int n, p;
    cin >> n;

    ll knjige[n], max_br[n] = {};
    int max_br_i[n] = {};
    for (int i = 0; i < n; i++)
        cin >> knjige[i];

    for (int i = n - 1; i >= 0; i--){
        if (i == n - 1){
            max_br[i] = knjige[i];
            max_br_i[i] = i;
        }
        else if (knjige[i] >= max_br[i + 1]){
            max_br[i] = knjige[i];
            max_br_i[i] = i;
        }
        else{
            max_br[i] = max_br[i + 1];
            max_br_i[i] = max_br_i[i + 1];
        }
    }

    bool b = 0;
    for (int i = 0; i < n - 1 && !b; i++)
        if (knjige[i] <= max_br[i + 1]){
            swap(knjige[i], knjige[max_br_i[i]]);
            b = 1;
        }


    if (b == 0)
        swap(knjige[n - 1], knjige[n - 2]);

    for (auto i : knjige)
        cout << i << " ";
    cout << endl;

    return 0;
}