#include <bits/stdc++.h>

using namespace std;
long long a[1000005];
int main()
{   long long n,z,k,l,maxx,p;
    p=0;
    cin>>n;
    for(long long i = 1 ;i <= n ; i++)
        cin>>a[i];
    maxx=0;
    if(n==2)
    cout<<a[2]<<" "<<a[1];
    else
    {
    for(long long i = 1; i<= n; i++)
        {
            maxx=0;
        for(long long j = i;j <= n; j++)
            {
             if(a[j]>=maxx)
            {
             maxx=a[j];
             l=j;
            }  
            }
               if(a[i]!=maxx)
            {
                z=a[l];
                a[l]=a[i];
                a[i]=z;
                p=1;
                break;
            }
        }
        if(p==1)
        {
             for(long long i = 1; i <= n; i++)
                cout<<a[i]<<" ";
        }
        else
        {
            for(long long i = 1; i <= n-2; i++)
                cout<<a[i]<<" ";
                cout<<a[n]<<" "<<a[n-1];
        }
    }

    return 0;
}