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 Rust (judge verdict: Accepted)

/**
 *  _           _                 __                            _   _ _   _                                 _                    _                  _
 * | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
 * | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
 * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
 * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
 * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
 *                                                  | |                                                                           | |   | |
 *                                                  |_|                                                                           |_|   |_|
 *
 * https://github.com/hatoo/competitive-rust-snippets
 */
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

#[derive(Debug, Clone)]
struct SkewHeapNode<T: Ord> {
    v: T,
    l: SkewHeap<T>,
    r: SkewHeap<T>,
    length: usize,
}
#[derive(Debug, Clone)]
pub struct SkewHeap<T: Ord>(Option<Box<SkewHeapNode<T>>>);
impl<T: Ord> SkewHeapNode<T> {
    fn swap(&mut self) {
        let &mut SkewHeapNode {
            ref mut l,
            ref mut r,
            ..
        } = self;
        std::mem::swap(l, r);
    }
    fn divide(self) -> (T, SkewHeap<T>, SkewHeap<T>) {
        let SkewHeapNode { v, l, r, .. } = self;
        (v, l, r)
    }
}
impl<T: Ord> SkewHeap<T> {
    pub fn new() -> SkewHeap<T> {
        SkewHeap(None)
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_none()
    }
    pub fn len(&self) -> usize {
        self.0.as_ref().map(|n| n.length).unwrap_or(0)
    }
    pub fn meld(&mut self, mut other: SkewHeap<T>) {
        if other.0.is_none() {
            return;
        }
        if self.0.is_none() {
            *self = other;
            return;
        }
        if self.0.as_ref().unwrap().as_ref().v < other.0.as_ref().unwrap().as_ref().v {
            std::mem::swap(self, &mut other);
        }
        if let Some(ref mut node) = self.0.as_mut() {
            node.length += other.0.as_ref().unwrap().length;
            node.r.meld(other);
            node.swap();
        }
    }
    pub fn push(&mut self, x: T) {
        let n = SkewHeap(Some(Box::new(SkewHeapNode {
            v: x,
            l: SkewHeap::new(),
            r: SkewHeap::new(),
            length: 1,
        })));
        self.meld(n);
    }
    pub fn pop(&mut self) -> Option<T> {
        if let Some(node) = self.0.take() {
            let (v, mut l, r) = node.divide();
            l.meld(r);
            *self = l;
            Some(v)
        } else {
            None
        }
    }
    pub fn peek(&self) -> Option<&T> {
        self.0.as_ref().map(|node| &node.v)
    }
}

#[allow(dead_code)]
fn main() {
    let mut heap = SkewHeap::new();

    util::with_bufwriter(|mut out| {
        let mut line = String::new();
        loop {
            line.clear();
            stdin().read_line(&mut line).unwrap();
            let mut split = line.split_whitespace();
            let command = split.next();

            if command == Some("end") {
                break;
            } else if command == Some("extract") {
                writeln!(out, "{}", heap.pop().unwrap()).unwrap();
            } else {
                let x: u64 = split.next().unwrap().parse().unwrap();
                heap.push(x);
            }
        }
    });
}