#include <stdio.h>
#include <stdlib.h>

int partition(int *A,int p,int r){
	int x = A[r];
	int i = p-1;
	int j = 0;
	int temp = 0;
	for(j = p;j < r;j++){
		if(A[j] <= x){
			i = i+1;
			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(int argc, char **argv){
	int n = 0;
	int i = 0;
	int r = 0;
	scanf("%d",&n);

	int *A = malloc(sizeof(int) * n);

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

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