#include <iostream>

using namespace std;

int main()
{
    int n;
    int last[10];
    cin >> n;
    int a[n+1];
    for(int i=0;i<10;i++)
        last[i]=-1;
    for(int i=0;i<n;++i)
    {
        cin >> a[i];
        last[a[i]]=i;
    }
    bool changeHappened=false;
    bool thereAreTwoSame=false;
    for(int i=0;i<n;++i) //changing the two digits to make the highest number if possible
    {
        if(a[i]<10)
        {
            for(int j=9;j>0;--j)
            if(last[j]!=-1 && last[j]>i && a[i]<j)
            {
                int temp = a[i];
                a[i]=a[last[j]];
                a[last[j]]=temp;
                changeHappened = true;
                break;
            }
        }
    }

    for(int i=0;i<n;++i) //need to check if there are two of the same digit so I can change those two making the number the same instead of making it lower
        for(int j=i+1;j<n;++j)
            if(a[i]==a[j])
            {
                thereAreTwoSame=true;
                break;
            }

    if(!changeHappened) //if the change has not been yet made, either leaving the number as is (changing two same numbers) or changing the last two to do as little "damage" to the number
    {
        if(!thereAreTwoSame)
        {
            int temp=a[n-1];
            a[n-1]=a[n-2];
            a[n-2]=temp;
        }
    }
    for(int i=0;i<n;++i)
        cout << a[i] << " ";
    return 0;
}