#include <stdio.h>

void Swap(int* x, int* y){
    int tmp;
    tmp = *x;
    *x = *y;
    *y = tmp;
}

int partition(int A[], int p, int r){
    int x = A[r];
    int i = p - 1;
    int tmp;
    for(int j = p; j < r; j++){
        if(A[j] <= x){
            i++;
            Swap(&A[i], &A[j]);
        }
    }
    Swap(&A[i+1], &A[r]);
    return i+1;
}

int main(){
    int n, i;
    scanf("%d", &n);
    int A[n];
    for(i = 0; i < n; i++){
        scanf("%d", &A[i]);
    }
    int q = partition(A, 0, n-1);
    for(i = 0; i < n; i++){
        if(i != 0) printf(" ");
        if(i == q) printf("[");
        printf("%d", A[i]);
        if(i == q) printf("]");
    }
    printf("\n");
    return 0;
}
