def maxHeapify(h, a, i)
  l = left(i)
  r = right(i)
  # 左の子、自分、右の子で値が最大のノードを選ぶ
  if l <= h && a[l] > a[i]
    largest = l
  else
    largest = i
  end

  if r <= h && a[r] > a[largest]
    largest = r
  end

  if largest != i # i の子の方が値が大きい場合
    a[i],a[largest] = a[largest],a[i]
    maxHeapify(h, a, largest) # 再帰的に呼び出し
  end
end

def buildMaxHeap(h, a)
  (h/2).downto(1){|i|
    maxHeapify(h, a, i)
  }
end

def right(i);
  (i*2)
end

def left(i)
  (i*2+1)
end

h = gets.to_i
a = [0] + gets.split.map(&:to_i)

buildMaxHeap(h, a)

puts " "+a[1..-1]*" "


