Find the maximum value of | Ai - Aj | + | i - j |:
The minor problem is that you are printing the result in the wrong place.

The major problem is that n may be up to 100,000.  This means you need to run 10,000,000,000 of your inner test.  And you may have only 0.02 seconds to do that.

The trick to dealing with that is probably to determine the maximum possible score of a given i and l[i], and skip the inner loop if it isn't worth it.  As part of this, you might find it beneficial to determine the range of the l[] values.

As an example, if you determine that all l[i]&lt;=5, then it isn't worth looking at i&gt;6 and i&lt;n-6 (or thereabouts... I'm not positive about the border condition).