#include<stdio.h>
#include<stdlib.h>
#define MAX 1000001
#define VMAX 10000

int partition(int *,int ,int );

int main(){
unsigned short *B;
 int i,j,k,n,p=0,q;
 int A[MAX];
int C[VMAX+1];
k=VMAX;
  scanf("%d",&n);

  B = malloc(sizeof(short)*n+1);

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

    
    q=partition(A, p, n);
 
  for(i=0;i<n-1;i++){
    if(i==q) 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 x,i,f,j;
  x = A[r-1];
  i = p-1;
  for (j = p; j<r-1;j++){
    if (A[j] <= x){
      i = i+1;

      /*A[i] と A[j] を交換*/
      f=A[j];
      A[j]=A[i];
      A[i]=f;
    }
  }
  /*A[i+1] と A[r] を交換*/
        f=A[r-1];
	A[r-1]=A[i+1];
	A[i+1]=f;
	return i+1;
}

