import java.util.Scanner

object Main extends App {

  val sc = new Scanner(System.in)

  val n = sc.nextInt
  val al = Vector.fill(n)(sc.nextInt)

  val res = loop(al)
  println(res)


  def loop(list: Vector[Int]): Vector[Vector[Int]] = {
    val (res1, res2, _) =
      list.tail.foldLeft( (Vector.empty[Vector[Int]], Vector(list.head), list.head) ){ case ( (acc , stack, pre), now) =>
        if(stack.length == 1) (acc, stack :+ now, now)
        else {
          val booby = stack.init.last
          if(
            booby <= pre && pre <= now ||
            booby >= pre && pre >= now
          ) (acc, stack :+ now, now)
          else ( acc :+ stack, Vector(now), now )
        }
      }

    res1 :+ res2
  }
}
