dp[i][j][m]
with infinity, except for dp[i][i][1]
which should be 0 since merging a single pile has no cost.[i, j]
, try to merge it into m
piles where m
ranges from 1 to k
.m == 1
, the cost is the cost to merge into k
piles plus the sum of stones in [i, j]
.m > 1
, find the minimum cost by trying to split the range [i, j]
into two parts at every possible split point, and add the cost of merging the left part into one pile and the right part into m-1
piles.dp[0][n-1][1]
, which represents the cost to merge the whole array into one pile.