#include <string>
#include <iostream>
using namespace std;
#define NMAX 1000000
int a[NMAX];

void prevNum(int *num, int n)
{
	int index = -1;
	for (int i = n - 2; i >= 0; i--) {
		if (num[i] > num[i + 1])
		{
			index = i;
			break;
		}
	}
	int smallGreatDgt = -1;
	for (int i = n - 1; i > index; i--) {
		if (num[i] < num[index]) {
			if (smallGreatDgt == -1)
				smallGreatDgt = i;
			else if (num[i] >= num[smallGreatDgt])
				smallGreatDgt = i;
		}
	}
	if (index == -1)
		return;
	if (smallGreatDgt != -1)
	{
		swap(num[index], num[smallGreatDgt]);	
	}

}
void largestNum(int *num,int n)
{
	int max_digit = -1;
	int max_digit_index= -1;
	int left_index = -1;
	int right_index = -1;
	for (int i = n - 1; i >= 0; i--) {

		if (num[i] > max_digit) {
			max_digit = num[i];
			max_digit_index= i;
			continue;
		}
		if (num[i] < max_digit) {
			left_index = i;
			right_index = max_digit_index;
		}
	}

	if (left_index == -1)
		prevNum(num, n);
	swap(num[left_index], num[right_index]);
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	bool first = true;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	largestNum(a,n);
	for (int i = 0; i < n; i++)
	{
		if (!first) cout << " ";
		cout<< a[i];
		first = false;
	}
	return 0;
}