#include <bits/stdc++.h>

using namespace std;

void Swap(int a[], int left, int right)
{
    int temp = a[left];
    a[left] = a[right];
    a[right] = temp;
}

void Difference(int a[], int b[], int n)
{
    bool swapTrue = false;
    for(int k = 0; k < n; k++)
    {
        int left, right;
        if (a[k] != b[k])
        {
            swapTrue = true;
            left = k;
            for(int j = n; j > 0; --j)
            {
                if (a[j] == b[k])
                {
                    right = j;
                    break;
                }
            }
            Swap(a, left, right);
            break;
        }
    }
    if (swapTrue == false)
    {
        bool sameTrue = false;
        for (int i = 0; i < n-1; i++)
    {
        if (a[i] == a[i+1])
        {
            sameTrue = true;
            break;
        }
    }
    if (sameTrue == false)
        Swap(a, n-2, n-1);
    }
}

int main()
{
    int n;
    cin >> n;
    int a[n], b[n];

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

    Difference(a, b, n);
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";

    return 0;
}