#include <iostream>

using namespace std;


int main()	{
	long *f = new long[(long)1e6];
	long N, max=-1, maxi=-1, mini=-1, sv=-1;
	
	cin>>N;
	
	for(long i=0; i<N; i++)
		cin>>f[i];
		
	for(long i=N-1; i>=0; i--){
		if(f[i]>max){
			maxi=i;
			max=f[i];
		}
		else if (f[i]<max){
			mini=i;
			sv=maxi;
		}
	}
	
	if(mini==-1){
		mini=N-1;
		sv=N-2;
	}	
	
	swap(f[sv], f[mini]);
		
	for(long i=0; i<N-1; i++)
		cout<<f[i]<<' ';
	cout<<f[N-1];
	
	delete [] f;
	return 0;
}