#include <stdio.h>

int partition(int A[], int p, int r) {

    int x = A[r];
    int i = p - 1;

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

    int tmp = A[i + 1];
    A[i + 1] = A[r];
    A[r] = tmp;

    return i + 1;
}

int main() {

    int num;
    scanf("%d", &num);

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

    int p = partition(A, 0, num - 1);

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

    return 0;
}

