#include<stdio.h>
#define N 100000
int partition(int, int);
int A[N];
int main(){
  int n,a,i;
  scanf("%d",&n);
  for(i=0;i<n;i++) {
    scanf("%d",&A[i]);
  }
  a=partition(0,n-1);
  for(i=0;i<n;i++){
    if(i>0){
      printf(" ");
    }if(i==a){
      printf("[%d]",A[i]);
    }
    else {
      printf("%d",A[i]);
    }
  }
  printf("\n");

  return 0;
}

int partition(int a, int r){
  int x,i,k,cn;
  x=A[r];
  cn=a-1;
  for(i=a;i<r;i++){
    if(A[i]<=x){
      cn++;
      k=A[cn];
      A[cn]=A[i];
      A[i]=k;
    }
  }

  k=A[cn+1];
  A[cn+1]=A[r];
  A[r]=k;

  return cn+1;
}