#include <iostream>
#include <vector>
using namespace std;

void swap(vector<int> &arr, int  x, int y)
{
	int temp = arr[x];
	arr[x] = arr[y];
	arr[y] = temp;
}

void QuickSort(vector<int> &arr, int l, int d)
{
	if (l < d)
	{
		int k = l;
		for (int i = l + 1; i <= d; i++)
		{
			if (arr[i] >= arr[l])
			{
				k++;
				swap(arr, i, k);
			}
		}
		swap(arr, l, k);
		QuickSort(arr, l, k - 1);
		QuickSort(arr, k + 1, d);
	}
}

int main()
{
	vector <int> polica;
	vector <int> sortirane_cifre;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int element;
		cin >> element;
		polica.push_back(element);
	}
	sortirane_cifre = polica;
	QuickSort(sortirane_cifre, 0, n - 1);
	int k1 = -1;
	int k2 = -1;
	int max_cifra = -1;
	for (int i = 0; i < n; i++)
	{
		if (polica[i] != sortirane_cifre[i] && k1 == -1)
		{
			k1 = i;
			max_cifra = sortirane_cifre[i];
		}
		else if (k1 != -1)
		{
			if (polica[n + k1 - i] == max_cifra)
			{
				k2 = n + k1 - i;
				break;
			}
		}
	}
	if (k1 != -1)
	{
		swap(polica, k1, k2);
	}
	else
	{
		swap(polica, n - 1, n - 2);
	}
	for (int i = 0; i < n; i++)
	{
		cout << polica[i] << " ";
	}
}