#include <iostream>
#include <limits>

using namespace std;

typedef numeric_limits<streamsize> ignore_t;

void zamena(int &a, int &b) 
{
	int temp = a;
	a = b;
	b = temp;
}

int main() 
{
	int N{};
	
	scanf_s("%d\n", &N);

	int* A = new int[N];

	for (int i{}; i < N; ++i)
		cin >> A[i];
	cin.ignore(ignore_t::max(), '\n');

	bool zam = false;

	if (N > 2) 
	{
		for (int i{}; i < N; ++i) 
		{
			int max{i};
			for (int j{ i + 1 }; j < N; ++j) 
				if (A[j] > A[max]) 
					max = j;

			if (A[max] > A[i]) 
			{
				zamena(A[i], A[max]);
				zam = true;
				break;
			}
		}
	}
	else if(N = 2)
	{
		zamena(A[0], A[1]);
		zam = true;
	}

	if (!zam) 
	{
		zamena(A[N - 2], A[N - 1]);
	}

	for (int i{}; i < N; ++i)
		cout << A[i] << ' ';

	cin.clear();
	cin.ignore(ignore_t::max(), '\n');
	cin.get();

	return 0;
}