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

int partition(int *A,int r);

int main(int argc, char** argv) {
    int size;
    scanf("%d",&size);
    int array[size+1];
    for(int i= 1; i<size+1;i++)
    {
        scanf("%d", &array[i]);
    }
     for(int i= 1; i<size+1;i++)
    {
        printf("%d ", array[i]);
    }
    int number;
    number = partition( array, size);
    int a= array[size];
    
    array[size]=array[number];
    array[number]=a;
     for(int i= 1; i<size+1;i++)
    {
        if(i!=number&& i!=size)
            printf("%d ", array[i]);
        if(i==number&&i!=size)
            printf("[%d] ",array[i]);
        if(i==size&&i!=number)
            printf("%d\n",array[i]);
        if(i==size && i== number)
            printf("[%d]\n",array[i]);
        
    }
    return (EXIT_SUCCESS);
}

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

