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

void printer2(int *, int, int);
int partition(int *, int, int);

int main(){
  int i, n;
  int *A;

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

  i = partition(A, 0, n);

  printer2(A, n, i);

  return 0;
}

void printer2(int *array, int n, int x){
  int i;
  for(i=0; i<n; i++){
    if(i == x) printf("[%d]",array[i]);
    else printf("%d",array[i]);

    if(i != n-1) printf(" ");
    else printf("\n");
  }
}

int partition(int *A, int p, int r){
  int i=p-1, j, temp,  x=A[r-1];

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

  return i+1;
}

