#include <stdio.h>
#define N 100000

void swap(int *,int *);
int partition(int *,int,int);

int main() {
    int n, a[N], i,p,r,key;
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
      }

    p = 0;
    r = n-1;
    key = partition(a, p, r);

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

    return 0;
}

void swap(int *x, int *y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int partition(int a[], int p, int r){
    int i = p-1;        // p = 0
    int j;
    int key = a[r];     // r = n-1

    for (j = p; j < r; j++) {
        if(key >= a[j]) {
            i++;
            swap(&a[j], &a[i]);
        }
    }
    swap(&a[i+1], &a[r]);
    return i+1;
}