#include <bits/stdc++.h> 
#include <sstream>
using namespace std; 

int largestNum(int num) 
{ 
    int max_digit = -1; 
    int max_digit_indx = -1; 
    int l_indx = -1; 
    int r_indx = -1; 
    string num_in_str = to_string(num); 
    for (int i = num_in_str.size() - 1; i >= 0; i--) { 
        if (num_in_str[i] > max_digit) { 
            max_digit = num_in_str[i]; 
            max_digit_indx = i; 
            continue; 
        } 
        if (num_in_str[i] < max_digit) { 
            l_indx = i; 
            r_indx = max_digit_indx; 
        } 
    } 
    if (l_indx == -1) 
    return num; 
    swap(num_in_str[l_indx], num_in_str[r_indx]); 
    return stoi(num_in_str); 
} 
int main() 
{ 
int n,br=0;
cin>>n;
int niz[100000];
for(int i=0;i<n;i++)
{
cin>>niz[i];
br+=(niz[i]*pow(10,n-i-1));
}
int r=largestNum(br);
int dal=0;
if(r==br)
dal=1;
int rez[100000];
for(int i=0;i<n;i++)
{
rez[i]=r%10;
r-=r%10;
r/=10;
}
if(dal==1)
swap(rez[0],rez[1]);
for(int i=n-1;i>=0;i--)
cout<<rez[i]<<" ";
cout<<endl;
return 0; 
}