#include <stdio.h>

int main()
{
    long long n;
    scanf("%lld",&n);
    long long a[n];
    for (long long i=0;i<n;i++) scanf("%lld",&a[i]);
    long long b[10];
    for (long long i=0;i<=9;i++) b[i]=-1;
    for (long long i=n-1;i>=0;i--)
    {
        if (b[a[i]]==-1)
        {
            b[a[i]]=i;
        }
    }
    int r;
    for (long long i=0;i<n;i++)
    {
        for (long long j=9;j>a[i];j--)
        {
            if (b[j]>i)
            {
                long long c=a[b[j]];
                a[b[j]]=a[i];
                a[i]=c;
                r=1;
                break;
            }
        }
        if (r==1) break;
    }
    if (r==1)
    {
        for (long long i=0;i<n;i++) printf("%lld ",a[i]);
        return 0;
    }
    for (long long i=0;i<n;i++)
        if (a[i]==a[i+1])
        {
            for (long long i=0;i<n;i++) printf("%lld ",a[i]);
            return 0;
        }
    for (long long i=0;i<n-2;i++) printf("%lld ",a[i]);
    printf("%lld %lld",a[n-1],a[n-2]);
    return 0;
}