#include <stdio.h>
#define MAX_LENGTH 100000
int Arr[MAX_LENGTH]={0};
int n=0;
int b = 0;
int Partition(int Arr[], int p, int r)
{
    int x=Arr[r];
    int i=p-1;
    for(int j=p;j<r;j++)
    {
        if(Arr[j]<=x)
        {
            i+=1;
            int temp=Arr[i];
            Arr[i]=Arr[j];
            Arr[j]=temp;
        }
    }
    int t = Arr[i+1];
    Arr[i+1]=Arr[r];
    Arr[r]=t;
    return i+1;
}
int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    for(int i=0;i<n;i++){scanf("%d ", &Arr[i]);}
    b=Partition(Arr, 0, n-1);
    for(int i=0;i<n;i++)
    {
        if(i==n-1){printf("%d\n", Arr[i]);}
        else if(i==b){printf("[%d] ", Arr[i]);}
        else{printf("%d ", Arr[i]);}
    }
    return 0;
}

