#include<stdio.h>
#define MAX 100001

int A[MAX];

int partion(int n){
    int i,j,tmp;
    int key=A[n-1];
    i=0;
    j=i-1;
    while(i<n-1){
          if(A[i]<=key){i++;}
          else {
                j++;
                tmp=A[j];A[j]=A[i];A[i]=tmp;
                i++;
                }
          }
    tmp=A[j];A[j]=A[n-1];A[n-1]=tmp;
    return j;
    }



int main(){
    int i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++){
                scanf("%d",&A[i]);
                }
    int key=partion(n);
    for(i=0;i<key;i++){
                printf("%d ",A[i]);
                }
    printf("[%d] ",A[key]);
    for(i=0;i<n;i++){
                printf("%d%c",A[i],i==n-1?'\n':' ');
                }
    return 0;
    }
              






