#include <stdio.h>

int A[100001];
int n;

int pratition(int *,int,int);

int main(){
    int i,j,q;

    scanf("%d",&n);

    for(i=0;i<n;i++){
      scanf("%d",&A[i]);  
    }
    q = pratition(A,0,n-1);

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

int pratition(int A[],int p,int r){
    int i,j;
    int temp,x;

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

