#include <stdio.h>
#include <stdlib.h>

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

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

/*
 * 
 */
int main(int argc, char** argv) {

    int p = 0, n, track; 
    scanf("%d",&n);
    int *A = (int*)malloc(n * sizeof(int));
    for(int a = 0; a < n; a++){
            scanf("%d",&A[a]);
    }
    track = Partition(A,0,n-1);
    
    for(int a = 0; a < n; a++){
        
            if(a == track)printf("[");
            printf("%d",A[a]);
            if(a == track)printf("]");
            if(a < n - 1)printf(" ");
            
    }
    printf("\n");
    return (0);
}
