#include<stdio.h>
#define N 100000

int A[N];
int partition(int p,int r){
int x,i,j,change;
x=A[r-1];
i=p-1;//pは0を渡す、i=0での入れ替え用
for(j=p;j<r-1;j++){
  if(A[j]<=x){
    i++;
    change=A[i];
    A[i]=A[j];
    A[j]=change;
  }
}
change=A[i+1];//基準はi+1の位置に動く
A[i+1]=A[r-1];
A[r-1]=change;
return i+1;
}

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




  return 0;
}

