#include <stdio.h>
#define exchange(a,b) {tmp=a;a=b;b=tmp;}

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

  return i+1;
}

int main(){
  int A[100000],n,i,k;
  
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
  }
  k=Partition(A,0,n-1);

  for(i=0;i<n;i++){
    if(i == k){    
      printf("[%d]",A[i]);
    }else{
      printf("%d",A[i]);
    }
    if(i<n-1)
      printf(" ");
  }
  printf("\n");

  return 0;
  
}