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

namespace Polica_takmicenje
{
    class Program
    {
        static void Main(string[] args)
        {

            int n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n];
            int[,] m = new int[2,n];
            int p = 0;
            bool t = false;
            string[] tokens = Console.ReadLine().Split();
            for(int i = 0; i < n; i++)
            {
                a[i] = int.Parse(tokens[i]);
                m[0, i] = int.Parse(tokens[i]);
                m[1, i] = i;
            }
            for(int i = 0; i < n; i++)
            {
                for(int j = i + 1; j < n; j++)
                {
                    if (m[0, i] < m[0, j])
                    {
                        p = m[0, i];
                        m[0, i] = m[0, j];
                        m[0, j] = p;
                        p = m[1, i];
                        m[1, i] = m[1, j];
                        m[1, j] = p;
                    }
                }
            }
            //Ovde gore je sortiran niz i ovo gore radi 
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    if ((a[i] < m[0, j]) && (i < m[1,j]))
                    {
                        p = m[0,j];
                        a[m[1,j]] = a[i];
                        a[i] = p;
                        t = true;
                        break;
                    }
                    
                }
                if (t) break;
            }
            if (!t)
            {
                p = a[n-1];
                a[n-1] = a[n - 2];
                a[n - 2] = p;
            }
            for(int i = 0; i < n; i++)
            {
                Console.Write("{0} ", a[i]);
            }

            Console.ReadKey();
        }
        
    }
}