#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
main(){
	int n,mark1,mark2,l=0,temp;
	cin>>n;
	int a[n];
	int b[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n,greater<int>());
	for(int c=0;c<n;c++){
		if(b[c]!=a[c]){
			mark1=c;
			break;
			
		}
	}
	for(int u=mark1+1;u<n;u++){
		if(l<a[u]){
			l=a[u];
			mark2=u;
		}
	}
	//cout<<mark1<<" "<<mark2<<endl;
	temp=a[mark1];
	a[mark1]=a[mark2];
	a[mark2]=temp;
	for(int g=0;g<n;g++){
		cout<<a[g]<<" ";
	}
}