#include<bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int n,knjiga,maks,maksrev,pozadipoz,pr;
	cin>>n;
	pr=0;
	vector<int> pv;
	vector<int> kopijapv;
	vector<int> proverapv;
	vector<int> reversepv;

	for (int i=0; i<n; i++)
	{
		cin>>knjiga;
		kopijapv.push_back(knjiga);
		pv.push_back(knjiga);
		proverapv.push_back(knjiga);
		reversepv.insert(reversepv.begin(), knjiga); 

	}
	maks=*max_element(pv.begin(),pv.end());
	for (int i=0; i<n; i++)
	{
		if(maks==pv[i])
		{
		kopijapv.erase(kopijapv.begin());
		reversepv.pop_back();
		maks=*max_element(kopijapv.begin(),kopijapv.end());
		continue;
		}
		maksrev=*max_element(reversepv.begin(),reversepv.end());
		pozadipoz=distance(reversepv.begin() , max_element(reversepv.begin(),reversepv.end()));
		pv[n-1-pozadipoz]=pv[i];
		pv[i]=reversepv[pozadipoz];
		break;		
	}
	if (pv==proverapv)
	{
		for (int i=0;i<n;i++)
		{
			for(int j=0; j<n; j++)
			{
				if (pv[i]==proverapv[j] && i!=j)
				pr=1;
			}
		}
 	}
	if(pr==0)
	{
			pr=pv[n-1];
			pv[n-1]=pv[n-2];
			pv[n-2]=pr;
	}
	for (int i=0; i<n; i++)
	{
		cout<<pv[i];
	}
    //cout<<a;
    
    return 0; 
}