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

namespace ConsoleApplication1
{
    class Program
    {
        private static void Quick_Sort(int[] arr, int left, int right)
        {
            if (left < right)
            {
                int piv = Partition(arr, left, right);

                if (piv > 1)
                {
                    Quick_Sort(arr, left, piv - 1);
                }
                if (piv + 1 < right)
                {
                    Quick_Sort(arr, piv + 1, right);
                }
            }

        }

        private static int Partition(int[] arr, int left, int right)
        {
            int piv = arr[left];
            while (true)
            {

                while (arr[left] < piv)
                {
                    left++;
                }

                while (arr[right] > piv)
                {
                    right--;
                }

                if (left < right)
                {
                    if (arr[left] == arr[right]) return right;

                    int temp = arr[left];
                    arr[left] = arr[right];
                    arr[right] = temp;


                }
                else
                {
                    return right;
                }
            }
        }
        static void ispis(int[] arr, int n)
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write(arr[i] + " ");
            }
        }
        static void clanovi(int [] arr1,int []arr2,int n)
        {
            for (int i = 0; i < n; i++)
            {
                arr1[i] = arr2[i];
            }
            
        }
        static void Main(string[] args)
        {
            int n=Convert.ToInt32(Console.ReadLine());
            string[] fr = Console.ReadLine().Split();
            int [] arr=new int[n];
            int [] fr1=new int[n];
            for(int i=0;i<n;i++)
            {
                fr1[i]=int.Parse(fr[i]);
            }
            clanovi(arr, fr1, n);
            Quick_Sort(fr1, 0, n-1);
            int k1=0;
            int k2=0;
            int ind1=0;
            bool f = false;
            for (int i = 0; i < n; i++)
            {
                if (f)
                    continue;
                else if (arr[i] != fr1[n-1-i])
                {
                    k1 = arr[i];
                    ind1=i;
                    k2 = fr1[n-i-1];
                    f = true;
                }
            }
            if (f == false)
            {
                int temp = arr[n-1];
                arr[n-1] = arr[n-2];
                arr[n-2] = temp;
            }
            int ind = 0;
            for (int i = 0; i < n; i++)
            {
                if (arr[i] == k2)
                    ind = i;
            }
            if (f)
            {
                int tempp = arr[ind1];
                arr[ind1] = arr[ind];
                arr[ind] = tempp;
            }
            ispis(arr, n);
            Console.ReadKey();
        }
    }
}