#define N 100005
#include <stdio.h>

int A[N];

void showArray(int A[], int n, int pos) {
    for (int i = 0; i < n; i++) {
        if (i == pos) {
            printf(" [%d]" + !i, A[i]);
        }
        else {
            printf(" %d" + !i, A[i]);
        }
    }
    printf("\n");
}

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

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 = i + 1;
            swap(&A[i], &A[j]);
        }
    }
    swap(&A[i + 1], &A[r]);
    return i + 1;
}

int main() {
    int n = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &A[i]);
    }
    int pos = partition(A, 0, n - 1);
    showArray(A, n, pos);
    return 0;
}
