using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{    

    
    class Program
    {
        static int n;
        static long[] A;

        static int Parent(int i)
        {
            return i / 2;
        }

        static int Left(int i)
        {
            return 2 * i;
        }
        
        static int Right(int i)
        {
            return 2 * i + 1;
        }

        static void MaxHeap(long[] A)
        {

            int p;
            long tmp;

            for(int i = 0; ; i++)
            {
                p = n / 2 - i;
                tmp = 0;   
                
                if(p == 0)
                {
                    break;
                }

                while (true)
                {
                                        
                    //両方もち
                    if(Right(p) <= n)
                    {                     
                        if (A[Left(p)] > A[Right(p)])
                        {
                            if (Left(p) <= n && A[p] < A[Left(p)])
                            {                               
                                tmp = A[p];
                                A[p] = A[Left(p)];
                                A[Left(p)] = tmp;                               
                                p = Left(p);                              
                            }
                            else
                            {                               
                                break;
                            }
                        }
                        else
                        {
                            if (A[Right(p)] > A[p])
                            {                               
                                tmp = A[p];
                                A[p] = A[Right(p)];
                                A[Right(p)] = tmp;                                
                                p = Right(p);
                            }
                            else
                            {                              
                                break;
                            }
                        }

                    }
                    //右なし
                    else
                    {                       
                        if (Left(p) <= n && A[p] < A[Left(p)])
                        {                           
                            tmp = A[p];
                            A[p] = A[Left(p)];
                            A[Left(p)] = tmp;                           
                            p = Left(p);
                        }
                        else
                        {                           
                            break;
                        }
                    }
                    
                    
                }
                
            }            

        }

        static void Main(string[] args)
        {
            n = int.Parse(Console.ReadLine());
            A = new long[n + 1];

            string[] input = Console.ReadLine().Split();

            for(int i = 1; i <= n; i++)
            {
                A[i] = long.Parse(input[i - 1]);
            }

            MaxHeap(A);

            for(int i = 1; i <= n; i++)
            {
                Console.Write($" {A[i]}");
            }

            Console.WriteLine();

        }
    }
}



Measured memory_bytes: 55516