#include <iostream>
#include <vector>

using namespace std;
using int_vec = vector<int>;
using Iterator = int_vec::iterator;


Iterator partition(Iterator begin, Iterator end, int x) {
    for (auto it = begin; it != end; ++it) {
        if (*it <= x) {
            swap(*begin++, *it);
        }
    }
    return begin-1;
}


int main() {
    int n;
    cin >> n;

    int_vec v;
    for (auto i = 0; i < n; ++i) {
        int x;
        cin >> x;
        v.push_back(x);
    }

    auto mid = partition(v.begin(), v.end(), v.back());
    auto it = v.begin();
    for (; it != v.end()-1; ++it) {
        if (it == mid) {
            cout << "[" << *it << "]" << " ";
        } else {
            cout << *it << " ";
        }
    }
    cout << *it << endl;

    return 0;
}
#include<iostream> 
#include<string>
#include<queue>
#include<algorithm> 
#include<cstdio>              
#include<vector>
#include<stack>
#include<map>
#include<iterator>
#include<cstdlib>
#include<cmath>
using namespace std; 
#define MAX 100000
#define SENTINEL 2000000000

int A[MAX],n;

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

int main()
{
	int i,q;
	scanf("%d",&n);
	for(i=0;i<n;i++) scanf("%d",&A[i]);
	
	q = partition(0,n-1);
	for(i=0;i<n;i++)
	{
		if(i) printf(" ");
		if(i==q)printf("[");
		printf("%d",A[i]);
		if(i==q)printf("]");
	}
	printf("\n");
	
	return 0;
}