#include <iostream>
#define NMAX 1000000
using namespace std;

int N, a[NMAX];

main(){	

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin >> N;
	cin >> a[0];
	int max = a[0]; int maxi = 0;
	//int min = a[0]; int mini = 0;
	
	for (int i=1; i<N; i++) {
		cin >> a[i];
		if (a[i] > max) {
			max = a[i];
			maxi = i;
		}
		/*if (a[i]<min) {
			min = a[i];
			mini = i;
		}*/
	}
		
		
	if (N==2) cout << a[1] << " " << a[0];
	else {
		if (a[0] != max) {
			a[maxi] = a[0];
			a[0] = max;
		}
		else {
			int ind = 1, p, t, m, pi, ti, mi;
			for (int i=0; i<N-1; i++)
				if (a[i]<a[i+1]) {
					ind = 0;
					p = a[i-1]; pi = i-1;
					t = a[i]; ti = i;
					break;
				}
			if (ind == 1) {
				int pom = a[N-1];
				a[N-1] = a[N-2];
				a[N-2] = pom;
			}
			else {
				m = a[ti+1]; mi = ti+1;
				for (int i=ti+2; i<N; i++)
					if (a[i]>m) {
						m = a[i];
						mi = i;
					}
				if (m>p) {
					a[pi] = m;
					a[mi] = p;
				}
				else {
					a[ti] = m;
					a[mi] = t;
				}
			}
		}
		for (int i=0; i<N; i++)
		cout << a[i] << " ";
	}
}