#include <stdio.h>
#include <stdlib.h>
int partition(int *A, int p, int r){
    int x = A[r - 1];
    int i = p-1;
    int tmp;

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

    tmp = A[i];
    
    A[i] = x;
    A[r-1] = tmp;
    

    return i;
}

int main(void){
    int n;
    int A[100000];
    int p;

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

    p = partition(A, 0, n);

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

    printf("[%d] ", A[p]);

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

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

    //printf("\n");
    return 0;
}

