Time Limit Exceed about max heap
<p>I want to design a function to find the k biggest elements in a unordered set of N elements with time complexity: <code>Θ(N+klogN)</code> on an online judge.</p>

<p>here is a sample:</p>

<hr>

<p>Input</p>

<pre><code>LN 1 : N K

LN 2 : N numbers
</code></pre>

<p>Output</p>

<pre><code>LN 1 : K biggest number

LN 2 : Final heap
</code></pre>

<p>Sample Input</p>

<pre><code>10 4
17 19 26 37 30 11 5 29 32 1
</code></pre>

<p>Sample Output</p>

<pre><code>29
26 19 11 17 1 5
</code></pre>

<hr>

<p>And here is my code:</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

int main(){

    int i,j,rc,temp,temp1,length,K;
    cin&gt;&gt;length&gt;&gt;K;
    int *heap = new int[length];

    for(i=0;i&lt;length;i++) cin&gt;&gt;heap[i];
    for(i=length/2-1;i&gt;=0;i--){                  //build a max heap first with Θ(N)
        while(!((i&gt;=length/2)&amp;&amp;(i&lt;length))){
            j = 2*i+1;
            rc = 2*i+2;
            if((rc&lt;length)&amp;&amp;(heap[j]&lt;heap[rc])) j=rc;
            if(heap[i]&gt;heap[j]) break;
            temp = heap[i];
            heap[i]=heap[j];
            heap[j]=temp;
            i=j;
        }
    }
    int k,n=length;
    for(k=0;k&lt;K;k++){                         //shiftdown k times to find k biggest 
        temp1=heap[--n];                      //numbers with Θ(klogN)
        heap[n] = heap[0];
        heap[0] = temp1;
        if(n!=0) {
            i=0;
                while(!((i&gt;=n/2)&amp;&amp;(i&lt;n))){
                     j = 2*i+1;
                    rc = 2*i+2;
                    if((rc&lt;n)&amp;&amp;(heap[j]&lt;heap[rc])) j=rc;
                    if(heap[i]&gt;heap[j]) break;
                    temp = heap[i];
                    heap[i]=heap[j];
                    heap[j]=temp;
                    i=j;
                }
            }
        }


    cout&lt;&lt;heap[length-K]&lt;&lt;endl;
    for(i=0;i&lt;length-K;i++)
        cout&lt;&lt;heap[i]&lt;&lt;" ";
    return 0;
}
</code></pre>

<p>It's all right but one of datas is Time Limit Exceed ,I am so confused with how to solve this problem.</p>
