#include<stdio.h>
int partition(int *,int,int);
int main(){
	int n,x,i,k=-1;
	scanf("%d",&n);
	int A[n];
	for(i=0;i<n;i++){
		scanf("%d",&A[i]);
	}
	k=partition(A,0,n-1);
for(i=0;i<n;i++){
		if(i==n-1 && i!=k) printf("%d",A[i]);
		else if(i!=n-1 && i!=k) printf("%d ",A[i]);
		else if(i!=n-1 && i==k) printf("[%d] ",A[i]);
		else printf("[%d]",A[i]);
	}
printf("\n");
	return 0;
}

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