#include <bits/stdc++.h>
#include<vector>

using namespace std;

void swaping(int a[], int i, int j){
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

void fun(int a[], int n){
    int maxx = -1, maxi;
    int l = -1, r;
    for(int i = n - 1; i >= 0; i--){
        if(a[i] > maxx){
            maxx = a[i];
            maxi = i;
        }else{
            if(a[i] <= maxx){
                l = i;
                r = maxi;
            }
        }
    }

    if(l == -1){
        swaping(a, n - 2, n - 1);
    }else{
        swaping(a, r, l);
    }
}

int main(void)
{
    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d", &n);
    int a[n];
    int r;
    for(int i = 0;i < n; i++){
        scanf("%d", &r);
        a[i] = r;
    }
    if(n == 2){
        swaping(a, 0, 1);
    }else{
        fun(a, n);
    }
    for(int i = 0;i < n; i++){
        cout << a[i] << " ";
    }
}