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

int partition(int * ,int ,int );

int main(){
  int i,n,q;
  int *a;

  scanf("%d",&n);

  a = (int*)malloc(n*sizeof(int));

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

  q = partition( a , 0 , n-1 );

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

  printf("\n");

  return 0;
}


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

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

  return i+1;
}