#include <stdio.h>
#include <string.h>

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

int main(void){
    int n;
    scanf("%d", &n);
    
    int A[n];
    for(int i=0 ; i<n; i++) scanf("%d", &A[i]);
    int p = partition(A, n-1);
    for(int i=0 ; i<p; i++) printf("%d ", A[i]);
    printf("[%d] ", A[p]);
    for(int i=p+1 ; i<n-1; i++) printf("%d ", A[i]);
    printf("%d\n", A[n-1]);

    return 0;
}
