#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;

int daLiJeSort(int *a,int n)
{
    int k=0;
    for (int i=0;i<n-1;i++)
    {
        if (a[i]>=a[i+1]) k++;
    }
    if (k==n-1) return 1;
    else return 0;
}

int main()
{
    int n;
    scanf("%d",&n);
    int *a=(int*) calloc (n,sizeof(int));
    int brperm=0;
    int maks=0,ind=0;
    for (int i=0;i<n;i++)
    {
        scanf("%d",&(a[i]));
        brperm+=i;
        if (a[i]>=maks)
        {
            maks=a[i];
            ind=i;
        }
    }
    if (n==2) printf("%d %d", a[1],a[0]);
    else if (ind==1 && a[ind-1]==a[ind])
    {
        for (int i=0;i<n;i++) printf("%d ",a[i]);

    }
    else if (daLiJeSort(a,n)==1)
    {
        int k=0;
        for (int i=0;i<n-1;i++)
        {
            if (a[i]==a[i+1]) k=1;
        }
        if (k==0) swap(a[n-2],a[n-1]);
        for (int i=0;i<n;i++) printf("%d ",a[i]);
    }
    else
    {
        int br=0;
        while (ind==br)
        {
            int maks=0;
            for (int i=br+1;i<n;i++)
            {
                if (a[i]>=maks)
                {
                    maks=a[i];
                    ind=i;
                }
            }
            br++;
        }
        for (int i=0;i<n;i++)
        {
            if (a[i]<a[ind])
            {
                swap(a[i],a[ind]);
                break;
            }
        }
        for (int i=0;i<n;i++) printf("%d ", a[i]);
    }
}