using System;

namespace polica
{
    class Program
    {     
        static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine());
            string s = Console.ReadLine();
            string[] s1 = s.Split(' ');
            int[] a = new int[N];
            for (int i = 0; i < N; i++)
            {
                int p = Convert.ToInt32(s1[i]);
                int j = i;
                while (j > 0 && p > a[j - 1])
                {
                    a[j] = a[j - 1];
                    j = j - 1;
                }
                a[j] = p;
            }
            int max = a[0];
            bool d = true;
            int br = 0;
            for(int i = 0; i < N && d; i++)
            {
                max = a[i];
                if (Convert.ToInt32(s1[i]) != max)
                {
                    int j = i + 1;
                    bool c = true;
                    while (j < N && c)
                    {
                        if (Convert.ToInt32(s1[j]) == max)
                        {
                            c = false;
                        }
                    }
                    string p = s1[i];
                    s1[i] = s1[j];
                    s1[j] = p;
                    d = false;
                    br++;
                }
                else
                {
                    if (br == 0 && i < N - 1 && Convert.ToInt32(s1[i + 1]) == max)
                    {
                        string p = s1[i];
                        s1[i] = s1[i + 1];
                        s1[i + 1] = p;
                        br++;
                        d = false;
                    }
                }
            }
            if (br == 0)
            {
                string p = s1[N - 1];
                s1[N - 1] = s1[N - 2];
                s1[N - 2] = p;
            }
            s = "";
            for(int i = 0; i < N; i++)
            {
                s += s1[i];
                s += " ";
            }
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}