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

int main ()
{

    long N;
    scanf ("%ld", &N);
    int A[N];
    for (long i=0; i<N; i++)
        scanf ("%d", &A[i]);
    long p[10];
    for(int i=0; i<10; i++)
        p[i] = 0;
    long x = -1, y = -1;

    for (long i=0; i<N; i++)
        if (!p[A[i]])
        {
            for (long j=i+1; j<N; j++)
                if (A[j]>A[i])
                {
                    x = i;
                    y = j;
                    break;
                }
            if (x>-1)
                break;
            p[A[i]] = 1;
        }

    if (x>-1)
    {
        int t = A[x];
        A[x] = A[y];
        A[y] = t;
    }
    else if (N<=10)
    {
        for(int i=0; i<10; i++)
        p[i] = 0;
        for (long i=0; i<N; i++)
            p[A[i]]++;
        int found = 0;
        for(int i=0; i<10; i++)
            if (p[i]>1)
            {
                found = 1;
                break;
            }
        if (!found)
        {
            int t = A[N-2];
            A[N-2] = A[N-1];
            A[N-1] = t;
        }
    }

    for (long i=0; i<N; i++)
        printf ("%d ", A[i]);

    return 0;
}