#include<stdio.h>
#define N 100001
#define SWAP(a,b){int temp=a; a=b; b=temp;}

int A[N];

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

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


int Partition(int A[], int p, int r){
  int x, i, j;
  
  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;
}





