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

int Partition(int *, int, int);

int n;

int main(){
        int *A;
        int i, p, r, q;

        scanf("%d", &n);
        A = malloc(sizeof(int) * n);
        for(i = 0 ; i < n ; i++){
                scanf("%d", &A[i]);
        }
        p = 0;
        r = n - 1;

        q = Partition(A, p, r);

        for(i = 0 ; i < n ; i++){
                if(i == q)printf("[");
                printf("%d", A[i]);
                if(i == q)printf("]");
                if(i + 1 != n)printf(" ");
        }
        printf("\n");

        return 0;
}

int Partition(int *A, int p, int r){
        int x, buf, i, j;
        x = A[r];
        i = p - 1;
        for(j = p  ; j < r ; j++){
                if(A[j] <= x){
                        i = i + 1;
                        buf = A[i];
                        A[i] = A[j];
                        A[j] = buf;
                }
        }
        buf = A[i + 1];
        A[i + 1] = A[r];
        A[r] = buf;
        return i + 1;
}

