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

bool double_digit(int digits[]){
	for(int i = 9; i >= 0; i--){
		if(digits[i] > 1) return true;
	}
	return false;
}

void show(int a[], int n){
	for(int i = 0; i < n; i++) cout << a[i] << " ";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	long long n, p;
	int digits[10] = {}, temp;
	bool found = false, nonincreasing = true;
	cin >> n;
	int a[n], ideal[n]={};

	cin >> a[0];
	digits[a[0]]++;

	for(long long i = 1; i < n; i++){
		cin >> a[i];
		if(nonincreasing && a[i] > a[i-1]) nonincreasing = false;
		digits[a[i]]++;
	}

	if(nonincreasing){
		if(double_digit(digits) == false){
			temp = a[n-1];
			a[n-1] = a[n-2];
			a[n-2] = temp;			
		}
		
		show(a,n);
		return 0;
	}

	p = 0;
	for(int i = 9; i >= 0; i--){	
		for(int j = digits[i]; j > 0; j--){
			ideal[p++] = i;
		}
	}

	for(long long i = 0; i < n; i++){
		if(a[i] != ideal[i]){
			long long pmax = i;
			for(long long j = i+1; j < n; j++){
				if(a[j] >= a[pmax]){
					pmax = j;
				}
			}
			int max = a[pmax];
			a[pmax] = a[i];
			a[i] = max;
			show(a, n);
			return 0;
		}
	}

	return 0;
}