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

void swap(int Array[], int i, int j) {
    int temp;
    temp = Array[i];
    Array[i] = Array[j];
    Array[j] = temp;
}

int partition(int Array[], int pIndex, int rIndex) {
    int x = Array[rIndex];
    int i = pIndex;
    int temp;
    
    for (int j = pIndex; j < rIndex; j++)
        if (Array[j] <= x) {
            swap(Array, i, j);
            i++;
        }

    swap(Array, i, rIndex);
    return i;
}

void printArray(int array[], int n) {
    for (int i = 0; i < n; i++) {
        if (i != 0) printf(" ");
        printf("%d", array[i]);
    };
}

void printArrayHighlight(int array[], int n, int hightlightIndex) {
    for (int i = 0; i < n; i++) {
        if (i != 0) printf(" ");
        if (i == hightlightIndex)
            printf("[%d]", array[i]);
        else
            printf("%d", array[i]);
    };
}

int main(int argc, char** argv) {
    int nArray;
    scanf("%d", &nArray);
    
    int array[nArray], brrby[nArray];
    
    for (int iArray = 0; iArray < nArray; iArray++) {
        scanf("%d", &array[iArray]);
    }
    
    // do the magic
    int index = partition(array, 0, nArray - 1);
    
    printArrayHighlight(array, nArray, index);
    printf("\n");

    return (EXIT_SUCCESS);
}


