Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let <code>dp[i][j][x == 0/1]</code> be the maximum strength to select <code>j</code> disjoint subarrays from the original array’s suffix (<code>nums[i..(n - 1)]</code>), x denotes whether we select the element or not.
Initially <code>dp[n][0][0] == 0</code>.
We have <code>dp[i][j][1] = nums[i] * get(j) + max(dp[i + 1][j - 1][0], dp[i + 1][j][1])</code> where <code>get(j) = j</code> if <code>j</code> is odd, otherwise <code>-j</code>.
We can select <code>nums[i]</code> as a separate subarray or select at least <code>nums[i]</code> and <code>nums[i + 1]</code> as the first subarray. <code>dp[i][j][0] = max(dp[i + 1][j][0], dp[i][j][1])</code>.
The answer is <code>dp[0][k][0]</code>.