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

namespace _1_Pikado
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine());
            string s1 = Console.ReadLine();
            string[] s = s1.Split(' ');
            if (s.Length > 2)
            {
                int max = Convert.ToInt32(0);
                for (int i = 0; i < s.Length; i++)
                {
                    if (max < Convert.ToInt32(s[i]))
                    {
                        max = Convert.ToInt32(s[i]);
                    }
                }
                int k = Convert.ToInt32(0);
                while (max != Convert.ToInt32(s[k]))
                {
                    k++;
                }
                if (k != 0)
                {
                    int e = Convert.ToInt32(s.Length - 1);
                    while (Convert.ToInt32(s[e]) != max)
                    {
                        e--;
                    }

                    string hh = s[0];
                    s[0] = s[e];
                    s[e] = hh;
                }
                else
                {
                    bool ok = false;
                    int e = Convert.ToInt32(s.Length - 1);
                    while (ok == false)
                    {
                        k++;
                        if ((max != Convert.ToInt32(s[k])) || (k == s.Length - 1))
                        {
                            ok = true;
                        }
                    }
                    max = Convert.ToInt32(0);
                    for (int i = k; i < s.Length; i++)
                    {
                        if (max < Convert.ToInt32(s[i]))
                        {
                            max = Convert.ToInt32(s[i]);
                        }
                    }
                    while (Convert.ToInt32(s[e]) != max)
                    {
                        e--;
                    }

                    string hh = s[k];
                    s[k] = s[e];
                    s[e] = hh;
                }

                for (int i = 0; i < s.Length; i++)
                {
                    Console.Write(s[i] + " ");
                }
            }
            else
            {
                Console.Write(s[1] + " ");
                Console.Write(s[0] + " ");
            }
        }
    }
}