#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    long long m=0,n,a,i1=0,i2=0,prvi,drugi,max1,imax;
    bool p=false;
    cin>>n;
    while(n>0)
    {
        cin>>a;
        m=m*10+a;
        n--;
        i1++;
    }
    i1--;
    long long i3=i1;
    while(i1>=0)
    {
        i2=i1-1;
        prvi=m/(long long)pow(10,i1);
        while(i2>=0)
        {
            drugi=m/(long long)pow(10,i2)%10;
            if(prvi<=drugi)
             {
                 p=true;
                 max1=drugi;
                 imax=i2;
             }
                i2--;
        }
        if(p)
        {
            i2=imax;
            drugi=max1;
            m=(m/(long long)pow(10,i1+1)*(long long)pow(10,i1+1)+m%(long long)pow(10,i1)+drugi*(long long)pow(10,i1))/(long long)pow(10,i2+1)*(long long)pow(10,i2+1)+(m/(long long)pow(10,i1+1)*(long long)pow(10,i1+1)+m%(long long)pow(10,i1)+drugi*(long long)pow(10,i1))%(long long)pow(10,i2)+prvi*(long long)pow(10,i2);
            break;
        }
        i1--;
    }
    if(!p)
    {
        prvi=m%10;
        drugi=m/10%10;
        m=m/100*100+prvi*10+drugi;
    }
    while(i3>=0)
    {
        cout<<m/(long long)pow(10,i3)%10<<" ";
        i3--;
    }
    return 0;
}