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

int partition(int*,int,int);

int main(){
  
  int i,n;
  int A[100000];
  
  scanf("%d",&n);
  
  for(i=0; i<n; i++)
    scanf("%d",&A[i]);
  
  int Y;
  Y = partition(A, 0, n-1);
  
  i = A[Y];
  A[Y] = A[n-1];
  A[n-1] = i;
 
  for(i=0; i<n-1; i++){
    if(i==Y) printf("[%d] ",A[i]);
    else printf("%d ",A[i]);
  }
  printf("%d",A[n-1]);
  printf("\n");
  
  return 0;
}


int partition(int *A,int p,int r){
  int x=A[r];
  int i = p-1;
  int y=0;
  int j;
  for(j=p; j<=r-1; j++){
    if(A[j] <= x){ 
      i=i+1;
      y = A[i];
      A[i] = A[j];
      A[j] = y;
    }
    else{y = A[i+1];
    A[i+1] = A[r];
    A[r] = y;}
  }
  
  return i+1;
}