#include<stdio.h>

#define N 100000

int partition(int,int);

int A[N];
int x;

int main() {
  int i,res;

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

  res = partition(0,x - 1);

  for(i = 0; i < x; i++) {
    if(i) {
      printf(" ");
    }
    if(i == res) {
      printf("[");
    }
    printf("%d",A[i]);
   
    if(i == res) {
      printf("]");
    }
    
  }
  printf("\n");
  return 0;
}

int partition(int a, int b) {
  int y,i,j,t;

  y = A[b];
  i = a - 1;

  for(j = a; j < b; j++) {
    if(A[j] <= y) {
      i++;
      t = A[i];
      A[i] = A[j];
      A[j] = t;
    }
  }

  t = A[i + 1];
  A[i + 1] = A[b];
  A[b] = t;

  return i + 1;
}
  

