#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;

void swap(int &a, int &b) {
	int t = a;
	a = b;
	b = t; 
}

int partition(vector<int> &A, int p, int r) {
	int x = A[r];
	int i = p;
	for (int j = p; j < r; j++) {
		if (A[j] <= x) {
			swap(A[i], A[j]);
			i++;
		}
	}
	swap(A[i], A[r]);
	return i;
}

int main() {
	int n = 0;
	cin >> n;
	vector<int> v;
	v.resize(n);

	{
		vector<int>::iterator i = v.begin();
		copy(istream_iterator<int>(cin), istream_iterator<int>(), i++);
	}
	int p = partition(v, 0, n-1);
	
	for (int i = 0; i < n; i++) {
		if (i == p) {
			cout << "[" << v[i] << "] ";
		}
		else if (i == n - 1) {
			cout << v[i] << "\n";
		}
		else {
			cout << v[i] << " ";
		}
	}
	return 0;
}
#include <stdio.h>

#define N 1000000
int n;
int S[N];

int partition(int n){
	int x = S[n];
	int i,p=0;
	int tmp;
	int j;
	for(i = 0;i < n;i++){
		//printf("%d\n",i);
		if(S[i] <= x){
			tmp  = S[i];
			S[i] = S[p];
			S[p] = tmp;
			p++;
		}
		/*
		for(j=0;j<n+1;j++){
			printf("%d ",S[j]);
		}
		printf("\n");
		printf("p = %d\n",p);*/
	}
	tmp = S[p] ;
	S[p] = S[n];
	S[n] = tmp;
	return p;
}

int main(){
	scanf("%d",&n);
	int i;
	for(i=0;i<n;i++){
		scanf("%d",&S[i]);
	}
	int m = partition(n-1);
	for(i = 0;i< n;i++){
		if(m == i){
			printf("[%d] ",S[i]);
		}
		else if(i == n-1)printf("%d\n",S[i]);
		else printf("%d ",S[i]);
	}
	return 0;
}