#include <iostream>
#include <vector>
using namespace std;

void swap(int &x, int &y) {
	int pom = x;
	x = y;
	y = pom;
}

int main()
{
	int n;
	cin >> n;
	vector<int> nums(n);
	for (int i = 0; i < n; i++)
	{
		cin >> nums[i];
	}
	
	int max = n - 1;
	int li = -1, ri = -1;

	for (int i = n - 1; i >= 0; i--) {
		if (nums[i] > nums[max]) {
			max = i;
			continue;
		}

		if (nums[i] < nums[max]) {
			li = i;
			ri = max;
		}			
	}

	if (n == 2) swap(nums[0], nums[1]);
	else if (li != -1) swap(nums[li], nums[ri]);
	
	for (auto x : nums) {
		cout << x << " ";
	}
	cout << endl;
}