#include <stdio.h>
#define MAX 1000000

int A[MAX];
int partition(int,int);

int main(){
    int x,n;

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

    x = partition(0,n-1);

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

    return 0;
}

int partition(int p,int r){
    int x,i,temp;

    x = A[r];
    i = p - 1;

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

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

    return i + 1;
}


