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

int Partition(int *A, int p, int r);
void PrintData(int *A, int n, int r);

int main(){

  int i;
  int n;
  int *A;
  int p,q,r;
  scanf("%d", &n);

  A = (int *)malloc(n * sizeof(int));

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

  q = Partition(A, 0, n);

  PrintData(A, n, q);
  
  return 0;
}

int Partition(int *A, int p, int r){

  int x;
  int i,j;
  int temp;
  
  x = A[r-1];
  i = p-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;
}

void PrintData(int *A, int n, int r){

  int i;

  for(i=0; i<n; i++){
    if(i == r) printf("[%d] ", A[i]);
    else if(i == n-1) printf("%d\n", A[i]);
    else printf("%d ", A[i]);
  }
}

