#include <stdio.h>
#include <stdlib.h>
int a[100001],n;

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

int main()
{

     int i,m;

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

   m = Partition(a,0,n-1);

   for(i=0;i<m;i++){
          printf("%d ",a[i]);
   }
   printf("[%d] ",a[m]);

   for(i=m+1;i<n-1;i++){
          printf("%d ",a[i]);
   }

   printf("%d\n",a[n-1]);

   return 0;
}


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

     x = a[r];
     i = p-1;

     for(j=p;j<=r-1;j++){
          if(a[j] <= x){

               i += 1;
               temp = a[i];
               a[i] = a[j];
               a[j] = temp;
        }
 }

 temp2 = a[i+1];
 a[i+1] = a[r]; 
 a[r]= temp2;  

 return i+1;
}