#include <stdio.h>
int partition(int *,int,int);

int main(){
  int i,A[1000000],num,pa;

  scanf("%d",&num);

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

  pa = partition(A,1,num-1);

 for(i=0;;i++){
    printf("%d",A[i]);
    if(i>=pa-1){
      break;
    }
    printf(" ");
  }

 printf(" [%d] ",A[pa]);
  
  for(i=pa+1;;i++){
    printf("%d",A[i]);
    if(i>=num-1){
      break;
    }
    printf(" ");
  }

printf("\n");

  return 0;
}

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

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

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

  return i+1;
}