#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int N = 1e6;
int arr[N] = {};
int num[11]={};
int pos[11]={};

int main()
{
    int n, maxDig = 0, maxDigID = 0;
    cin >> n;

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
        num[arr[i]]++;
        pos[arr[i]] = i;
    }
    for(int i=0;i<n;i++)
    {
        int id=9;
        num[arr[i]]--;
        while(id>arr[i] and num[id]==0) id--;
        if(num[id] != 0 and id>arr[i])
        {
            swap(arr[i],arr[pos[id]]);
            for(int i=0;i<n;i++) cout<<arr[i]<<" ";
            return cout<<endl,0;
        }
    }
    swap(arr[n-1],arr[n-2]);
    for(int i=0;i<n;i++) cout<<arr[i]<<" ";
    return cout<<endl,0;
}