#include <bits/stdc++.h>

using namespace std;

int niz[1000000];
pair<int, int> maksniz[1000000];

int main ()
{
	int n;
	cin >> n;
	srand(time(NULL));
	for(int i=0;i<n;i++)
	{
		cin >> niz[i];

	}
	int maks = -1;
	int maxpos;
	for(int i=n-1;i>=0;i--)
	{
		maksniz[i].first = maks;
		maksniz[i].second = maxpos;
		if(niz[i] > maks)
		{
			maks = niz[i];
			maxpos = i;
		}
	}
	bool lololol = true;
	for(int i=0;i<n;i++)
	{
		if(niz[i] < maksniz[i].first)
		{
			swap(niz[i], niz[maksniz[i].second]);
			lololol = false;
			break;
		}
	}
	if(lololol)
	{
		swap(niz[n-1], niz[n-2]);
	}
	for(int i=0;i<n;i++)
	{
		cout << niz[i] << " ";
	}
}