values. You must perform exactly one operation: pick one element and replace it with its square.
After that, take the contiguous non-empty segment with the largest sum. Return the largest such sum you can reach by choosing the element to square in the best way.values=[3, -2, -5, 1] → 27
Square the −5: the array becomes [3, −2, 25, 1], and the whole of it sums to 3 − 2 + 25 + 1 = 27.
values=[1, -3, 1] → 11
Squaring −3 gives 9, and then the whole array sums to 1 + 9 + 1 = 11, which beats squaring a 1.
values=[0] → 0
The operation is mandatory, but 0² = 0, so the answer is 0.
values=[3, -2, -5, 1]
27
Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.