#include <stdio.h>
int patition(int a[100000], int p, int r) {
    int x, i, j, tmp;
 
    x = a[r];
    i = p - 1;
    for (j = p; j < r; j++) {
        if (a[j] <= x) {
            i += 1;
            tmp = a[j];
            a[j] = a[i];
            a[i] = tmp;
        }
    }
    tmp = a[i+1];
    a[i+1] = a[r];
    a[r] = tmp;
    return i + 1;
}
 
int main(void) {
    int in[100000], n, i, p;
    const char *fmt = "%d";
 
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", in+i);
    }
    p = patition(in, 0, n-1);
 
    for (i = 0; i < p; i++) {
        printf(fmt, in[i]);
        fmt = " %d";
    }
    printf(" [%d]", in[p]);
    for (i = p+1; i < n; i++) {
        printf(fmt, in[i]);
    }
    putchar('\n');
    return 0;
}

