using System;

namespace prog
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] input = Console.ReadLine().Split(' ');

            int L = -1, Ll = -1, Lr = -1;
            for (int i = N - 1; i >= 0; i--)
            {
                if (L == -1 || input[i][0] > input[L][0])
                    L = i;
                else if (input[i][0] <= input[L][0])
                {
                    Ll = i;
                    Lr = L;
                }
            }

            if (Ll != -1)
            {
                string tmp = input[Lr];
                input[Lr] = input[Ll];
                input[Ll] = tmp;
            }
            else
            {
                string tmp = input[N - 1];
                input[N - 1] = input[N - 2];
                input[N - 2] = tmp;
            }

            for (int i = 0; i < N; i++)
                Console.Write(input[i] + " ");
            Console.WriteLine();
        }
    }
}