Problem p01529 (C++) — buggy submission, verdict: Runtime Error

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const ll INF = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N;
vector<ll> W;

//  dp[i][j] := ??¨?????¨[i, j]????????????????????????????°?
//  dp[i][j] = min(dp[i][k] + dp[k+1][j] + w[i][j])
// sum[i][j] = w_i + ... + w_j
ll dp[4010][4010];
ll p[4010][4010];
ll sum[4010];

ll cost(int i, int j) { return sum[j + 1] - sum[i]; }

int main() {
  cin >> N;
  W.resize(N);
  rep(i, N) cin >> W[i];

  sum[0] = 0LL;
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + W[i];
  }

  rep(i, N) rep(j, N) dp[i][j] = (i == j ? 0LL : INF);
  rep(i, N) p[i][i] = i;

  REP(l, 1, N) {
    rep(i, N - l) {
      int j = i + l;
      int k0 = p[i][j - 1], k1 = p[i + 1][j];
      REP(k, k0, k1 + 1) {
        ll tmp = dp[i][k] + dp[k + 1][j] + cost(i, j);
        if (tmp < dp[i][j]) {
          dp[i][j] = tmp;
          p[i][j] = k;
        }
      }
    }
  }

  cout << dp[0][N - 1] << endl;
}

Error: -11

Fixed submission:

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const ll INF = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N;
vector<ll> W;

//  dp[i][j] := ??¨?????¨[i, j]????????????????????????????°?
//  dp[i][j] = min(dp[i][k] + dp[k+1][j] + w[i][j])
// sum[i][j] = w_i + ... + w_j
ll dp[3010][3010];
ll p[3010][3010];
ll sum[3010];

ll cost(int i, int j) { return sum[j + 1] - sum[i]; }

int main() {
  cin >> N;
  W.resize(N);
  rep(i, N) cin >> W[i];

  sum[0] = 0LL;
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + W[i];
  }

  rep(i, N) rep(j, N) dp[i][j] = (i == j ? 0LL : INF);
  rep(i, N) p[i][i] = i;

  REP(l, 1, N) {
    rep(i, N - l) {
      int j = i + l;
      int k0 = p[i][j - 1], k1 = p[i + 1][j];
      REP(k, k0, k1 + 1) {
        ll tmp = dp[i][k] + dp[k + 1][j] + cost(i, j);
        if (tmp < dp[i][j]) {
          dp[i][j] = tmp;
          p[i][j] = k;
        }
      }
    }
  }

  cout << dp[0][N - 1] << endl;
}