#include <stdio.h>
#include <stdlib.h>

int partition(int A[],int p,int r){
    int x = A[r];
    int i = p-1;
    int ex, j;
    for(j=p; j<r; j++){
        if(A[j]<=x){
      i = i+1;
      ex = A[j];
      A[j] = A[i];
      A[i] = ex;
        }
    }
    ex = A[r];
    A[r] = A[i+1];
    A[i+1] = ex;
    return i+1;
}

int main(int argc, char** argv){
    int n;
    scanf("%d",&n);
    int A[n];

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

    int tar = partition(A, 0, n-1);

    for(int i=0; i<tar; i++){
        printf("%d ", A[i]);
    }
        printf("[%d] ", A[tar]);

    for(int i=tar+1; i<n; i++){
        if(i==n-1){
            printf("%d\n", A[i]);
        }
        else printf("%d ",A[i]);
    }

    return 0;
}

