let partition (a : int array) p r =
  let swap i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp in
  let rec doit i j =
    if j = r then (swap i r; i)
    else if a.(j) <= a.(r) then (swap i j; doit (i + 1) (j + 1))
    else doit i (j + 1) in
  doit p p

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> Scanf.scanf "%d " (fun i -> i)) in
  let k = partition a 0 (n - 1) in
  print_int a.(0);
  for i = 1 to n - 1 do
    if i = k then (print_string " ["; print_int a.(i); print_string "]")
    else (print_string " "; print_int a.(i))
  done;
  print_newline ()