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

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

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

    int xxx = partition(arr, 0, n-1);

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

    return 0;
}
