#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	long long n, maxo = 0, t, l = 0, r = 0; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) cin >> a[i];
	if(n>2){
		for (int i = 0; i < n; ++i) {
			if (a[i] < *max_element(a.begin()+i, a.end())) {
				l = max_element(a.begin()+i, a.end()) - a.begin();
				t = a[i];
				a[i] = *max_element(a.begin()+i, a.end());
				a[l] = t;
				break;
			}
		}
	}
	if (n == 2) {
		t = a[0];
		a[0] = a[1];
		a[1] = t;
	}
	vector<int> bla = a;
	sort(bla.begin(),bla.end(),greater<int>());
	if (bla == a) {
		t = a[n-1];
		a[n-1] = a[n - 2];
		a[n - 2] = t;
	}
	for (int i = 0; i < n; ++i) cout << a[i] << ' ';
	return 0;
}