#include <stdio.h>
#include <stdlib.h>
#define MAX 100000
#define VMAX 100000

int P(int * , int , int);

int main(){
    int *A;
    int n, i, q;

    scanf("%d", &n);

    A = malloc(sizeof(int)*(n+1));

    /* your code */
    for(i = 1; i <= n; i++){
        scanf("%d",&A[i]);

    }

    q = P(A, 1, n);

    for(i = 1; i < n; i++){
        if(i == q) printf("[%d] ",A[i]);
        
        else printf("%d ",A[i]);
    }


    if(i == q) printf("[%d]\n",A[i]);
    else printf("%d\n",A[i]);

    free(A);




  return 0;
}

int P(int *A, int p, int r){
    int x, i, j, k, a;

    x = A[r];
    i = p-1;

    for(j = p; j < r; j++){
        if(A[j] <= x){
            i++;
            a = A[j];
            A[j] = A[i];
            A[i] = a;
        }
    }

    a = A[r];
    A[r] = A[i+1];
    A[i+1] = a; 

    return i+1;
}
