#include <stdio.h>
#define MAX 100000

int Partition(int *, int, int);

int main()
{
    int i, n;
    int A[MAX];
    scanf("%d", &n);
    for (i = 0; i < n; i++) scanf("%d", &A[i]);
    int pos = Partition(A, 0, n-1);
    for (i = 0; i < n-1; i++) {
        (i == pos)? printf("[%d] ", A[i]) : printf("%d ", A[i]);
    }
    printf("%d\n", A[i]);
}

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

