#include<bits/stdc++.h>
#define maxn 1000010
using namespace std;

int n, ans, a[maxn], nearD[11], farD[11];

struct expT
{
	int a, x1, x2; // a*10^x1 - a*10^x2, x2 < x1
	expT(int a, int x1, int x2){
		this->a = a;
		this->x1 = x1;
		this->x2 = x2;
	}
	bool operator<(const expT& e) const{
	    if(a == 0){
            if(e.a){
                return 1;
            } else {
                return 0;
            }
	    }
        if(e.a == 0){
            return 0;
        }
		if(x1 == e.x1){
			if(a == e.a){
				return x2 > e.x2;
			} else {
				return a < e.a;
			}
		} else {
			return x1 < e.x1;
		}
	}
};

vector<expT> posC, negC;

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> n;
	for(int i = 0; i < 10; ++i){
		farD[i] = -1;
		nearD[i] = -1;
	}
	for(int i = 0; i < n; ++i){
		int x;
		cin >> x;
		a[i] = x;
		for(int j = 0; j <= x; ++j){
			if(farD[j] != -1){
				posC.push_back({x-j, n - farD[j] - 1, n - i - 1});
			}
		}
		for(int j = x + 1; j < 10; ++j){
			if(nearD[j] != -1){
				negC.push_back({j-x, n - nearD[j] - 1, n - i - 1});
			}
		}
		if(farD[x] == -1){
			farD[x] = i;
		}
		nearD[x] = i;
	}
	expT res(0, 0, 0);
	if(posC.size()){
		res = *max_element(posC.begin(), posC.end());
	} else {
		res = *min_element(negC.begin(), negC.end());
	}
	swap(a[n-res.x1-1], a[n-res.x2-1]);
	for(int i = 0; i < n; ++i){
		cout << a[i] << '\n';
	}
}