#include<stdio.h>

int A[100000],n;

int partition(int,int);

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

  a = partition(0,n - 1);

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

  return 0;
}

int partition(int s,int t){
  int x,y,them,j;

  x = A[t];
  y = s - 1;
  for(j = s; j < t; j++){
    if(A[j] <= x){
      y++;
      them = A[y];
      A[y] = A[j];
      A[j] = them;
    }
  }
  them = A[y+1];
  A[y+1] = A[t];
  A[t] = them;
  return y+1;
}

