#include <iostream>
#include <vector>

int main()
{
	long long N;
	std::cin >> N;
	
	std::vector<int> v;
	
	int t;
	
	while (N > 0)
	{
		std::cin >> t;
		v.push_back(t);
		--N;
	}
	
	if (v.size() == 2)
	{
		std::swap(v[0], v[1]);
		std::cout << v[0] << " " << v[1];
		
		return 0;
	}
	
	long long r = v.size() - 1;
	long long l = 0;
	
	for (int i = v.size() - 1; i >= 0; --i)
	{
		if (v[i] > v[r] && i != 0)
		{
			r = i;
		}
		else if (v[i] < v[l])
		{
			l = i;
		}
	}
	
	std::swap(v[l], v[r]);
	
	for (const auto& r : v)
	{
		std::cout << r << " ";
	}
	
	return 0;
}