#include<stdio.h>

 int A[100001];

 int part(int p,int n){
       int i,j,key,tmp,k;
       key=A[n];
       i=p-1;
       for(j=p;j<n;j++){
               if(A[j]<=key){
                     i++;
                     tmp=A[j];A[j]=A[i];A[i]=tmp;
                     }
                }
       i++;
       tmp=A[n];A[n]=A[i];A[i]=tmp;

       return i;
       }
       
                

 int main(){
       int i,n,key,k;
       scanf("%d",&n);
       for(i=0;i<n;i++){
                 scanf("%d",&A[i]);
                 }
       key=part(0,n-1);
       for(k=0;k<n;k++){
                if(k)printf(" ");
                if(k==key)printf("[");
                printf("%d",A[k]);
                if(k==key)printf("]");
               }
       printf("\n");

       return 0;
       }


