Here's the D&C solution:
function solve(g, l, r, optL, optR, dp, cost)
if l > r then
return
mid := (l + r) / 2
opt := optL
dp[g][mid] := infinity
for j from optL to min(optR, mid - 1)
val := dp[g-1][j] + cost[j+1][mid]
if val < dp[g][mid] then
dp[g][mid] := val
opt := j
solve(g, l, mid-1, optL, opt, dp, cost)
solve(g, mid+1, r, opt, optR, dp, cost)
// Each layer g: solve(g, 1, n, 0, n-1, dp, cost)
Time: . Precompute in . The QI property guarantees that optimal splits are monotonic, enabling the Knuth improvement.
Space: for the DP table.
Time: . Space: .