#include <bits/stdc++.h>
#define ll long long
using namespace std;
string s;
void ispisi() {
    for (ll i = 0 ; i < s.length() ; i++ ) cout << s[i] << ' ';
}
int main () {
	ios_base::sync_with_stdio(false);
	ll n ;
	cin >> n ;
	char c ;
	string s2 ;
	for (ll i = 0 ; i < n ; i++ ) {
	    cin >> c ;
	    s.push_back(c);
	}
	
	s2 = s ;
	sort(s2.begin(),s2.end(),greater<int>());
	if ( s == s2 ) {
	    swap(s[s.length()-1],s[s.length()-2]);
	    ispisi() ;
	    return 0 ;
	}
	
	for (ll i = 0 ; i < s.length() ; i++ ) {
	    if ( s[i] != s2[i] ) {
	        for (ll j = s.length()-1 ; j > i ; j-- ) {
	            if ( s[j] == s2[i] ) {
	                swap(s[j],s[i]);
	                break;
	            }
	        }
	        break;
	    }
	}
	
	ispisi();
    return 0 ;

}