//Modifikovan https://www.geeksforgeeks.org/form-largest-number-using-one-swap-operation/ algoritam

#include <bits/stdc++.h> 
using namespace std; 
  
string largestNumber(string num) 
{ 
    int n = num.size(); 
    int rightMax[n], right; 
  
    rightMax[n - 1] = -1; 
    right = n - 1; 
  
    for (int i = n - 2; i >= 0; i--) { 
  
        if (num[i] < num[right]) 
            rightMax[i] = right; 

        else { 
            rightMax[i] = -1; 
            right = i; 
        } 
    } 
  
    for (int i = 0; i < n; i++) { 
        if (rightMax[i] != -1) { 
            swap(num[i], num[rightMax[i]]); 
            return num; 
        } 
    } 
  
    swap(num[n-1], num[n-2]);
    return num; 
} 
  
int main() 
{ 
	int n;
	char x;
	string num;
	cin >> n;
	
	for (int i = 0; i < n; ++i) {
		cin >> x;
		num += x;
	}
	
	num = largestNumber(num);
	
	for (int i = 0; i < num.length(); ++i) {
		cout << num[i] << " ";
	}
}