#include<stdio.h>
#define MAX 2000000

int partition(int [],int,int);

int a[MAX];
int main(){
  int i,n,p,o;

  scanf("%d",&n);

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

  p = 0;
  o = partition(a,p,n-1);

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


int partition(int a[],int p,int r){
  int x,i,j,key,key2;
  x = a[r];
  i = p-1;
  for(j=p; j<=r-1; j++){
    if(a[j] <= x){
      i += 1;
    key = a[i];
    a[i] = a[j];
    a[j] = key;
    }
  }
  key2 = a[i+1];
  a[i+1] = a[r];
  a[r] = key2;

  return i+1;
}