#include<stdio.h>
using namespace std;

int qq[1000005],n;
int i,pom1,pom2,maxx,promena,t;

int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&qq[i]);
			
		
	pom2=n; pom1=n-1; 
	if (qq[pom1]>qq[pom2]) {maxx=n-1; promena=-1;}
	if (qq[pom1]<qq[pom2]) {maxx=n; promena=1;}
	if (qq[pom1]==qq[pom2]) {maxx=n; promena=0;}
	for (i=n-2;i>=1;i--)
	{
		if (qq[i]<qq[maxx]) {promena=1; pom2=maxx; pom1=i; continue;}
		if (qq[i]>qq[maxx]) {maxx=i; continue;}
		if (promena==1) continue;
		if (promena==0) continue;
		pom1=i;
	}
	
	t=qq[pom1];
	qq[pom1]=qq[pom2];
	qq[pom2]=t;
	
	for (i=1;i<=n;i++)
		if (i!=n) printf("%d ",qq[i]); else printf("%d",qq[i]); 
}