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"); } }

var PairingHeapNode = function PairingHeapNode(val) {
  _classCallCheck(this, PairingHeapNode);

  this.value = val;
  this.head = null;
  this.next = null;
};

function merge(a, b, comp) {
  if (!(a && b)) return a || b;
  if (comp(a.value, b.value)) {
    ;
    var _ref = [b, a];
    a = _ref[0];
    b = _ref[1];
  }b.next = a.head;
  a.head = b;
  return a;
}

function mergeList(s, comp) {
  var n = new PairingHeapNode(null);
  while (s) {
    var a = s,
        b = null;
    s = s.next;a.next = null;
    if (s) {
      b = s;s = s.next;b.next = null;
    }
    a = merge(a, b, comp);a.next = n.next;
    n.next = a;
  }
  while (n.next) {
    var j = n.next;
    n.next = n.next.next;
    s = merge(j, s, comp);
  }
  return s;
}

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

    this.comp = comp || function (a, b) {
      return a < b;
    };
    this.root = null;
  }

  _createClass(PairingHeap, [{
    key: "push",
    value: function push(val) {
      this.root = merge(this.root, new PairingHeapNode(val), this.comp);
      return this;
    }
  }, {
    key: "top",
    value: function top() {
      return this.root ? this.root.value : null;
    }
  }, {
    key: "pop",
    value: function pop() {
      if (!this.root) return null;
      var ret = this.root.value;
      this.root = mergeList(this.root.head, this.comp);
      return ret;
    }
  }, {
    key: "empty",
    value: function empty() {
      return !!this.root;
    }
  }]);

  return PairingHeap;
})();


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

(function solve(lines){
  var pq = new PairingHeap();
  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);