#include <stdio.h>

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

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

