#include <iostream>
#define MAX_N 1000000

using namespace std;

int N,br=0,pom;
short int A[MAX_N], maks = 0;

int main() {
	int i = 0, pom2=0,pom3=0, joker=0;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> N;
	for (int i = 0;i < N;i++) {
		cin >> A[i];
	}
	if (N == 2) swap(A[0], A[1]);
	else {
		label:
		for (i;i < N;i++) {
			if (A[i] == maks) { br++; if (br > joker) joker = br; }
			else if (A[i] > maks) {
				maks = A[i];
				br=1;
				pom = i;
			}
		}
		if (A[pom2] != maks) swap(A[pom2], A[pom]);
		else if (A[pom2] == maks) {
			{
				i = ++pom2;
				if (N - pom2 <= 2) {
					if(A[N-1] > A[N-2] || joker < 2)
					swap(A[N - 2], A[N - 1]);
				}
				else {
					maks = 0;
					goto label;
				}
			}
		}
	}
	for (int i = 0;i < N;i++) {
		cout << A[i] << " ";
	}
}