#include <bits/stdc++.h>
using namespace std;
vector<int> orgulaz, res;
deque <int> ulaz;
int pozmaks()
{
	int trmaks=ulaz[0],pozmakss=0,velicina=ulaz.size();
	for(int brojac=1;brojac<velicina;brojac++)
	{
		if (ulaz[brojac]>=trmaks)
		{
			trmaks=ulaz[brojac];
			pozmakss=brojac;
		}
	}
	return pozmakss;
}
bool dvaista(vector<int> x)
{
	bool p;
	int duzz=x.size();
	for (int h=1;h<duzz;h++)
	{
		if (x[h]==x[h-1]) return true;
	}
	return false;
}
int main() {
	int n,maks=0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		int pom;
		cin >> pom;
		ulaz.push_back(pom);
		orgulaz.push_back(pom);
		if (pom>maks) maks=pom;
	}
	for (int i=0;i<n;i++)
	{
		if (orgulaz[i]==ulaz[pozmaks()])
		{
			res.push_back(orgulaz[i]);
			ulaz.pop_front();
		}
		else
		{
			int duzina=ulaz.size() ,poz=pozmaks();
			res.push_back(ulaz[poz]);
			for (int u=1;u<duzina;u++)
			{
				if (u==poz) res.push_back(orgulaz[i]);
				else res.push_back(ulaz[u]);
			}
			break;
		}
	}
	int poredi=0;
	for (int j=0;j<n;j++)
	{
		if(orgulaz[j]!=res[j]) poredi++;
	}
	if (poredi > 0)
	{
		for (int i=0;i<n;i++)
		cout << res[i] << " ";
	}
	else if (dvaista(orgulaz))
	{
		for (int i=0;i<n;i++)
		cout << res[i] << " ";
	}
	else {
		for (int i=0;i<n-2;i++)
		{
		cout << res[i] << " ";
		}
		cout << res[n-1] << " " << res[n-2];
	}
	return 0;
}