#include<stdio.h>
#define N 100000

int  partition(int *,int,int);

int main(){
  int n,p=0,q,r,i;
  int A[N];

  scanf("%d",&n);
  r = n-1;

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

  q = partition(A,p,r);

  for(i=0;i<q;i++){
    printf("%d",A[i]);
    printf(" ");
  }
  printf("[%d]",A[q]);
  printf(" ");
  for(i=q+1;i<=r;i++){
    printf("%d",A[i]);
    printf(" ");
  }
  printf("\n");
  
  return 0;
}

int partition(int A[],int p,int r){
  int temp,temp2,i,j,x;
  x = A[r];
  i = p - 1;
  for(j=p;j<r;j++){
    if(A[j] <= x){
      i++;
      temp = A[i];
      A[i] = A[j];
      A[j] = temp;
    }
  }

  temp2 = A[i+1];
  A[i+1] = A[r];
  A[r] = temp2;
  return i+1;
}
  

