#include <stdio.h>
#include <stdlib.h>
#define NMAX 2000001
#define AMAX 10000

int partition(short*,int,int);

int main(){

  unsigned short *A;

  int i,n;
  int x,p,q,r;

  scanf("%d",&n);

  p=0;
  r=n-1;

  A=malloc(sizeof(short)*n+1);

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


  q=partition(A,p,r);



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

  for(i=q+1;i<n;i++){
    printf("%d",A[i]);
    if(i!=n-1) printf(" ");
  }
  printf("\n");
  


  return 0;
  
}


int partition(short* A,int p,int r){
    int i,j,x,mv;
  i=p-1;
  x=A[r];

  for(j=p;j<r;j++){
    if(A[j]<=x){
      i++;
 
      mv=A[i];
      A[i]=A[j];
      A[j]=mv;
    }

  }

  mv=A[i+1];
  A[i+1]=A[r];
  A[r]=mv;

  return i+1;
}