#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; bool dupli=false;
    cin >> n;

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
        num[arr[i]]++;
        if(num[arr[i]]>=2) dupli=true;
        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;
        }
    }
    //cout<<"F mayor\n";
    if(!dupli)swap(arr[n-1],arr[n-2]);
    for(int i=0;i<n;i++) cout<<arr[i]<<" ";
    return cout<<endl,0;
}