#include <stdio.h>
#include <stdlib.h>
int partition(int,int);
int *A;
int main(){
  int n,i,r,a;
  scanf("%d",&n);

  A=(int *)malloc(sizeof(int)*n);
  
  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
 
  }

  r=partition(0,n-1);


  for(i=0;i<r;i++){
    if(i>0) printf(" ");
    printf("%d",A[i]);
  }
  printf(" [%d]",A[r]);

  for(i=r+1;i<n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  
  free(A);
  return 0; 
}

int partition(int p,int r){
  int x,j,a,b,i;

  x=A[r];
  i=p-1;
  for(j=p;j<r;j++){
  
    if(A[j]<=x){
      i=i+1;
      a=A[i];
      A[i]=A[j];
      A[j]=a;
    }
  }
  b=A[i+1];
  A[i+1]=A[r];
  A[r]=b;
  
  return i+1;
}