#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];
    }
    priority_queue<pair<long,long> > q;
    q.push({a[n-1],n-1});
    q.push({a[n-2],n-2});
    long f=n-2,s=n-1;
    for (long i=n-3;i>=0;i--)
    {
        long cur=q.top().first,ind=q.top().second;
        if (cur>=a[i]) { f=i; s=ind; }
        q.push({a[i],i});
    }
    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;
}