Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let's look at the condition as: <code>prices[indexes[i]] - indexes[i] == prices[indexes[j]] - indexes[j]</code>.
So now we define a new array named <code>group</code> and is constructed as <code>group[i] = prices[i] - i</code>.
A subarray of <code>prices</code> is linear if they belong to the same group.
Since all elements are positive, if we choose some index <code>i</code>, the optimum way is to choose all elements from <code>group[i]</code>.
So for each group, we calculate the sum of its prices and the answer would be the maximum sum over all groups.