object Main extends App {
  loop(EmptyHeap)

  def loop(heap: LeftistHeap): Unit = {
    val op: Array[String] = io.StdIn.readLine().split(" ")
    op(0) match {
      case "end" =>
      case "insert" => loop(heap.insert(op(1).toInt))
      case "extract" =>
        println(heap.findMax)
        loop(heap.deletedMax)
      case _ => throw new Error("Unknown operation")
    }
  }

  sealed trait LeftistHeap {
    def rank: Int
    def merge(that: LeftistHeap): LeftistHeap
    def insert(x: Int): LeftistHeap = merge(HeapNode(1, x, EmptyHeap, EmptyHeap))
    def findMax: Int
    def deletedMax: LeftistHeap
  }
  case object EmptyHeap extends LeftistHeap {
    def rank: Int = 0
    def merge(that: LeftistHeap): LeftistHeap = that
    def findMax: Int = throw new Error("Heap is Empty")
    def deletedMax: LeftistHeap = EmptyHeap
  }
  case class HeapNode(rank: Int, elem: Int, left: LeftistHeap, right: LeftistHeap) extends LeftistHeap {
    def findMax: Int = elem
    def deletedMax: LeftistHeap = left.merge(right)
    def merge(that: LeftistHeap): LeftistHeap = {
      def make(x: Int, a: LeftistHeap, b: LeftistHeap): LeftistHeap = {
        if (a.rank >= b.rank) HeapNode(b.rank + 1, x, a, b)
        else HeapNode(a.rank + 1, x, b, a)
      }
      that match {
        case EmptyHeap => this
        case HeapNode(_, y, l2, r2) =>
          if (elem > y) make(elem, left, that.merge(right))
          else make(y, l2, r2.merge(this))
      }
    }
  }
}
