#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>

using namespace std;

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long n;
    cin >> n;
    long *a=new long[n];
    for (long i=0;i<n;i++)
    {
        cin >> a[i];
    }
    long mx=a[n-1],ind=n-1;
    if (a[n-2]>mx) { mx=a[n-2]; ind=n-2; }
    long f=n-2,s=n-1,k=0;
    for (long i=n-3;i>=0;i--)
    {
        if (mx>a[i]) { f=i; s=ind; k=1; } else
        if (mx<a[i]) { mx=a[i]; ind=i; } else
        if (mx==a[i] && k==0) { f=i; s=ind; }
    }
    long tmp=a[f];
    a[f]=a[s];
    a[s]=tmp;
    for (long i=0;i<n;i++)
        cout << a[i] << " ";
    delete[] a;
    return 0;
}