#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n;
    cin>>n;
    if(n==2)
    {
        ll x,y;
        cin>>x>>y;
        cout<<y<<" "<<x;
    }
    else
    {
        ll a[n];
        for(ll i=0;i<n;i++)
        {
            cin>>a[i];
        }
        ll nv=-1;
        ll id1,id2;
        for(ll i=n-1;i>=0;i--)
        {
            if(a[i]>nv){nv=a[i];id1=i;}
            if(a[i]<=nv){id2=i;}
        }
        ll pom=a[id1];
        a[id1]=a[id2];
        a[id2]=pom;
        for(int i=0;i<n;i++)
        {
            cout<<a[i]<<" ";
        }
    }

}