#include <bits/stdc++.h>
using namespace std;

#define long int64_t
#define endl '\n'

#ifdef OFFLINE
	#define logan(...) logg(#__VA_ARGS__, __VA_ARGS__)
	template<class Arg>
	void logg(const char* name, Arg&& arg){ cerr << name << ": " << arg << '\n'; }
	template<class Arg, class ... Args>
	void logg(const char* name, Arg&& arg, Args&& ... args){
		char *next = (char*)name, *cand;
		int depth = 0;
		do{
			if(depth){ next = strchr(next+1, ')'); --depth; }
			else if((cand = strchr(next+1, '('))){ next = cand; ++depth; }
			else{ next = strchr(next+1, ','); }
		}while(depth);
		cerr.write(name, next-name) << ": " << arg << ",";
		logg(next+1, args...);
	}
	#define timeme 1.0*clock()/CLOCKS_PER_SEC
#else
	#define logan(...) {}
	#define timeme {}
#endif

int a[1'000'001];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	int n; cin >> n;
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}

	if(is_sorted(a, a+n, greater<>())){
		swap(a[n-1], a[n-2]);
		for(int i = 0; i < n; ++i){
			cout << a[i] << ' ';
		}
		cout << endl;
		return 0;
	}

	int k = 0;
	for(int i = 9; i; --i){
		while(a[k] == i) ++k;

		int maxi = n-1;
		for(int j = n-1; j >= k; --j){
			if(a[j] > a[maxi])
				maxi = j;
		}

		if(a[maxi] == a[k]) continue;

		swap(a[k], a[maxi]);
		for(int i = 0; i < n; ++i)
			cout << a[i] << ' ';
		cout << endl;
		return 0;
	}

}