Buggy python submission (verdict: Time Limit Exceeded):
import sys


class QueueEmptyError(Exception):
    pass


class PriorityQueue:
    def __init__(self):
        self._nodes = []
        self.size = 0

    def add(self, v):
        self._nodes.append(v)
        self.size += 1

        self._build_up(self.size-1)

    def extract(self):
        if self.size == 0:
            raise QueueEmptyError()

        node = self._nodes[0]
        self.size -= 1
        last = self._nodes.pop()
        if self.size > 0:
            self._nodes[0] = last
            self._build_down(0)

        return node

    def _build_down(self, i):
        top = i
        left = (i+1) * 2 - 1
        right = (i+1) * 2
        if left < self.size and self._nodes[left] > self._nodes[top]:
            top = left
        if right < self.size and self._nodes[right] > self._nodes[top]:
            top = right

        if top > i:
            self._nodes[top], self._nodes[i] = \
                self._nodes[i], self._nodes[top]
            self._build_down(top)

    def _build_up(self, i):
        if i == 0:
            return

        parent = (i-1) // 2
        if self._nodes[parent] < self._nodes[i]:
            self._nodes[parent], self._nodes[i] = \
                    self._nodes[i], self._nodes[parent]
            self._build_up(parent)


def run():
    q = PriorityQueue()

    for line in sys.stdin:
        if line.startswith("insert"):
            q.add(int(line[7:]))
        elif line.startswith("extract"):
            print(q.extract())
        elif line.startswith("end"):
            break
        else:
            ValueError("invalid command")


if __name__ == '__main__':
    run()



Fixed submission:
import sys


def run():
    q = []

    for line in sys.stdin:
        if line.startswith("i"):
            v = int(line[7:])
            q.append(v)
            i = len(q) - 1
            while i > 0:
                p = (i-1) // 2
                vp = q[p]
                if vp < v:
                    q[p], q[i] = v, vp
                    i = p
                else:
                    break
        elif line.startswith("ex"):

            size = len(q) - 1
            if size < 0:
                raise ValueError('get from empty queue')

            v = q[0]
            last = q.pop()
            if size > 0:
                q[0] = last
                i = 0
                while i < size // 2:
                    top, vt = i, last
                    left = (i+1) * 2 - 1
                    right = (i+1) * 2
                    if left < size:
                        vl = q[left]
                        if vl > vt:
                            top, vt = left, vl
                    if right < size:
                        vr = q[right]
                        if vr > vt:
                            top, vt = right, vr

                    if top > i:
                        q[top], q[i] = last, vt
                        i = top
                    else:
                        break
            print(v)
        elif line.startswith("end"):
            break
        else:
            ValueError("invalid command")


if __name__ == '__main__':
    run()

Buggy python submission (verdict: Time Limit Exceeded):
import sys


class QueueEmptyError(Exception):
    pass


class PriorityQueue:
    def __init__(self):
        self._nodes = []

    def add(self, v):
        self._nodes.append(v)
        i = len(self._nodes) - 1
        while i > 0:
            parent = (i-1) // 2
            vp, vi = self._nodes[parent], self._nodes[i]
            if vp < vi:
                self._nodes[parent], self._nodes[i] = vi, vp
                i = parent
            else:
                break

    def extract(self):
        size = len(self._nodes) - 1
        if size < 0:
            raise QueueEmptyError()

        node = self._nodes[0]
        last = self._nodes.pop()
        if size > 0:
            self._nodes[0] = last
            i = 0
            while i < size // 2:
                top = i
                left = (i+1) * 2 - 1
                right = (i+1) * 2
                vi = self._nodes[top]
                vt = vi
                if left < size:
                    vl = self._nodes[left]
                    if vl > vt:
                        top = left
                        vt = vl
                if right < size:
                    vr = self._nodes[right]
                    if vr > vt:
                        top = right
                        vt = vr

                if top > i:
                    self._nodes[top], self._nodes[i] = vi, vt
                    i = top
                else:
                    break

        return node


def run():
    q = PriorityQueue()

    for line in sys.stdin:
        if line.startswith("i"):
            q.add(int(line[7:]))
        elif line.startswith("ex"):
            print(q.extract())
        elif line.startswith("end"):
            break
        else:
            ValueError("invalid command")


if __name__ == '__main__':
    run()



Fixed submission:
import sys


def run():
    q = []

    for line in sys.stdin:
        if line.startswith("i"):
            v = int(line[7:])
            q.append(v)
            i = len(q) - 1
            while i > 0:
                p = (i-1) // 2
                vp = q[p]
                if vp < v:
                    q[p], q[i] = v, vp
                    i = p
                else:
                    break
        elif line.startswith("ex"):

            size = len(q) - 1
            if size < 0:
                raise ValueError('get from empty queue')

            v = q[0]
            last = q.pop()
            if size > 0:
                q[0] = last
                i = 0
                while i < size // 2:
                    top, vt = i, last
                    left = (i+1) * 2 - 1
                    right = (i+1) * 2
                    if left < size:
                        vl = q[left]
                        if vl > vt:
                            top, vt = left, vl
                    if right < size:
                        vr = q[right]
                        if vr > vt:
                            top, vt = right, vr

                    if top > i:
                        q[top], q[i] = last, vt
                        i = top
                    else:
                        break
            print(v)
        elif line.startswith("end"):
            break
        else:
            ValueError("invalid command")


if __name__ == '__main__':
    run()