#include <bits/stdc++.h>
#include <climits>

#include <math.h>
using namespace std;
int cifre[10];
bool postoji_dupla(){
   bool b=false;
   int i=0;
   while(i<10&&!b){
    if(cifre[i]>1)b=true;
    i++;
   }
   return b;
}
void ispisi(string largest,int n){

        for(int i=0;i<n;i++){
            cout<<largest[i]<<" ";
        }
}
string largestNum(string num)
{
    int max_digit = -1;
    int max_digit_indx = -1;
    int l_indx = -1;
    int r_indx = -1;

    for (int i = num.size() - 1; i >= 0; i--) {
         cifre[num[i]-'0']++;
        if (num[i] > max_digit) {
            max_digit = num[i];
            max_digit_indx = i;
            continue;
        }

        if (num[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
        }
    }

    if (l_indx == -1)
        return num;

    swap(num[l_indx], num[r_indx]);

    return num;
}

// driver function
int main()
{
    memset(cifre,0,sizeof(cifre));
    string num ="",b;
    int n;
    cin>>n;

    for(int i=0;i<n;i++){
            cin>>b;
            num+=b;
    }
    if(n==2){
        cout<<num[1]<<" "<<num[0];
    }
    else{
      string largest= largestNum(num);
      if(largest!=num||n>10||postoji_dupla()){
       ispisi(largest,n);
      }
      else{
             char c=largest[n-1];
                largest[n-1]=largest[n-2];
                largest[n-2]=c;

        ispisi(largest,n);
      }
    }

    return 0;
}