#include <stdio.h>
#define Max 100000
 
int partition(int A[],int,int);
 
int main(){
  int a[Max];
  int n,q;
  int i;
 
  scanf("%d",&n);
  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]);
 
      if(i == q) printf("]");
 
      if(i != n-1) printf(" ");
  }
  printf("\n");
 
  return 0;
}
 
int partition(int A[],int p,int r){
  int x;
  int i,j;
  int s;
 
  x = a[r];
  i = p-1;
  for(j = p; j < r; j++){
    if(a[j] <= x){
      i++;
      s = a[i];
      a[i] = a[j];
      a[j] = s;
    }
  }
      s = a[i+1];
      a[i+1] = a[r];
      a[r] = s;
  
    return i+1;
  }