#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

static int n, cifre[10] = {};
static unsigned char polica[1000000];

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    	cin >> n;
        	unsigned char max_cifra = 0;
            	for (int i = 0; i < n; i++)
                	{
                    		unsigned int cifra;
                            		cin >> cifra;
                                    		cifre[cifra]++;
                                            		polica[i] = cifra;
                                                    		if (cifra > max_cifra)
                                                            			max_cifra = cifra;
                                                                        	}
                                                                            	int jedan = n-1, dva = n-2;
                                                                                	for (int i = 0; i < n; i++)
                                                                                    		if (max_cifra != polica[i])
                                                                                            		{
                                                                                                    			jedan = i;
                                                                                                                			for (dva = i + 1; polica[dva] != max_cifra; dva++);
                                                                                                                            			break;
                                                                                                                                        		}
                                                                                                                                                		else
                                                                                                                                                        		{
                                                                                                                                                                			if (i != n-1 && polica[i] == polica[i+1])
                                                                                                                                                                            			{
                                                                                                                                                                                        				jedan = i; dva = i + 1;
                                                                                                                                                                                                        			}
                                                                                                                                                                                                                    			cifre[max_cifra]--;
                                                                                                                                                                                                                                			while (!cifre[max_cifra])
                                                                                                                                                                                                                                            			   	if (--max_cifra == -1)
                                                                                                                                                                                                                                                               					goto boutta_head_out;
                                                                                                                                                                                                                                                                                   		}
                                                                                                                                                                                                                                                                                           	boutta_head_out:
                                                                                                                                                                                                                                                                                               	swap(polica[jedan], polica[dva]);
                                                                                                                                                                                                                                                                                                   	for (int i = 0; i < n; i++)
                                                                                                                                                                                                                                                                                                       		cout << (int)polica[i] << ' ';
                                                                                                                                                                                                                                                                                                               }