n items in a row. Item i has cost costs[i] and gain gains[i].
Pick three items at positions i < j < k whose costs strictly rise from left to right: costs[i] < costs[j] < costs[k]. The gain of such a trio is gains[i] + gains[j] + gains[k].
Return the largest gain of a valid trio, or -1 if no trio can be picked.costs=[7, 3, 5, 4, 9], gains=[50, 6, 8, 20, 10] → 36
The most profitable item (cost 7) is useless: only 9 is dearer after it, with nothing to make a third. The best trio is costs 3 < 4 < 9: 6 + 20 + 10 = 36.
costs=[2, 2, 2, 3], gains=[1, 1, 1, 1] → -1
Costs must strictly rise, and the three equal 2s cannot do that — no trio exists, the answer is -1.
costs=[1, 2, 3], gains=[4, 5, 6] → 15
The only trio is all three items: 4 + 5 + 6 = 15.
costs.length = gains.length ≤ 50,000costs[i] ≤ 5000gains[i] ≤ 10⁶costs=[7, 3, 5, 4, 9], gains=[50, 6, 8, 20, 10]
36
Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.