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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int n;
int cut[4010][4010];
// 4000*4000*8=128,000,000=128MB
ll dp[4010][4010];
ll w[4010];

int main(void) {
  cin >> n;
  rep(i, n) cin >> w[i];

  rep(i, n) if (i - 1 >= 0) w[i] += w[i - 1];

  clr(dp, -1);
  rep(i, n) dp[i][i] = 0;
  rep(i, n) cut[i][i] = i;

  // dp[0][n-2] dp[1][n-1]
  // dp[0][n-1]
  for (int d = 1; d <= n - 1; ++d) {
    for (int i = 0, j; j = i + d, j <= n - 1; ++i) {
      for (int k = cut[i][j - 1]; k <= cut[i + 1][j]; ++k) {
        if (k + 1 > j)
          continue;
        ll cur = dp[i][k] + dp[k + 1][j] + w[j];
        if (i - 1 >= 0)
          cur -= w[i - 1];
        if (dp[i][j] == -1 || dp[i][j] > cur) {
          dp[i][j] = cur;
          cut[i][j] = k;
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int n;
int cut[4010][4010];
ll dp[4010][4010];

ll cost[4010];
ll w[4010][4010];

int main(void) {
  cin >> n;
  rep(i, n) cin >> cost[i];

  rep(i, n) rep(j, n) {
    if (i > j)
      continue;
    w[i][j] = cost[j];
    if (j - 1 >= 0)
      w[i][j] += w[i][j - 1];
  }

  clr(dp, -1);
  rep(i, n) dp[i][i] = 0;
  rep(i, n) cut[i][i] = i;

  // dp[0][n-2] dp[1][n-1]
  // dp[0][n-1]
  for (int d = 1; d <= n - 1; ++d) {
    for (int i = 0, j; j = i + d, j <= n - 1; ++i) {
      for (int k = cut[i][j - 1]; k <= cut[i + 1][j]; ++k) {
        if (k + 1 > j)
          continue;
        ll cur = dp[i][k] + dp[k + 1][j] + w[i][j];
        if (dp[i][j] == -1 || dp[i][j] > cur) {
          dp[i][j] = cur;
          cut[i][j] = k;
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}