dp
of length n + 1
with all elements as 0, where n
is the length of the input array arr
. dp[i]
will store the maximum sum we can achieve for the subarray ending at position i - 1
.i
, iterate over all possible lengths of the subarray ending at i
, from 1 to k
.l
, find the maximum value in the subarray arr[i - l + 1]
to arr[i]
.l
.i - l
, which is dp[i - l]
.dp[i]
with the maximum of its current value and the new sum calculated.dp
will contain the maximum sum of the array after partitioning.