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

#define SWAP(x, y) do { int t = x; x = y; y = t; } while (0)

int partition(int a[], int p, int r);

int main(void)
{
    int n;
    int *a;
    int i;
    int k;

    scanf("%d", &n);
    a = calloc(n, sizeof(int));

    scanf("%d", &a[0]);
    for (i = 1; i < n; i++){
        scanf(" %d", &a[i]);
    }
    
    k = partition(a, 0, n - 1);

    printf("%d", a[0]);
    for (i = 1; i < n; i++){
        if (i == k){
            printf(" [%d]", a[i]);
        }else{
            printf(" %d", a[i]);
        }
    }
    puts("");
    return (0);
}

int partition(int a[], int p, int r)
{
    int i;
    int x;
    int j;

    x = a[r];
    i = p - 1;

    for (j = p; j < r; j++){
        if (a[j] <= x){
            i = i + 1;
            SWAP(a[i], a[j]);
        }
    }
    SWAP(a[i+1], a[r]);
    return (i + 1);
}
