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

	for (long 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 (long 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 (long int i=0;i<n;i++)
		{
			for(long 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 (long int i=0; i<n; i++)
	{
		cout<<pv[i];
	}  
    //cout<<a;
    
    return 0; 
}