1 class Solution { 2 public: 3 int minCost(int n, vector<int>& cuts) { 4 int m = cuts.size(); 5 sort(cuts.begin(), cuts.end()); 6 cuts.insert(cuts.begin(), 0); 7 cuts.push_back(n); 8 vector<vector<int>> f(m + 2, vector<int>(m + 2)); 9 for (int i = m; i >= 1; --i) { 10 for (int j = i; j <= m; ++j) { 11 f[i][j] = (i == j ? 0 : INT_MAX); 12 for (int k = i; k <= j; ++k) { 13 f[i][j] = min(f[i][j], f[i][k - 1] + f[k + 1][j]); 14 } 15 f[i][j] += cuts[j + 1] - cuts[i - 1]; 16 } 17 } 18 return f[1][m]; 19 } 20 };