#include <iostream>
#include <algorithm>
using namespace std;

int main()
{

long n, help, hilf = 0, f = 0, d = 0;
  cin >> n;
  int a[n];
  int b[n];

  for (long i = 0; i < n; i++)
    {
      cin >> a[i];
      b[i] = a[i];
    }
   
   
  sort (b, b + n);

  for (long i = n - 1; i >= 0; i--)
        {  
            if (b[i] > a[n - i - 1])
            {
                for (long j = n - 1; j >= 0; j--)
                {
                    if (a[j] == b[i])
                    {
                       help = a[n - i - 1];
                       a[n - i - 1] = a[j];
                       a[j] = help;
                       break;
                    }
                } 
                break;
            }
           
        }
       
  f = 0;
  for (long i = 0; i < n; i++)
    {
        if (a[i] == b[n - i - 1])
   {
       f++;
   }
    }
   
  if (f == n)
    {
        for (long j = 0; j < n; j++)
   {
       if (a[j] == a[j + 1])
       {
           d = 1;
       }
   }
   if (d != 1)
            {
                hilf = a[n - 1];
                a[n - 1] = a[n - 2];
                a[n - 2] = hilf;
            }
           
    }
for (long i = 0; i < n; i++)
           {
               cout << a[i] << " ";
           }
return 0;
}