#include <stdio.h>
#define N 1000000
int A[N];
int main(){
  int i,n,p=0,a,b;
  
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
  }
  a = A[n-1];

  b = partition(A,p,n-1);

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

int partition(int *A,int p,int r){
  int x,i,j,hozon;
  x = A[r];
  i = p-1;
  for(j = p;j<=r-1;j++){
    if(A[j] <= x){
      i++;
      hozon = A[i];
      A[i] = A[j];
      A[j] = hozon;
    }
  }
  hozon = A[i+1];
  A[i+1] = A[r];
  A[r] = hozon;
  return i+1;
  
}

