#include <stdio.h>
#include <stdlib.h>

int partition(int *,int ,int );

int main(){
  int n, i, *A, key, last;

  scanf("%d",&n);
  if(n<1 || n>100000) exit(1);

  A = malloc( n*sizeof(int) );
  
  for(i=0; i<n; i++){
    scanf("%d", &A[i]);
    if(A[i]<0 || A[i]>100000) exit(2);
  }
  last = A[n-1];
  
  key = partition(A, 0, n-1);

  for(i=0; i<n; i++){
    if(A[i] == last){
      printf("[%d]", last);
      if(i<n-1) printf(" ");
      continue;
    }
    printf("%d", A[i]);
    if(i<n-1) printf(" ");
  }
  printf("\n");
    
  return 0;
}

int partition(int *A, int p, int r){
  int i, j, x, tmp, omp;

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

  return i+1;
}

