#include <stdio.h>
#include<math.h>
#define MAX 100005

int i,j,n;
int A[MAX], B[MAX], C[10000];

int parti(int a[], int p, int r){
	int x, temp;
	x = a[r];
	i = p-1;
	for (j = p; j < r; j++){
		if (a[j] <= x){
			i++;
			temp = a[i]; 
			a[i] = a[j];
			a[j] = temp;
		}
	}	
	temp = a[i+1];
	a[i+1] = a[r];
	a[r] = temp;
	return i+1;
}

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