#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n;
vector <int> A;
vector <int> B(10);
int ic;
int p;
int c = -1;
int cmax;

void PrikaziB()
{
	for ( int g = 0; g < B.size(); g++ )
	{
		cout << B[g] << " ";
	}
}

void PrikaziA()
{
	for ( int g = 0; g < n; g++ )
	{
		cout << A[g] << " ";
	}
}

void Faza1()
{
	cin >> n;
	for ( int g = 0; g < n; g++ )
	{
		cin >> p;
		A.push_back(p);
	}
	fill( B.begin(), B.end(), -1 );

	for ( int t = 0; t < n-1; t++ )
	{
		if ( B[A[t]] == -1 )
		{
			B[A[t]] = t;
		}
		if ( A[t+1] > A[t] )
		{
			ic = t;
			cmax = A[t+1];
			break;
		}
	}
}

void Faza2()
{
	for ( int t = ic; t < n; t++ )
	{
		if ( A[t] >= cmax )
		{
			c = t;
			cmax = A[t];
		}
	}
	if ( c == -1 )
	{
		c = ic;
	}
}

void Faza3()
{
	int t;
	for ( t = 0; t < c; t++ )
	{
		if ( A[t] < cmax )
		{
			int sw = A[t];
			A[t] = A[c];
			A[c] = sw;
			break;
		}
	}
	if ( t == c )
	{
		int sw = A[c+1];
		A[c+1] = A[c];
		A[c] = sw;
	} 
}

int _tmain(int argc, _TCHAR* argv[])
{	
	Faza1();
	if ( n == 2 )
	{
		cout << A[1] << " " << A[0];
		cin.get(); cin.get(); return 0;
	}
	Faza2();
	Faza3();
	PrikaziA();

	cin.get(); 
	cin.get();
	return 0;
}