/* B Partition */

#include<stdio.h>
int n; /* 1<=n<=100000 */

int Partition(int A[], int p, int r);

int main(){
  int i;
  int q;

  scanf("%d",&n);
  int A[n];

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

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

  return 0;
}


int Partition(int A[], int p, int r){
  int i, j, x;
  int temp;

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