#include<stdio.h>

int Partition(int A[], int p, int r)
{
  int w;
  int x = A[r];
  int i = p-1, j, k;
  for(j = p; j < r; j++){
    if(A[j] <= x){
		i = i+1;
		w = A[i];
		A[i] = A[j];
		A[j] = w;
	}
  }
  w = A[i+1];
  A[i+1] = A[r];
  A[r] = w;
  return(i+1);
}

int main()
{
	int A[100000];
	int n, i, p;

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

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

	return(0);
}