#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int partition(int a[], int p, int r){
    int i = p - 1;
    int x = a[r];
    for(int j = p; j <= r-1; j++){
        if(a[j] <= x){
            i++;
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    int t = a[i+1];
    a[i+1] = a[r];
    a[r] = t;
    return i+1;
}
int main(){
    int n;
    scanf("%d", &n);
    int a[n];
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);

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

        }
        else{
            if(i == q){
                printf("[%d] ", a[i]);
            } else{
                printf("%d ", a[i]);
            }

        }
    }
    return 0;
}
