#include<stdio.h>
#define N 100000

int partition(int *,int, int);

int main(){

  int i,n,a,S[N];

  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&S[i]);
  a = partition(S,0,n-1);

  for(i=0;i<n;i++){
    if(i==a) printf("[%d]",S[i]);
    else printf("%d",S[i]);
    if(i != n-1) printf(" ");  
	 

  }
  printf("\n");


  return 0;
}

int partition(int *S,int p,int r){
  int x,i,j,temp;

  x = S[r];
  i = p-1;
  for(j=p;j<r;j++){
    if(S[j]<= x){
      i = i+1;
      temp = S[i];
      S[i] = S[j];
      S[j] = temp;

    }

  }
  temp = S[i+1];
  S[i+1] = S[r];
  S[r] = temp;

  return i+1;
  


}

