#include <bits/stdc++.h>

using namespace std;

int n, arr[1000010], maxArr[1000010], poz, br[15];

int main()
{

    cin>>n;

    for (int i=0; i<n; i++)
    {
        cin>>arr[i];
        br[arr[i]]++;
    }

    int k=0;
    for (int i=9; i>=0; i--)
    {
        for (int j=0; j<br[i]; j++)
            maxArr[k++]=i;
    }

    for (int i=0; i<n; i++) cout<<maxArr[i]<<" ";
    cout<<endl;

    bool t=false;
    for (int i=0; i<n; i++)
    {
        if (arr[i]==maxArr[i]) continue;
        /*else*/
        t=true;
        poz=i;
        break;
    }

    //cout<<"DA";
    if (!t)
    {
        for (int i=9; i>=0; i--)
            if (br[i]>=2) t=true;
        if (!t)
            swap(arr[n-2], arr[n-1]);
    }
    else
    {
        //cout<<"DA:";
        int pozSwap;
        for (int i=n-1; i>=0; i--)
        {
            //cout<<i<<" "<<arr[i]<<endl;
            if (arr[i]==maxArr[poz])
            {
                pozSwap=i;
                break;
            }
        }
        //cout<<maxArr[poz]<<" "<<poz<<" "<<pozSwap<<endl;
        swap(arr[poz], arr[pozSwap]);
    }
    for (int i=0; i<n; i++)
        cout<<arr[i]<<" ";
}

/*
10
9 9 1 8 7 2 3 9 7 1
*/