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

#define SWAP(type, a, b) do { type t = a; a = b; b = t; } while(0)

int partition(int *array, int left, int right) {
    int x = array[right];

    int bnd = 0;
    for(int i = left; i < right; i++) {
        if(array[i] <= x) {
            SWAP(int, array[i], array[bnd]);
            bnd++;
        }
    }

    SWAP(int, array[right], array[bnd]);
    return bnd;
}

int main(void) {
    int n;
    int *array;

    scanf("%d", &n);
    if((array = malloc(n * sizeof(int))) == NULL) {
        return 1;
    }

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

    int bnd = partition(array, 0, n - 1);

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

    return 0;
}
