#include <stdio.h>

int A[100000];

int partition(int p, int r){
    int i = p - 1;
    int x = A[r];
    for(int j = p; j < r; j++){
        if(A[j] <= x){
            i += 1;
            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 n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &A[i]);
    }

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

    printf("\n");
    return 0;
}
