Codechef Deforestation problem: Finding maximum rope length between trees
There appears to be a bug in your program. In particular, I think this line is wrong:

if (h[l] &gt; x[l])


Starting from location j, the question is not whether h[l] &gt; x[l] but whether h[l] - h[j] &gt; x[l] - x[j]. The 45 degree angle will be relative to the start-point of the line, not the origin, I think.

That said, your loop over l is duplicating the results of looping over k. I think you could profit by keeping a running total of lossage during your k loop, and just adding it in when appropriate.

Viz:

for (j...)
    auto hj = h[j], xj = x[j];

    for (k...)
        auto dx = x[k] - xj;
        auto dh = h[k] - hj;

        auto over_height = dh - dx;

        if (over_height &gt; 0) deforestation += over_height;

        if (over_height == 0) {
            // this k is a candidate
            rope_len = sqrt(...) - deforestation;

        }