#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define ABS(x) ( (x)>0 ? x : -(x) )
#define MIN(x,y) ( (x) < (y) ? (x) : (y) )
#define MAX(x,y) ( (x) > (y) ? (x) : (y) )

#define INF 1000000000



//typedef long long ll;

void swap(int* a, int* b)
{
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

/*

int binary_search(int* a, int n,int key)
{
	int low, middle, high;

	low = 0, high = n - 1;

	while (low <= high)
	{
		middle = (low + high) / 2;

		if (key < a[middle])
			high = middle - 1;
		else if (key > a[middle])
			low = middle + 1;
		else
			return 1;
	}
	return 0;
}

void reverse(int a[], int b, int e)
{
	int i;

	for (i = 0; i <= (e-b-1) / 2; i++)
	{
		swap(&a[b+i], &a[e - 1 - i]);
	}
}

void CountingSort(int A[],int k,int n)
{
	int B[2000000] = { 0 };

	int i, C[20000] = { 0 };

	for (i = 0; i < n; i++)
		C[A[i]]++;

	for (i = 0; i < k; i++)
		C[i + 1] += C[i];

	for (i = n - 1; i >= 0; i--)
		B[--C[A[i]]] = A[i];

	for (i = 0; i < n; i++)
	{
		printf("%d", B[i]);
		if (i != n - 1)
			printf(" ");
	}
	printf("\n");
}
*/

int partion(int A[], int p, int r)
{
	int x = A[r-1];
	int i, j;

	i = p - 1;

	for (j = p; j < r;j++)
	{
		if (A[j] <= x)
		{
			i++;
			swap(&A[i], &A[j]);
		}
	}
	//swap(&A[i++], &A[r-1]);

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

int main()
{
	int n;
	int A[100000];

	scanf("%d", &n);

	int i;

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

	partion(A, 0, n);



}
