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

using namespace std;

int nadji(int niz[], int vr, int p, int br){

    for(int i = p; i < br;i++)
        if(niz[i] == vr)
        return i;
    return -1;}


int main()
{
    int n, i, c = 0, tmp = 0;
    cin >> n;
    int a[n], b[n];

    int p = 1;

    for(i = 0; i < n; i++){
        cin >> a[i];
    }
   for(i = 0; i < n; i++){
        b[i] = a[i];
    }

    sort(b, b+n);

    if(n == 2) {
        for(i = n-1; i >=0;i--)
            cout << a[i] << " ";
    }
    else{
        for(i = 0; i < n && p;i++){
            if(a[i] < b[n-1-i]){
                c = nadji(a, b[n-1-i], i+1, n);
                tmp = a[i];
                a[i] = a[c];
                a[c] = tmp;
                p = 0;
            }
        }

        if(p == 1){
            if(a[0] == a[1]){
                tmp = a[0];
                a[0] = a[1];
                a[1] = tmp;
            }
            else{
                tmp = a[n-1];
                a[n-1] = a[n-2];
                a[n-2] = tmp;
            }
        }
        for(i = 0; i < n; i++)
            cout << a[i] << " ";
    }


    return 0;
}