#include "bits/stdc++.h"
using namespace std;
template<typename Itr>
auto partition2(const Itr first, const Itr last){
  auto i=first;
  for(auto j=first; j!=last; ++j){
    if(*j<=*last){
      iter_swap(i, j);
      ++i;
    }
  }
  iter_swap(i, last);
  return i;
}
int32_t main(){
  size_t n; cin>>n;
  vector<uint32_t> a(n); for(auto&& e:a) cin>>e;
  const auto last=--end(a);
  auto mid=partition2(begin(a), last);
  for(auto i=begin(a); i!=mid; ++i) cout<<*i<<' ';
  cout<<'['<<*mid<<"] ";
  for(auto i=next(mid); i!=last; ++i) cout<<*i<<' ';
  cout<<*last<<'\n';
}

#include <iostream>
#define MAX 100001
using namespace std;

void Partition(int A[], int n, int& l) // l is the last index of smaller part
{
    int x = A[n-1];
    for (int i = 0; i < n-1; ++i) 
    {
        if (A[i] <= x) 
        {
            swap(A[i], A[l+1]);
            l++;
        }
    }
    l++;
    swap(A[l], A[n-1]);
}

int main() 
{
    int n, A[MAX];
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) scanf("%d", &A[i]);
    int l = -1;
    Partition(A, n, l);

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