Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Priority Queue

A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:

$insert(S, k)$: insert an element $k$ into the set $S$

$extractMax(S)$: remove and return the element of $S$ with the largest key

Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$.
The priority queue manages a set of integers, which are also keys for the priority.

Input

Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $k$ represents an integer element to be inserted to the priority queue.

The input ends with "end" operation.

Output

For each "extract" operation, print the element extracted from the priority queue $S$ in a line.

Constraints

The number of operations $\leq 2,000,000$

$0 \leq k \leq 2,000,000,000$

Sample Input 1

insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end

Sample Output 1

8
10
11
2

Solution in JavaScript (judge verdict: Accepted)

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _parent(i) {
  return ~ ~(i / 2);
}
function _right(i) {
  return 2 * i + 1;
}
function _left(i) {
  return 2 * i;
}

var BinaryHeap = (function () {
  function BinaryHeap(comp) {
    _classCallCheck(this, BinaryHeap);

    this.comp = comp || function (a, b) {
      return a < b;
    };
    this.collection = [];
  }

  _createClass(BinaryHeap, [{
    key: "from",
    value: function from(array) {
      this.collection = array;
      for (var i = ~ ~(array.length / 2); i >= 0; --i) this.heapify(this.collection, i);
      return this;
    }
  }, {
    key: "toArray",
    value: function toArray() {
      return this.collection.sort(this.comp);
    }
  }, {
    key: "heapify",
    value: function heapify(arr, i) {
      var l = _left(i);
      var r = _right(i);
      var largest;

      if (l < arr.length && this.comp(arr[i], arr[l])) largest = l;else largest = i;
      if (r < arr.length && this.comp(arr[largest], arr[r])) largest = r;

      if (largest !== i) {
        var t = arr[i];
        arr[i] = arr[largest];
        arr[largest] = t;
        return this.heapify(arr, largest);
      }
    }
  }, {
    key: "push",
    value: function push(value) {
      this.collection.push(value);
      var arr = this.collection;
      for (var i = arr.length - 1; i > 0 && this.comp(arr[_parent(i)], arr[i]); i = _parent(i)) {
        var tmp = arr[i];
        arr[i] = arr[_parent(i)];
        arr[_parent(i)] = tmp;
      }
      return this;
    }
  }, {
    key: "top",
    value: function top() {
      return this.collection[0];
    }
  }, {
    key: "pop",
    value: function pop() {
      var ret = this.collection[0];
      if (1 < this.collection.length) {
        this.collection[0] = this.collection.pop();
        this.heapify(this.collection, 0);
      } else {
        this.collection.pop();
      }
      return ret;
    }
  }]);

  return BinaryHeap;
})();


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

(function solve(lines){
  var pq = new BinaryHeap();
  lines.forEach(function(line){
    words = line.split(" ");
    if(words[0] === "insert"){
      pq.push(parseInt(words[1]));
    }else if(words[0] === "extract"){
      console.log(pq.pop());
    }
  });
})(lines);