#include <stdio.h>
#define N 100000

int hi[N];
int partition ( int a, int b);

int main()
{
  int i,n,p;

  scanf ("%d",&n);

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

  p=partition(0,n-1);

  for(i=0;i<n;i++){
    if(i)
      printf(" ");
    if(i==p)
      printf("[");
    printf("%d",hi[i]);
    if(i==p)
      printf("]");
  }
  printf("\n");

  return 0;
}
int partition (int a, int b){
  int i,j,x,y;
x=hi[b];
i=a-1;
for(j=a;j<b;j++){
  if(hi[j]<=x){
    i++;
    y=hi[i];
    hi[i]=hi[j];
    hi[j]=y;
  }
 }

y=hi[i+1];
hi[i+1]=hi[b];
hi[b]=y;

return i+1;
}

