/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.c
 * Author: Ivan
 *
 * Created on July 3, 2018, 11:31 AM
 */

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

/*
 * 
 */

int partition(int A[], int p, int r);

int main(int argc, char** argv) {
    int n;
    scanf("%d", &n);
    int A[n];
    for (int i = 0; i < n; i++) scanf("%d", &A[i]);
    
    int q = partition(A, 0, n-1);
    
    for (int ix = 0; ix < n; ix++)
    {
        if (ix) printf(" ");
        if (ix == q) printf("[");
        printf("%d", A[ix]);
        if (ix == q) printf("]");
    }
    
    printf("\n");
    
    return (EXIT_SUCCESS);
}

int partition(int A[], int p, int r)
{
    int temp;
    int x = A[r];
    int i = p - 1;
    for (int j = p; j < r; j++)
    {
        if (A[j] <= x)
        {
            i++;
            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;
}
