//パーティション
//2018-12-21
//Ebitani Akira

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

#define N 100000

int Array[N];
int n;

int main (){
  int i, p;

  scanf( "%d", &n );
  if( n < 1 || n > 100000 ){
    printf( "要素数が適当ではありません。\n" );
    exit(1);
  }
  for( i = 0 ; i < n ; i++ ){
    scanf( "%d", &Array[i] );
  }

  p = partition( 0, n - 1 );

  for( i = 0 ; i < n ; i++ ){
    if( i == p ){
      printf( "[" );
    }
    
    printf( "%d", Array[i] );
    
    if( i == p ){
      printf( "]" );
    }


    if( i != n - 1 ){
      printf( " " );
    }
  }
  printf( "\n" );
  return 0;
}

int partition( int a, int b ){
  int A, i, j, temp;

  A = Array[b];
  i = a - 1;

  for( j = a ; j < b ; j++ ){
    if( Array[j] <= A ){
      i++;
      temp = Array[i];
      Array[i] = Array[j];
      Array[j] = temp;
    }
  }

  temp = Array[i+1];
  Array[i+1] = Array[b];
  Array[b] = temp;

  return i + 1;
}
      

