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

int partition(int,int);

#define MAX 10000
int *A;
int N;

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

  x=partition(0,N-1);

  for (i=0;i<N-1;i++){
    if(i==x){
      printf("[");
    }
    printf("%d", A[i]);
    if(i==x){
      printf("]");
    }
    if(i!=N-1){
      printf(" ");
    }
  }

  printf("%d\n",A[N-1]);

  free(A);

  return 0;
}

int partition(int p, int r){
  int x,i,j,a,b;
  x = A[r];
  i = p-1;
  for(j=p;j<=r-1;j++){
    if(A[j] <= x){
      i = i+1;
      // A[i] ??¨ A[j] ?????????
      a=A[i];
      A[i]=A[j];
      A[j]=a;
    }
  }
  //A[i+1] ??¨ A[r] ?????????
  b=A[i+1];
  A[i+1]=A[r];
  A[r]=b;

  return i+1;
}