Buggy python submission (verdict: Time Limit Exceeded):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    l = left(i)
    r = right(i)
    largest = i
    if l < len(A) and A[largest] < A[l]:
        largest = l
    if r < len(A) and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(range((len(A)-1)/2+1)):
        max_heapify(A,i)

def up_heap(A,i):
    p = parent(i)
    if p >= 0 and A[p] < A[i]:
        A[p],A[i] = A[i],A[p]
        up_heap(A,p)

def extract_max(A):
    ret = A[0]
    A[0] = A[len(A)-1]
    del A[len(A)-1]
    max_heapify(A,0)
    return ret


heap = []
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        heap.append(n)
        up_heap(heap,len(heap)-1)
    elif c[:2] == 'ex':
        print extract_max(heap)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    l = left(i)
    r = right(i)
    largest = i
    if l < len(A) and A[largest] < A[l]:
        largest = l
    if r < len(A) and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(xrange((len(A)-1)/2+1)):
        max_heapify(A,i)

def up_heap(A,i):
    p = parent(i)
    if p >= 0 and A[p] < A[i]:
        A[p],A[i] = A[i],A[p]
        up_heap(A,p)

def extract_max(A):
    ret = A[0]
    A[0] = A[len(A)-1]
    del A[len(A)-1]
    max_heapify(A,0)
    return ret


heap = []
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        heap.append(n)
        up_heap(heap,len(heap)-1)
    elif c[:2] == 'ex':
        print extract_max(heap)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    global pt
    l = left(i)
    r = right(i)
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(xrange((len(A)-1)/2+1)):
        max_heapify(A,i)

def up_heap(A,i):
    p = parent(i)
    if p >= 0 and A[p] < A[i]:
        A[p],A[i] = A[i],A[p]
        up_heap(A,p)

def extract_max(A):
    global pt
    ret = A[0]
    A[0] = A[pt]
    pt -= 1
    max_heapify(A,0)
    return ret


heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        pt += 1
        heap[pt] = n
        up_heap(heap,pt)
    elif c[:2] == 'ex':
        print extract_max(heap)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    global pt
    l = left(i)
    r = right(i)
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(xrange((len(A)-1)/2+1)):
        max_heapify(A,i)

def up_heap(A,i):
    p = parent(i)
    while p >= 0 and A[p] < A[i]:
        A[p],A[i] = A[i],A[p]
        i = p
        p = parent(i)

def extract_max(A):
    global pt
    ret = A[0]
    A[0] = A[pt]
    pt -= 1
    max_heapify(A,0)
    return ret


heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        pt += 1
        heap[pt] = n
        up_heap(heap,pt)
    elif c[:2] == 'ex':
        print extract_max(heap)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

def right(n):
    return (n+1)*2

def max_heapify(A, i):
    global pt
    l = left(i)
    r = right(i)
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

def build_max_heap(A):
    for i in reversed(xrange((len(A)-1)/2+1)):
        max_heapify(A,i)

heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        pt += 1
        heap[pt] = n
        i = pt
        p = (i+1)/2-1
        while p >= 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = (i+1)/2-1
    elif c[1] == 'x':
        ret = heap[0]
        heap[0] = heap[pt]
        pt -= 1
        max_heapify(heap,0)
        print ret
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def max_heapify(A, i, pt):
    l = (i+1)*2-1
    r = (i+1)*2
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest,pt)

heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        n = int(c[7:])
        pt += 1
        heap[pt] = n
        i = pt
        p = (i+1)/2-1
        while p >= 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = (i+1)/2-1
    elif c[1] == 'x':
        ret = heap[0]
        heap[0] = heap[pt]
        pt -= 1
        max_heapify(heap,0,pt)
        print ret
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def max_heapify(A, i, pt):
    l = (i+1)*2-1
    r = (i+1)*2
    largest = i
    if l <= pt and A[largest] < A[l]:
        largest = l
    if r <= pt and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest,pt)

heap = [0]*2000000
pt = -1
while True:
    c = raw_input()
    if c[0] == 'i':
        pt += 1
        heap[pt] = int(c[7:])
        i = pt
        p = (i+1)/2-1
        while p >= 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = (i+1)/2-1
    elif c[1] == 'x':
        print heap[0]
        heap[0] = heap[pt]
        pt -= 1
        max_heapify(heap,0,pt)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[largest] < A[l]:
        largest = l
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
while True:
    c = raw_input()
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
while True:
    c = raw_input()
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def upheap(a,i):
    while i > 0:
        p = (i+1)/2-1
        if a[p] < a[i]:
            a[p],a[i]=a[i],a[p]
        i = p
def heap_insert(a,v):
    a.append(v)
    upheap(a,len(a)-1)
def extract(a):
    ret = a[0]
    a[0],a[len(a)-1]=a[len(a)-1],a[0]
    del a[len(a)-1]
    downheap(a,0)
    return ret
def downheap(a,i):
    lid = (i+1)*2-1
    rid = (i+1)*2
    maxc = -1
    if lid < len(a):
        maxc = lid
    if rid < len(a):
        if maxc == -1 or a[maxc] < a[rid]:
            maxc = rid
    if maxc != -1:
        if a[i] < a[maxc]:
            a[i],a[maxc]=a[maxc],a[i]
            downheap(a,maxc)


a = []
ret = []
while True:
    op = raw_input()
    if op[0] == 'e' and op[1] == 'n':
        break
    elif op[0] == 'i':
        num = int(op.split(' ')[1])
        heap_insert(a,num)
    else:
        ret.append(extract(a))

for r in ret:
    print r

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def heapify(values):
  n = len(values)
  for i in range(1,n):
    upheap(values,i)
    print(' '.join(map(str, values)))

def upheap(values, i):
  while parent(i) >= 0:
    if values[i] > values[parent(i)]:
      values[i], values[parent(i)] = values[parent(i)], values[i]
      i = parent(i)
    else:
      break

def pop(values):
  ret = values[0]
  values[0], values[len(values)-1] = values[len(values)-1], values[0]
  del values[len(values)-1]
  downheap(values)
  return ret

def push(values, x):
  values.append(x)
  upheap(values, len(values)-1)

def downheap(values):
  i = 0
  while left(i) < len(values) or right(i) < len(values):
    l = left(i)
    r = right(i)
    if l >= len(values):
      m = r
    elif r >= len(values):
      m = l
    else:
      m = r
      if values[l] > values[r]:
        m = l
    if values[i] < values[m]:
      values[m], values[i] = values[i], values[m]
    i = m

def parent(i):
  i = i+1
  return i//2-1

def left(i):
  i = i+1
  return i*2-1

def right(i):
  i = i+1
  return i*2

values = []
while True:
  line = input()
  if line.startswith('end'):
    break
  elif line.startswith('extract'):
    print(pop(values))
  elif line.startswith('insert'):
    num = int(line.split(' ')[1])
    push(values, num)

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
def heapify(values):
  n = len(values)
  for i in range(1,n):
    upheap(values,i)
    print(' '.join(map(str, values)))

def upheap(values, i):
  while parent(i) >= 0:
    if values[i] > values[parent(i)]:
      values[i], values[parent(i)] = values[parent(i)], values[i]
      i = parent(i)
    else:
      break

def pop(values):
  ret = values[0]
  values[0], values[len(values)-1] = values[len(values)-1], values[0]
  del values[len(values)-1]
  downheap(values)
  return ret

def push(values, x):
  values.append(x)
  upheap(values, len(values)-1)

def downheap(values):
  i = 0
  while left(i) < len(values) or right(i) < len(values):
    l = left(i)
    r = right(i)
    if l >= len(values):
      m = r
    elif r >= len(values):
      m = l
    else:
      m = r
      if values[l] > values[r]:
        m = l
    if values[i] < values[m]:
      values[m], values[i] = values[i], values[m]
    else:
      break
    i = m

def parent(i):
  i = i+1
  return i//2-1

def left(i):
  i = i+1
  return i*2-1

def right(i):
  i = i+1
  return i*2

values = []
while True:
  line = input()
  if line.startswith('end'):
    break
  elif line.startswith('extract'):
    print(pop(values))
  elif line.startswith('insert'):
    num = int(line.split(' ')[1])
    push(values, num)

Fixed submission:
from sys import stdin

def max_heapify(A, i):
    l = i*2
    r = i*2+1
    largest = i
    if l <= A[0] and A[i] < A[l]:
        largest = l
    else:
        largest = i
    if r <= A[0] and A[largest] < A[r]:
        largest = r

    if largest != i:
        A[largest],A[i] = A[i],A[largest]
        max_heapify(A,largest)

heap = [0]*2000000
for c in stdin:
    if c[0] == 'i':
        heap[0] += 1
        heap[heap[0]] = int(c[7:])
        i = heap[0]
        p = i/2
        while p > 0 and heap[p] < heap[i]:
            heap[p],heap[i] = heap[i],heap[p]
            i = p
            p = i/2
    elif c[1] == 'x':
        print heap[1]
        heap[1] = heap[heap[0]]
        heap[0] -= 1
        max_heapify(heap,1)
    else:
        break