Problem p03173 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9;
const LL LINF = INF * INF;

const int MAX = 404;
int n;
int a[MAX];
LL dp[MAX][MAX];
LL sum[MAX];

LL get(int l, int r) {
  LL res = sum[r];
  if (l)
    res -= sum[l - 1];
  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  cin >> a[i];
  sum[0] = a[0];
  FOR(i, 1, n)
  sum[i] = sum[i - 1] + a[i];

  FOR(i, 0, n)
  FOR(j, 0, n)
  dp[i][j] = LINF;
  FOR(i, 0, n - 1)
  dp[i][i + 1] = a[i] + a[i + 1];

  FOR(i, 0, n)
  dp[i][i] = 0;

  FOR(len, 3, n + 1) {
    FOR(l, 0, n) {
      int r = l + len - 1;
      FOR(k, l, r) {
        LL bude = dp[l][k] + dp[k + 1][r];
        dp[l][r] = min(dp[l][r], bude);
      }

      dp[l][r] += get(l, r);
    }
  }

  cout << dp[0][n - 1] << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cout << #a << " = " << a << endl;

const double PI = acos(-1.0);
const LL INF = 1e9;
const LL LINF = INF * INF;

const int MAX = 404;
int n;
int a[MAX];
LL dp[MAX][MAX];
LL sum[MAX];

LL get(int l, int r) {
  LL res = sum[r];
  if (l)
    res -= sum[l - 1];
  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("In.txt", "r", stdin);
  // freopen("In.txt", "w", stdout);

  cin >> n;
  FOR(i, 0, n)
  cin >> a[i];
  sum[0] = a[0];
  FOR(i, 1, n)
  sum[i] = sum[i - 1] + a[i];

  FOR(i, 0, n)
  FOR(j, 0, n)
  dp[i][j] = LINF;
  FOR(i, 0, n - 1)
  dp[i][i + 1] = a[i] + a[i + 1];

  FOR(i, 0, n)
  dp[i][i] = 0;

  FOR(len, 3, n + 1) {
    FOR(l, 0, n) {
      int r = l + len - 1;
      if (r >= n)
        break;

      FOR(k, l, r) {
        LL bude = dp[l][k] + dp[k + 1][r];
        dp[l][r] = min(dp[l][r], bude);
      }

      dp[l][r] += get(l, r);
    }
  }

  cout << dp[0][n - 1] << endl;
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}