Problem:
Maximum Heap
A binary heap which satisfies max-heap property is called max-heap. In a max-heap, for every node $i$ other than the root, $A[i] \leq A[parent(i)]$, that is, the value of a node is at most the value of its parent. The largest element in a max-heap is stored at the root, and the subtree rooted at a node contains values no larger than that contained at the node itself.
Here is an example of a max-heap.
Write a program which reads an array and constructs a max-heap from the array based on the following pseudo code.
$maxHeapify(A, i)$ move the value of $A[i]$ down to leaves to make a sub-tree of node $i$ a max-heap. Here, $H$ is the size of the heap.
1  maxHeapify(A, i)
2      l = left(i)
3      r = right(i)
4      // select the node which has the maximum value
5      if l ≤ H and A[l] > A[i]
6          largest = l
7      else 
8          largest = i
9      if r ≤ H and A[r] > A[largest]
10         largest = r
11
12     if largest ≠ i　// value of children is larger than that of i
13         swap A[i] and A[largest]
14         maxHeapify(A, largest) // call recursively
The following procedure buildMaxHeap(A) makes $A$ a max-heap by performing maxHeapify in a bottom-up manner.
1 buildMaxHeap(A)
2    for i = H/2 downto 1
3        maxHeapify(A, i)
Input
In the first line, an integer $H$ is given. In the second line, $H$ integers which represent elements in the binary heap are given in order of node id (from $1$ to $H$).
Output
Print values of nodes in the max-heap in order of their id (from $1$ to $H$).
Print a single space character before each value.
Constraint
$1 \leq H \leq 500,000$
$-2,000,000,000 \leq$ value of a node $\leq 2,000,000,000$
Sample Input 1
10
4 1 3 2 16 9 10 14 8 7
Sample Output 1
16 14 10 8 7 9 3 2 4 1
Reference
Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
#define PI 3.14159265358979323846264338327950l
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

// 節点iを根とする部分木がmax-ヒープとなるようA[i]の値をmax-ヒープの葉へ向かって下降させる
void maxHeapify(vector<ll> &A, ll i, ll h) {
    ll left = i * 2;
    ll right = i * 2 + 1;
    // 左の子, 右の子, 自分で値が最大なノードを選ぶ
    ll largest = i;
    if (left <= h and A[left] > A[largest]) {
        largest = left;
    }
    if (right <= h and A[right] > A[largest]) {
        largest = right;
    }
    // iの子の方が大きかったら
    if (largest != i) {
        swap(A[i], A[largest]);
        maxHeapify(A, largest, h);
    }
}

// ボトムアップにmaxHeapifyを適用することで配列Aをmax-ヒープに変換する
void buildMaxHeap(vector<ll> &A, ll h) {
    for (int i = (h/2); i >= 1; i--) {
        maxHeapify(A, i, h);
    }
}


// main
void solve() {
    ll h;
    cin >> h;
    vl A(h+1);
    rep(i,h) {
        cin >> A[i+1];
    }
    buildMaxHeap(A, h);
    rep(i,h) {
        cout << " " << A[i+1];
    }
    cout << endl;

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 7312
final_rank: 20

code_snippet: #include<iostream>
using namespace std;
static const int MAX = 2000000;

int H, A[MAX+1];

void maxHeapify(int i){
    int l, r, largest;
    l = 2 * i;
    r = 2 * i + 1;

    if(l <= H && A[l] > A[i]) largest = l;
    else largest = i;
    if(r <= H && A[r] > A[largest]) largest = r;
    
    if(largest != i){
        swap(A[i],A[largest]);
        maxHeapify(largest);
    }
}

int main(){
    cin >> H;

    for(int i = 1; i <= H; i++) cin >> A[i];

    for(int i = H / 2; i >= 1; i--) maxHeapify(i);

    for(int i = 1; i <= H; i++){
        cout << " " << A[i];
    }
    cout << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 5276
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

void swap(vector<int>& a, int x, int y)
{
    int tmp = a[x];
    a[x] = a[y];
    a[y] = tmp;
}

void max_heapify(vector<int>& a, int h, int i)
{
    int l_id = i*2;
    int r_id = i*2+1;

    int largest_id = i;
    if(l_id <= h && a[l_id] > a[largest_id])
        largest_id = l_id;
    if(r_id <= h && a[r_id] > a[largest_id])
        largest_id = r_id;

    if(largest_id != i)
    {
        swap(a, i, largest_id);
        max_heapify(a, h, largest_id);
    }
}

void build_max_heap(vector<int>& heap, int h)
{
    for(int i = h/2; i >= 1; i--)
        max_heapify(heap, h, i);
}

int main()
{
    int h;
    cin >> h;
    vector<int> heap(h+1);
    for(int i = 1; i <= h; i++)
        cin >> heap[i];

    build_max_heap(heap, h);
    for(int i = 1; i <= h; i++)
        cout << " " << heap[i];
}
accuracy: 1
time_ms: 110
memory_kb: 4784
final_rank: 13

code_snippet: #include <iostream>
using namespace std;
#define MAX 2000000

int H, A[MAX+1];

void maxHeapify(int i) {
    int l, r, largest;
    l = 2 * i;
    r = 2 * i + 1;

    if ( l <= H && A[l] > A[i] ) largest = l;
    else largest = i;
    if ( r <= H && A[r] > A[largest] ) largest = r;

    if ( largest != i ) {
        swap(A[i], A[largest]);
        maxHeapify(largest);
    }
}

int main(){
    cin >> H;

    for ( int i = 1; i <= H; i++ ) cin >> A[i];
    for ( int i = H / 2; i >= 1; i-- ) maxHeapify(i);
    for ( int i = 1; i <= H; i++ ) {
        cout << " " << A[i];
    }
    cout << "\n";

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5144
final_rank: 12

code_snippet: #include<iostream>
using namespace std;
#define MAX 500000

int parent(int k) { return k / 2; }
int left(int k) { return 2 * k; }
int right(int k) { return 2 * k + 1; }

void maxHeapify(int k, int B[], int h) {
    int largest = k;
    if ( left(k) <= h && B[k] < B[left(k)] ) largest = left(k);
    if (  right(k) <= h && B[largest] < B[right(k)] ) largest = right(k);
    if ( largest != k ) {
        int temp = B[k];
        B[k] = B[largest];
        B[largest] = temp;
        maxHeapify(largest, B, h);
    }
}

void buildMaxHeap(int B[], int h) {
    for (int i = h / 2; i > 0; i--) {
        maxHeapify(i, B, h);
    }
}

int main() {
    int H, i, A[MAX+1];
    cin >> H;
    for (i = 1; i <= H; i++) cin >> A[i];

    buildMaxHeap(A, H);
    for (i = 1; i <= H; i++) cout << " " << A[i];
    cout << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 5272
final_rank: 15

code_snippet: #include <stdio.h>
#define max 500001
int n;
int a[max];
void maxHeapify(int i)//从根节点到叶节点方向找a[i]的恰当位置
{
    int l=2*i;
    int r=2*i+1;
    int largest;
    if(l<=n&&a[l]>a[i]){
        largest=l;
    }else largest=i;
    if(r<=n&&a[r]>a[largest]){
        largest=r;
    }
    if(largest!=i){
        int m=a[i];
        a[i]=a[largest];
        a[largest]=m;
        maxHeapify(largest);//largest值被换了，得算一下换完后这里的最大值
    }
    return;
}
void buildMaxHeap(void)//从最底部的根结点到根开始排位置
{
    for(int j=n/2;j>0;j--){
        maxHeapify(j);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    buildMaxHeap();
    for(int i=1;i<=n;i++){
        if(i!=1)printf(" ");
        printf("%d",a[i]);
    }
    printf("\n");
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4872
final_rank: 2

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vi;
    int h;
    std::cin >> h;
    while (h--) {
        int n;
        std::cin >> n;
        vi.push_back(n);
    }
    std::make_heap(vi.begin(), vi.end());
    for (int i = 0; i < vi.size(); ++i) {
        if (i) std::cout << " ";
        std::cout << vi[i];
    }
    std::cout << std::endl;

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5196
final_rank: 14

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

#define parent(x) ((x)>>(1))
#define lc(x) ((x)<<(1))
#define rc(x) ((x)<<(1)|1)

const int MAX = 500010;
int heap[MAX];
int H;

void maxHeapify(int i) {
    int l = lc(i);
    int r = rc(i);
    int largest = i;
    if (l <= H && heap[l] > heap[i]) {
        largest = l;
    }
    if (r <= H && heap[r] > heap[largest]) {
        largest = r;
    }
    if (largest != i) {
        swap(heap[i], heap[largest]);
        maxHeapify(largest);
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &H);
    for (int i = 1; i <= H; i++) {
        scanf("%d", &heap[i]);
    }
    for (int i = H / 2; i >= 1; i--) {
        maxHeapify(i);
    }
    for (int i = 1; i <= H; i++) {
        printf(" %d", heap[i]);
    }
    printf("\n");
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5444
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)


// vectorを出力する
void vecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void newvecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
int minval(vector<int> vec) {
    return *min_element(begin(vec), end(vec));
}
int maxval(vector<int> vec) {
    return *max_element(begin(vec), end(vec));
}

int parent(int i) {
    return i / 2;
}
int left(int i) {
    return 2 * i;
}
int right(int i) {
    return 2 * i + 1;
}

void maxHeapify(vector<int> &A, int i, int h) {
    int l = left(i);
    int r = right(i);
    int largest;
    if (l <= h && A[l] > A[i]) {
        largest = l;
    }
    else {
        largest = i;
    }
    if (r <= h && A[r] > A[largest]) {
        largest = r;
    }
    if (largest != i) {
        swap(A[i], A[largest]);
        maxHeapify(A, largest, h);
    }
}

void buildMaxHeap(vector<int> &A, int h) {
    for (int i = h / 2; i > 0; --i) {
        maxHeapify(A,i,h);
    }
}

void solve() {
    int h;
    cin >> h;
    vector<int> A(h+1);
    for (int i = 1; i <= h; i++) {
        cin >> A[i];
    }
    buildMaxHeap(A, h);
    for (int i = 1; i<=h; i++) {
        cout << " " << A[i];
    }
    cout << endl;

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 5248
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<n; i++)

void maxHeapify(vector<int>& a, int i) {
    int h = a.size()-1;
    int l = 2*i;
    int r = 2*i+1;
    int largest;
    if (l <= h && a[l] > a[i]) largest = l;
    else largest = i;
    if (r <= h && a[r] > a[largest]) largest = r;

    if (largest != i) {
        swap(a[i], a[largest]);
        maxHeapify(a, largest);
    }
}

void buildMaxHeap(vector<int>& a) {
    int h = a.size()-1;
    for (int i=h/2; i>=1; i--) {
        maxHeapify(a, i);
    }
}

int main()
{
    int h;
    cin >> h;
    vector<int> a(h+1);
    rep(i,h) cin >> a[i+1];
    buildMaxHeap(a);

    rep(i,h) cout << a[i+1] << " ";
    cout << endl;

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4720
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm> 
using namespace std;

void maxHeapify(vector<int>& a, int h, int i) {
    int l = i * 2;
    int r = i * 2 + 1;
    int largest;

    if (l <= h && a[i] < a[l]) {
        largest = l;
    } else {
        largest = i;
    }
    if (r <= h && a[largest] < a[r]) {
        largest = r;
    }
    if (largest != i) {
        swap(a[i], a[largest]);
        maxHeapify(a, h, largest);
    }
}

void buildMaxHeap(vector<int>& a, int h) {
    for (int i = h/2; i > 0; i --) {
        maxHeapify(a, h, i);
    }
}

int main(int argc, char const *argv[])
{
    int h;
    cin >> h;
    vector<int> a(h);
    for (int i = 1; i <= h; i ++) {
        cin >> a[i];
    }

    buildMaxHeap(a, h);

    for (int i = 1; i <= h; i ++) {
        cout << " " << a[i];
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4924
final_rank: 7

code_snippet: #include <stdio.h>
#include <algorithm>

using namespace std;

int H;

void maxHeapify(long long array[],int i){
	int left = 2*i;
	int right = 2*i + 1;
	if(left > H) return;
	int largest = i;
	if(right <= H){
		if((array[right] > array[left]) && (array[right] > array[i])){
			largest = right;
		}else if((array[left] > array[right]) && (array[left] > array[i])){
			largest = left;
		}
	}else{
		if(array[i] < array[left]){
			largest = left;
		}
	}
	if(largest != i){
		swap(array[i],array[largest]);
		maxHeapify(array, largest);
	}
}

void buildMaxHeap(long long array[]){
	for(int i = H/2; i >=1; i--) maxHeapify(array,i);
}

int main(){
	scanf("%d",&H);
	long long array[H+1];

	for(int i = 1; i <= H; i++) scanf("%lld",&array[i]);

	buildMaxHeap(array);

	for(int i = 1; i <= H; i++) printf(" %lld",array[i]);
	printf("\n");
}
accuracy: 1
time_ms: 50
memory_kb: 6628
final_rank: 6

code_snippet: #include <iostream>
using namespace std;
#define MAX 500000

int H, A[MAX + 1];

void maxHeapify(int i) {
  int l, r, largest;
  l = 2 * i;
  r = 2 * i + 1;

  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if (r <= H && A[r] > A[largest]) largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}

int main() {
  cin >> H;

  for (int i = 1; i <= H; i++) cin >> A[i];

  for (int i = H / 2; i >= 1; i--) maxHeapify(i);

  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;

  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5388
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
int H;
void maxHeapify(int a[],int i){
    int l=i*2;
    int r=i*2+1;
    int p=i;
    if(l<=H&&a[l]>a[p]) p=l;
    if(r<=H&&a[r]>a[p]) p=r;
    if(p!=i){
        swap(a[p],a[i]);
        maxHeapify(a,p);
    }
}
void buildMaxHeap(int a[]){
    for(int i=H/2;i>=1;i--) maxHeapify(a,i);
}
void solve(){
    cin>>H;
    int a[H+1];
    for(int i=1;i<=H;i++) cin>>a[i];
    buildMaxHeap(a);
    for(int i=1;i<=H;i++) cout<<" "<<a[i];
    cout<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7368
final_rank: 10

code_snippet: #include<iostream>
using namespace std;
#define MAX 2000000

int H, A[MAX + 1];

void maxHeapify(int i) {
    int l, r, largest;
    l = 2 * i;
    r = 2 * i + 1;
    
    if ( l <= H && A[l] > A[i]) largest = l;
    else largest = i;
    if (r <= H && A[r] > A[largest]) largest = r;
    
    if (largest != i) {
        swap(A[i], A[largest]);
        maxHeapify(largest);
    }
}

int main() {
    cin >> H;
    
    for (int i = 1; i <= H; i++) cin >> A[i];
    
    for (int i = H / 2; i >= 1; i--) maxHeapify(i);
    
    for (int i = 1; i <= H; i++) {
        cout << " " << A[i];
    }
    
    cout << endl;
    
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5320
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int H;
    cin >> H;
    vector<int> v;
    int now;
    for (int i = 0; i < H; ++i) {
        int now;
        cin >> now;
        v.push_back(now);
    }
    make_heap(v.begin(), v.end());
    for (auto i : v) cout << " " << i;
}
accuracy: 1
time_ms: 40
memory_kb: 5088
final_rank: 1

code_snippet: #include <iostream>

using namespace std;

int num[500005];
int H;

void maxHeapify(int i){
    int l = i*2;
    int r = i*2 + 1;
    int largest;
    if(l <= H && (num[l] > num[i])){
        largest = l;
    }else{
        largest = i;
    }
    if(r <= H && (num[r] > num[largest])){
        largest = r;
    }
    if(largest != i){
        swap(num[largest], num[i]);
        maxHeapify(largest);
    }

}


void buildMaxHeap(){
    for(int i = H / 2; i >= 1; i--){
        maxHeapify(i);
    }
}

int main(){
    cin >> H;
    for (int i = 1; i <= H; i++){
        cin >> num[i];
    }
    buildMaxHeap();
    for (int i = 1; i <= H; i++){
        cout << ' ' << num[i];
    }
}
accuracy: 1
time_ms: 110
memory_kb: 5300
final_rank: 19

code_snippet: #include <iostream>

using namespace std;

int num[500005];
int H;

void maxHeapify(int i){
    int l = i*2;
    int r = i*2 + 1;
    int largest;
    if(l <= H && (num[l] > num[i])){
        largest = l;
    }else{
        largest = i;
    }
    if(r <= H && (num[r] > num[largest])){
        largest = r;
    }
    if(largest != i){
        swap(num[largest], num[i]);
        maxHeapify(largest);
    }

}


void buildMaxHeap(){
    for(int i = H / 2; i >= 1; i--){
        maxHeapify(i);
    }
}

int main(){
    cin >> H;
    for (int i = 1; i <= H; i++){
        cin >> num[i];
    }
    buildMaxHeap();
    for (int i = 1; i <= H; i++){
        cout << num[i] << ' ';
    }
}
accuracy: 1
time_ms: 100
memory_kb: 5084
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int h[500001];
int n;



void maxHeapify(int i){
    int l = 2*i;
    int r = 2*i + 1;
    int largest;
    if(l <= n && h[l] > h[i])
        largest = l;
    else
        largest = i;
    if(r <= n && h[r] > h[largest])
        largest = r;
    if(largest != i){
        swap(h[i], h[largest]);
        maxHeapify(largest);
    }
}

int main()
{
    cin>>n;
    for(int i = 1; i <= n; ++i){
        int num;
        cin>>num;
        h[i] = num;
    }

    for (int i = n/2; i >= 1; --i)
       maxHeapify(i);

    for(int i = 1; i <= n; ++i){
        cout<<' '<<h[i];
    }
}
accuracy: 1
time_ms: 100
memory_kb: 5124
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
	int n;
	cin >> n;
	
	vector<int> v;
	for(int i = 0; i < n; i++) {
		int x;
		cin >> x;
		
		v.push_back(x);
	}
	
	make_heap(v.begin(), v.end());
	
	for(int i = 0; i < n - 1; i++) {
		cout << v[i] << " ";
	}
	cout << v.back() << endl;
	
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5076
final_rank: 8