#include <stdio.h>
int partition(int A[],int p,int r);

int main(){
   int a[100000];
  int i,j,n;

  scanf("%d",&n);

  for(i=0;i<n;i++)

  scanf("%d",&a[i]);
  j=partition(a,0,n-1);
 
  for(i=0;i<j;i++)
 
  printf("%d ",a[i]);
  printf("[%d] ",a[j]);
  for(i=j+1;i<n-1;i++)
  
  printf("%d ",a[i]);
  printf("%d",a[i]);
  printf("\n");
  return 0;
}

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