/* ADLS1_6_B:Partition */
/* 20181120 AOJ */

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#define N 100000

int partition(int *A, int p, int r);
void swap(int *x, int *y);

int main(void){
	int n, i = 0, A[N], q;

	scanf("%d", &n);
	while(i < n) {
		scanf("%d", &A[i++]);
	}

	q = partition(A, 0, n - 1);
	for(i = 0; i < n; i++) {
		if(i) printf(" ");
		if(i == q) {
			printf("[%d]", A[i]);
		}else {
			printf("%d", A[i]);
		}
	}
	printf("\n");

	return 0;
}

int partition(int *A, int p, int r) {
	int i, j;

	i = p - 1;
	for(j = p; j < r; j++) {
		if(A[j] <= A[r]) {
			i++;
			swap(&A[i], &A[j]);
		}
	}
	swap(&A[++i], &A[r]);
	return i;
}

void swap(int *x, int *y) {
	int temp = *x;
	*x = *y;
	*y = temp;
}

