#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<stdio.h>
#include<stdlib.h>
int partition(int A[], int p, int r);
int main(){
	int *A, p,r,n,i;
	scanf("%d", &n);
	A = (int *)malloc(sizeof(int)*n);
	r = n-1;
	for (i = 0; i < n; i++) scanf("%d", A + i);
	n=partition(A, 0, r);
	for (i = 0; i < n; i++) printf("%d ", A[i]);
	printf("[%d] ", A[i++]);
	for (; i <= r; i++) printf("%d%c", A[i],i==r?'\n':' ');
	free(A);
	return 0;
}

int partition(int A[], int p, int r){
	int i,x,j,tmp;
	x = A[r];
	i = p-1;
	for (j = p; j <= r; j++) {
		if (A[j] <= x) {
			i++;
			tmp = A[i]; A[i] = A[j]; A[j] = tmp;
		}
	}
	//tmp = A[i]; A[i] = A[r]; A[r] = tmp;
	return i;
}