#include <stdio.h>
#define MAX 100000

int partition(int *,int ,int);

int main(){

  int i=0;
  int A[MAX];
  int n=0;
  int border_n=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
  }
  border_n=partition(A,0,n);
  for(i=0;i<n;i++){
    if(i!=0)printf(" ");
    if(i==border_n)printf("[");
    printf("%d",A[i]);
     if(i==border_n)printf("]");
  }
  printf("\n");
  
  return 0;
}

int partition(int *A, int p, int r){
  int judge=0;
  int i=0;
  int j=0;
  int swap=0;
  judge=A[r-1];
  printf("judge:%d\n",judge);
  i=p-1;
  for(j=p;j<r-1;j++){
    if(A[j]<=judge){
      i++;
      swap=A[j];
      A[j]=A[i];
      A[i]=swap;
    }
  }
  swap=A[r-1];
  A[r-1]=A[i+1];
  A[i+1]=swap;
  return i+1;
}

