#include <bits/stdc++.h>
using namespace std;
int a[1000007];
void l(int n)
{
    int max_digit = -1;
    int max_digit_indx = -1;
    int l_indx = -1;
    int r_indx = -1;



    for (int i = n-1; i >= 0; i--) {


        if (a[i]> max_digit) {
            max_digit =a[i];
            max_digit_indx = i;
        }


        if (a[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
        }
    }


    if (l_indx == -1)
        {
            if(n>10)return;

            swap(a[n-1],a[n-2]);
        }

    swap(a[l_indx],a[r_indx]);
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;++i)
    {
        cin>>a[i];
    }


    l(n);
    for(int i=0;i<n;++i)cout<<a[i]<<' ';
}