#include <iostream>

using namespace std;

int n, niz[1000000];
//c1
int a, b;
//c2
int maxb, maxbl;
//fin
int ch[10];
int main(){
	cin >> n;
	//case 1
	if(n == 2){
		cin >> a >> b;
		cout << b << " " << a << "\n";
		return 0;
	}
	//case 2
	else{
		for (int i = 1; i <= n; i++){
			cin >> niz[i];
		}
		//prvi
		for(int x = 1; x <= n; x++){
			maxb = niz[x];
			if(maxb < 9){
				for(int y = n; y >= x; y--){
					if(niz[y] > maxb){
						maxb = niz[y];
						maxbl = y;
					}
				}
				if(maxb > niz[x]){
					niz[maxbl] = niz[x];
					niz[x] = maxb;
					for (int i = 1; i <= n; i++) cout << niz[i] << " ";
					return 0;
				}
			}
		}
	}
	if(n < 10){
		for(int i = 1; i <= n; i++) ch[niz[i]]++;
		for(int i = 1; i <= 10; i++) 
			if(ch[i] >= 2){
				for(int i = 1; i <= n; i++) cout << niz[i] << " ";
				return 0;
			}
			
		for(int i = 1; i <= n-2; i++) cout << niz[i] << " ";
		cout << niz[n] << " " << niz[n-1] << endl;
		return 0;
	}
	else{
		for(int i = 1; i <= n; i++) cout << niz[i] << " ";
		return 0;
	}
	
}