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

namespace ConsoleApplication1
{
    class Program
    {
        static int partition(int []arr, int l, int h) 
        { 
            int pivot = arr[h];  
            int i = (l - 1);  
            for (int j = l; j < h; j++) 
            { 
                if (arr[j] < pivot) 
                { 
                    i++; 
                    int temp = arr[i]; 
                    arr[i] = arr[j]; 
                    arr[j] = temp; 
                } 
            } 
            int temp1 = arr[i+1]; 
            arr[i+1] = arr[h]; 
            arr[h] = temp1; 
  
            return i+1; 
        } 
  
    static void quickSort(int []arr, int l, int h) 
    { 
        if (l < h) 
        { 
            int pp = partition(arr, l, h); 
            quickSort(arr, l, pp-1); 
            quickSort(arr, pp+1, h); 
        } 
    } 
   
        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);
            quickSort(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;
                }
            }
            int gl = 0;
            int klk = 0;
            int ppp = 0;
            if (f == false)
            {
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (i == arr[j])
                            klk++;
                    }
                    if (klk > 1)
                        ppp++;
                    klk = 0;
                }
                if (ppp == 0)
                {
                    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);
        }
    }
}