#include <stdio.h>
#include <stdlib.h>

int partition(int *,int,int);

int *A;

int main(){
  int n,i,index;

  scanf("%d",&n);

  A = (int *)malloc(sizeof(int) * n);
  if(A == NULL) exit(0);

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

  index = partition(A,0,n-1);

  /* 出力 */
  for(i = 0;i < n;i++){
    if(i > 0) printf(" ");
    if(i == index) printf("[%d]",A[i]);
    else printf("%d",A[i]);
  }
  printf("\n");
    return 0;
}

int partition(int *A,int p,int r) /* p = 要素の始め、r = 要素の終わり */
{
  int x,i,j,stack;

  x = A[r]; /* xはpartitionの基準 */
  i = p - 1;

  for(j = p;j <= r - 1;j++){
    if(A[j] <= x){
      i = i + 1;
      stack = A[i];
      A[i] = A[j];
      A[j] = stack;
    }
  }
  stack = A[i + 1];
  A[i + 1] = A[r];
  A[r] = stack;

  return i+1; /* 次のpartitionの基準となる値を返す */
}