using System;
class Program
{
    static void Main(string[] args)
    {
        int N = int.Parse(Console.ReadLine());
        string[] c = Console.ReadLine().Split(' ');
        int[] a = new int[N];
        for (int i = 0; i < N; i++) a[i] = int.Parse(c[i]);
        int index = -1, maxval = 0, index1 = 0;
        bool shouldrepeat = true;
        for (int i = 0; i < N && shouldrepeat; i++)
        {
            if (shouldrepeat)
            {
                maxval = 0;
                index = -1;
                for (int j = i; j < N; j++)
                {
                    if (i != j)
                    {
                        if (a[j] >= a[i])
                        {
                            index = j;
                            maxval = a[j];
                            if (a[j] > a[i]) shouldrepeat = false;
                        }
                    }
                }
            }
            index1 = i;
        }
        if(index == -1)
        {
            index = N - 2;
            index1 = N - 1;
        }
        int x = a[index];
        a[index] = a[index1];
        a[index1] = x;
        for (int i = 0; i < N; i++)
        {
            Console.Write(a[i] + " ");
        }
    }
}