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

int partition(int,int[],int,int);
void printArray(int,int[]);

int main(int argc, char** argv) {
    int size;
    scanf("%d\n", &size);
    int array[size];
    for(int i = 0; i < size; i++)
        scanf("%d", &array[i]);
    
    int loc = partition(size,array,0,size-1);
    for(int i = 0; i < size; i++){
        if(i == size-1)
            printf("%d\n", array[i]);
        else if(i == loc)
            printf("[%d] ", array[i]);
        else
            printf("%d ", array[i]);
    }
    return (EXIT_SUCCESS);
}

int partition(int size, int array[size], int start, int end){
    int compare = array[end];
    int temp;
    int i = start - 1;
    for(int j = start; j < end; j++){
        if(array[j] <= compare){
            i += 1;
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    temp = array[i+1];
    array[i+1] = array[end];
    array[end] = temp;
    return i+1;
}

void printArray(int size, int a[size]){
    for(int i = 0; i < size; i++){
        if(i != size - 1)
            printf("%d ", a[i]);
        else printf("%d\n", a[i]);
    }
}

