#include <stdio.h>

#define MAXSIZE 100000


int Partition(int A[], int p, int r){
    int x = A[r];
    int i = p - 1;
    int j, t;

    for(j = p; j < r; j++){
        if(A[j] <= x){
            i++;
            t = A[i];
            A[i] = A[j];
            A[j] = t;
        }
    }

    t = A[i + 1];
    A[i + 1] = A[r];
    A[r] = t;

    return i + 1;

}

int main(){

    int A[MAXSIZE];
    int N;
    int i;
    int q; //index of pivot

    scanf("%d", &N);
    for(i = 0; i < N; i++) scanf("%d", &A[i]);

    q = Partition(A, 0, N -1);

    for(int i = 0; i < N; i++){
        if(i > 0) printf(" ");
        if(i == q){
            printf("[%d]", A[i]);
        }else{
            printf("%d", A[i]);
        }
    }
    printf("\n");


    return 0;
}

