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

int Partition(int *,int ,int );

int main() {
  int *A;
  int n;
  int i,j;
  int key; //r
  int x;

  scanf("%d",&n);
  A = (int *)malloc(n * sizeof(int));
  for(i=0;i<n;i++) scanf("%d",&A[i]);

  j=Partition(A,0,n-1);
  /*ここで返ってくるjはkeyの座標点*/
  
  for(i=0;i<n-1;i++) {
    if(i==j) printf("[%d] ",A[i]);
    else printf("%d ",A[i]);
  }
  printf("%d\n",A[n-1]);
  
  return 0;
}

int Partition(int *A,int p,int r) {
  int key; //x
  int i,j;
  int tmp;
  key = A[r];
  i=p-1;
  for(j=p;j<r;j++) {
    if(A[j] <= key) {
      i++;
      tmp = A[i];
      A[i] = A[j];
      A[j] = tmp;
    }
  }
  tmp = A[i+1];
  A[i+1] = A[r];
  A[r] = tmp;
  return i+1;
}

