#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

//============================================================================
#define pred(x) ((x)-1)
#define succ(x) ((x)+1)
//============================================================================
//----------------------------------------------------------------------------
void exchange(long *x, long *y)
{
    long z;
    z=*x, *x=*y, *y=z;
}
//----------------------------------------------------------------------------
long Partition(long *A, long p,  long r)
{
    long i,j,x;

    x = A[r];
    i = p-1;
    for (j = p; j<r; j++)
        while (A[j] <= x){
            i++;
            exchange(&A[i], &A[j]);
        }
    exchange(&A[i+1], &A[r]);
    return i+1;
}
//----------------------------------------------------------------------------
/*
12
13 19 9 5 12 8 7 4 21 2 6 11
*/
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void input(FILE *file)
{
    long i,n,x; long *A;

    fscanf(file, "%ld", &n);
    A = malloc(succ(n)*sizeof(long));
    for (i=0; i<n; i++){
        fscanf(file, "%ld", &A[i]);
    }
    x=Partition(A, 0, pred(n));
    for (i=0; i<n; i++){
        if (i) printf(" ");
        if (i==x)
            printf("[%ld]", A[i]);
        else
            printf("%ld", A[i]);
    }
    printf("\n");
    free(A);
}
//============================================================================
#ifndef DESKTOP
int main()
{
    input(stdin);
    return 0;
}
#endif