//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_B&lang=jp
#include <iostream>
using namespace std;

void swap(int A[],int i,int j){
  int buf = A[j];
  A[j] = A[i];
  A[i] = buf;
}

int partition(int A[], int l, int r){
  const int x = A[r]; //????????¨?????????
  int i = l-1; //??§???????????????????????¨??????????????????-1
  for(int j = l; j < r; j++){
    //j?????????????????£???????????¨??????????????????????????¨????????????
     if (A[j] <= x){
       i += 1;
       //??´??????????????????????????¨???
       swap(A,i,j);
     }
  }
  swap(A,i+1,r);
  return i+1;
}


int main(){
  int n;
  cin >> n;
  int S[n];
  for(int i=0; i<n; i++){
    cin >> S[i];
  }//????????????????????????

  int mark = partition(S,0,n-1);
  for(int i=0; i<mark; i++){
    cout << S[i] << " ";
  }
  cout << "[" << S[mark] << "] ";
  for(int i=mark+1; i<n-1; i++){
    cout << S[i] << " ";
  }
  cout << S[n-1] << endl;
  return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long lli;
typedef vector<lli> vll;

lli partition(vll& a,lli p,lli r){
	lli x = a[r];
	lli i = p-1;
	for(lli j = p;j < r;j++){
		if(a[j] <= x){
			i++;
			swap(a[j],a[i]);
		}
	}
	swap(a[i+1],a[r]);
	return i+1;
}

lli n;
vll a;

int main(){
	cin >> n;
	a = vll(n);
	for(lli i = 0;i < n;i++) cin >> a[i];
	lli p = partition(a,0,n-1);
	for(lli i = 0;i < n;){
		if(i == p) cout << "[";
		cout << a[i];
		if(i == p) cout << "]";
		if(++i < n) cout << " ";
		else cout << endl;
	}
	return 0;
}