#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ldb long double
#define mt make_tuple
const int N=1000050;
int a[N],n,ans[N],b[N];
void input()
{
	scanf("%i",&n);
	for(int i=1;i<=n;i++) scanf("%i",&a[i]);
}
void output()
{
	for(int i=1;i<=n;i++) printf("%i ",ans[i]);
	printf("\n");
}
void Solve()
{
	for(int i=1;i<=n;i++) b[i]=a[i];
	sort(b+1,b+1+n);
	reverse(b+1,b+1+n);
	int x=-1;
	for(int i=1;i<=n;i++) if(a[i]!=b[i]){ x=i;break;}
	if(x==-1)
	{
		for(int i=1;i<=n;i++) ans[i]=a[i];
		bool ok=0;
		for(int i=1;i<n;i++) if(a[i]==a[i+1]) ok=1;
		if(!ok) swap(ans[n],ans[n-1]);
	}
	else
	{
		for(int i=1;i<=n;i++) ans[i]=a[i];
		for(int i=n;i>=1;i--) if(a[i]==b[x]){ swap(ans[i],ans[x]);break;}
	}
}
void Run()
{
	input();
	Solve();
	output();
}
int bans[N],tmp[N];
bool cmp(int x[], int y[], int n)
{
	for(int i=1;i<=n;i++)
	{
		if(x[i]>y[i]) return 1;
		if(x[i]<y[i]) return 0;
	}
	return 0;
}
void BruteForce()
{
	for(int i=1;i<=n;i++) tmp[i]=a[i];
	bool fir=1;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)
		{
			swap(tmp[i],tmp[j]);
			if(fir || cmp(tmp,bans,n))
				for(int k=1;k<=n;k++) bans[k]=tmp[k];
			fir=0;
			swap(tmp[i],tmp[j]);
		}
}
void MakeTestCase()
{
	n=rand()%1000000+2;
	for(int i=1;i<=n;i++) a[i]=rand()%10;
}
bool Compare()
{
	for(int i=1;i<=n;i++) if(ans[i]!=bans[i]) return 0;
	return 1;
}
void Print()
{
	printf("%i\n",n);
	for(int i=1;i<=n;i++) printf("%i ",a[i]);printf("\n");
	printf("Solve:\n");for(int i=1;i<=n;i++) printf("%i ",ans[i]);printf("\n");
	printf("Brute:\n");for(int i=1;i<=n;i++) printf("%i ",bans[i]);printf("\n");
}
double GetTime(){ return (ldb)clock()/CLOCKS_PER_SEC;}
double s_time,e_time,d_time;
void Start(){ s_time=GetTime();}
void Stop(){ e_time=GetTime();d_time=e_time-s_time;}
void Test()
{
	srand(time(0));
	cout<<fixed<<setprecision(4);
	int work=100000;
	double max_time=0;
	bool speed_only=1;
	for(int i=1;i<=work;i++)
	{
		MakeTestCase();
		if(!speed_only) BruteForce();
		Start();
		Solve();
		Stop();
		if(!speed_only && !Compare())
		{
			Print();
			return;
		}
		max_time=max(max_time,d_time);
		cout<<i<<":OK, max time so far="<<max_time<<"\n";
	}
	cout<<":D\n";
}
int main()
{
	Run();
	return 0;
}