Problem p01529 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

long long int optimalBST_KY(vector<long long int> &nums) {
  vector<long long int> sums(nums.size() + 1);
  vector<vector<long long int>> anss(nums.size(),
                                     vector<long long int>(nums.size() + 1));
  vector<vector<int>> ks(nums.size(), vector<int>(nums.size() + 1));
  sums[0] = 0;
  for (int i = 0; i < nums.size(); ++i) {
    sums[i + 1] = sums[i] + nums[i];
  }

  for (int i = 0; i < nums.size(); ++i) {
    for (int j = 0; j <= nums.size(); ++j) {

      anss[i][j] = 1e18;
    }
    anss[i][i + 1] = 0;
    ks[i][i + 1] = i; // +
  }
  // for (int w = 1; w <= n; ++w)
  //	for (int i = 0, j; (j = i + w) <= n; ++i)
  //		for (int r = K[i][j - 1]; r <= K[i + 1][j]; ++r) { // c
  //			int c = X[i][r] + X[r + 1][j] + W[i][j];
  //			if (X[i][j] > c) { X[i][j] = c; K[i][j] = r; } // c
  //		}

  for (int len = 2; len <= nums.size(); ++len) {
    for (int l = 0; l + len <= nums.size(); ++l) {
      const int r = l + len;
      for (int c = ks[l][r - 1]; c <= ks[l + 1][r]; ++c) {
        const long long int ans = anss[l][c] + anss[c][r] + sums[r] - sums[l];
        if (anss[l][r] > ans) {
          anss[l][r] = ans;
          ks[l][r] = c;
        }
      }
    }
  }
  return anss[0][nums.size()];
}
int main() {
  int N;
  cin >> N;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i)
    cin >> ws[i];
  const long long int ans = optimalBST_KY(ws);
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

long long int optimalBST_KY(vector<long long int> &nums) {
  vector<vector<long long int>> sums(nums.size(),
                                     vector<long long int>(nums.size() + 1));
  vector<vector<long long int>> anss(nums.size(),
                                     vector<long long int>(nums.size() + 1));
  vector<vector<long long int>> ks(nums.size(),
                                   vector<long long int>(nums.size() + 1));

  for (int i = 0; i < nums.size(); ++i) {
    sums[i][i] = 0;
    for (int j = i; j < nums.size(); ++j) {
      sums[i][j + 1] = sums[i][j] + nums[j];
    }
  }

  for (int i = 0; i < nums.size(); ++i) {
    for (int j = 0; j <= nums.size(); ++j) {

      anss[i][j] = 1e18;
    }
    anss[i][i + 1] = 0;
    ks[i][i + 1] = i; // +
  }
  // for (int w = 1; w <= n; ++w)
  //	for (int i = 0, j; (j = i + w) <= n; ++i)
  //		for (int r = K[i][j - 1]; r <= K[i + 1][j]; ++r) { // c
  //			int c = X[i][r] + X[r + 1][j] + W[i][j];
  //			if (X[i][j] > c) { X[i][j] = c; K[i][j] = r; } // c
  //		}

  for (int len = 2; len <= nums.size(); ++len) {
    for (int l = 0; l + len <= nums.size(); ++l) {
      const int r = l + len;
      for (int c = ks[l][r - 1]; c <= ks[l + 1][r]; ++c) {
        const long long int ans = anss[l][c] + anss[c][r] + sums[l][r];
        if (anss[l][r] > ans) {
          anss[l][r] = ans;
          ks[l][r] = c;
        }
      }
    }
  }
  return anss[0][nums.size()];
}
int main() {
  int N;
  cin >> N;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i)
    cin >> ws[i];
  const long long int ans = optimalBST_KY(ws);
  cout << ans << endl;
  return 0;
}