#include<iostream>
#include<algorithm>
using namespace std;
int partition(int a[],int p,int r){
int x=a[r];
int i=p-1,j,no;
	for(j=p;j<r;j++){
		if(a[j]<=x){
		i++;
		no=a[i];
		a[i]=a[j];
		a[j]=no;
		}
	}
		no=a[i+1];
		a[i+1]=a[r];
		a[r]=no;
	return i+1;		
}

int main(){
	int i,n,ind;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	ind=partition(a,0,n-1);
	for(i=0;i<n;i++){
		if(i==ind)
		printf("[%d] ",a[i]);
		else if(i==n-1)
		printf("%d\n",a[i]);
		else 
		printf("%d ",a[i]);
	}
	
	return 0;
}
