#include <bits/stdc++.h>

using namespace std;
long long a[2000050], maxx[2000050];
int main()
{
    long long n,k,i;
    cin >> n;
    for (i=0;i<n;i++) cin >> a[i];
    for (i=n-1;i>=0;i--){
        if (i==n-1) maxx[i]=a[i];
        else {
            if (a[i]>maxx[i+1]) maxx[i]=a[i];
            else maxx[i]=maxx[i+1];
        }

    }
    for (i=0;i<n;i++){
        if (a[i]!=maxx[i]){
            k=a[i];
            a[i]=maxx[i];
            for (int j=n-1;j>=0;j--){
                if (a[j]==maxx[i]) {a[j]=k;break;}
            }
            break;
        }
    }
    if (i==n){
        for (int i=0;i<n-2;i++) cout << a[i] << ' ';
        cout << a[n-1] << ' ' << a[n-2];
    }
    else for (int i=0;i<n;i++) cout << a[i] << ' ';
    return 0;
}