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

namespace Zadatak_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int N;

            N = Convert.ToInt32(Console.ReadLine());

            string[] unos = new string[N];

            unos = Console.ReadLine().Split(' ');

            int[] niz = new int[N];
            int[] niz1 = new int[N];
            int temp;
            int max;
            int j = N;
            int indeks = 0;
            int indeks1 = 0;

            for (int i = 0; i < N; i++)
            {
                niz[i] = Convert.ToInt32(unos[i]);
            }

            for (int i = 0; i < N; i++)
            {
                niz1[i] = niz[i];
            }

            max = niz[0];

            for (int i = 1; i < N; i++)
            {
                if (max < niz[i])
                {
                    max = niz[i];
                    indeks = i;
                }
            }

            while (true)
            {
                if (max == niz[0])
                {
                    for (int i = 1; i < N; i++)
                    {
                        niz[i - 1] = niz[i];
                    }
                    N--;
                    indeks1++;
                }

                if (N == 0)
                {
                    break;
                }

                max = niz[0];

                for (int i = 0; i < N; i++)
                {
                    if (max < niz[i])
                    {
                        max = niz[i];
                        indeks = i;
                    }
                }

                if (max != niz[0])
                {
                    break;
                }
            }

            if (N == 0)
            {
                temp = niz1[j - 2];
                niz1[j - 2] = niz1[j - 1];
                niz1[j - 1] = temp;
            }

            else
            {
                temp = niz1[indeks1];
                niz1[indeks1] = niz1[indeks + indeks1];
                niz1[indeks + indeks1] = temp;
            }

            for (int i = 0; i < j - 1; i++)
            {
                Console.Write(niz1[i] + " ");
            }
            Console.Write(niz1[j - 1]);
        }
    }
}