classMain{ publicstaticvoidmain(String[] a)throws Exception { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(r.readLine()); String[] str = r.readLine().split(" ");
int[] arr = newint[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); }
arr = buildHeap(arr);
int tmp; for (int i = n - 1; i >= 1; i--) { tmp = arr[0]; arr[0] = arr[i]; arr[i] = tmp; heapify(arr, 0, i); }
StringBuilder s = new StringBuilder(); for (int i = 0; i < n; i++) { s.append(arr[i]); if (i != n - 1) { s.append(" "); } } System.out.println(s); }
privatestaticint[] buildHeap(int[] arr) { int[] result = newint[arr.length]; for (int i = arr.length/2 - 1; i >= 0; i--) { result = heapify(arr, i, arr.length); } return result; }
privatestaticint[] heapify(int[] arr, int index, int maxIndex) { int leftIndex = 2 * index + 1; int rightIndex = 2 * index + 2;
int largestIndex = index; if (leftIndex < maxIndex && arr[leftIndex] > arr[index]) { largestIndex = leftIndex; }