#include <iostream>
#define MAX_N 1000000

using namespace std;

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

int main() {
	int i = 0, pom2=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] == max) br++;
			else if (A[i] > max) {
				max = A[i];
				br=1;
				pom = i;
			}
		}
		if (A[pom2] != max) swap(A[pom2], A[pom]);
		else if (A[pom2] == max && br < 2) {
			i = ++pom2;
			max = 0;
			goto label;
		}
	}
	for (int i = 0;i < N;i++) {
		cout << A[i] << " ";
	}
}