using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            long[] polica=new long[N];
            string[] niz = Console.ReadLine().Split();
            for (int i = 0; i < N; i++)
                polica[i] = long.Parse(niz[i]);
            if (polica.Length == 2)
            {
                Console.WriteLine(polica[1] + " " + polica[0]);
            }
            else
            {
                long max = 0;
                int poz = 0;
                for (int i = 0; i < polica.Length; i++)
                {
                    if (polica[i] >= max)
                    {
                        max = polica[i];
                        poz = i;
                    }
                }
                if (max != polica[0])
                {
                    long copy = polica[poz];
                    polica[poz] = polica[0];
                    polica[0] = copy;
                    Ispis(polica);
                }
                else
                {
                    if (polica[1] == max)
                    {
                        if (opadajuce(polica))
                            Ispis(polica);
                        else
                        {
                            for (int i = 0; i < niz.Length - 1; i++)
                            {
                                if (polica[i] < polica[i + 1])
                                {
                                    max = 0;
                                    for (int j = i; j < polica.Length; j++)
                                    {
                                        if (polica[j] >= max)
                                        {
                                            max = polica[j];
                                            poz = j;
                                        }
                                    }
                                    long copy = polica[poz];
                                    polica[poz] = polica[i];
                                    polica[i] = copy;
                                    Ispis(polica);
                                    break;
                                }
                            }
                        }
                    }
                    else if (poz >= 1)
                    {
                        long copy = polica[poz];
                        polica[poz] = polica[1];
                        polica[1] = copy;
                        Ispis(polica);
                    }
                    else
                    {
                        max = 0;
                        for (int i = 1; i < polica.Length; i++)
                        {
                            if (polica[i] >= max)
                            {
                                max = polica[i];
                                poz = i;
                            }
                        }
                        long copy = polica[poz];
                        polica[poz] = polica[1];
                        polica[1] = copy;
                        Ispis(polica);
                    }
                }
            } 
        }
        static void Ispis(long[] mat)
        {
            foreach (var item in mat)
                Console.Write(item+" ");
            Console.WriteLine();
        }
        static bool opadajuce(long[] niz)
        {
            for (int i = 0; i < niz.Length-1; i++)
            {
                if (niz[i] < niz[i + 1])
                    return false;
            }
            return true;
        }
    }
}