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

int Partition(int *,int,int);
void swap(int *,int *); 

int main(){
  int n,i;
  int *A;
  int q;
  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 != 0) printf(" ");
    if(i == q)printf("[");
    printf("%d",A[i]);
    if(i == q)printf("]");
    }

  printf("\n");

  return 0;
}



int Partition(int A[],int p,int r){
  int x,i,j,tmp;
  x = A[r];
  i = p-1;
  for(j=p; j<=r-1; j++){
    if(A[j] <= x){
      i++;
      swap(&A[i],&A[j]);
    }
    
  }

  swap(&A[i+1],&A[r]);
  return i+1;
}

void swap(int *a,int *b){
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}

