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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
using ll = long long;
template <typename T> constexpr T INF = numeric_limits<T>::max() / 8;
constexpr int MAXN = 4000;
int N;
ll sum[MAXN];
ll getsum(const int inf, const int sup) // [inf, sup)
{
  assert(0 <= inf);
  assert(sup <= N);
  assert(inf < sup);
  return sum[sup - 1] - (inf == 0 ? 0LL : sum[inf - 1]);
}
int dp[MAXN + 1]
      [MAXN + 1]; // 最後の区間が[i,j)である増加列の項数の最大値 (i < j)
int pre[MAXN + 1][MAXN + 1];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sum[i];
  }
  for (int i = 1; i < N; i++) {
    sum[i] += sum[i - 1];
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      pre[i][j] = -1;
    }
  }
  for (int j = 1; j <= N; j++) {
    dp[0][j] = 1;
  }
  using P = pair<ll, int>;
  using PP = pair<P, int>;
  for (int mid = 1; mid < N; mid++) {
    vector<PP> v;
    for (int inf = 0; inf < mid; inf++) {
      v.push_back({{getsum(inf, mid), -inf}, dp[inf][mid]});
    }
    for (int sup = mid + 1; sup <= N; sup++) {
      v.push_back({{getsum(mid, sup), -sup}, -1});
    }
    sort(v.begin(), v.end());
    int maxi = -INF<int>;
    int maxind = -1;
    for (const auto &pp : v) {
      const int ind = -pp.first.second;
      const int value = pp.second;
      if (ind > mid) {
        dp[mid][ind] = maxi + 1;
        pre[mid][ind] = maxind;
      } else {
        if (maxi <= value) {
          maxi = value;
          maxind = ind;
        }
      }
    }
  }

  int num = -1;
  int left = 0;
  for (int i = 0; i < N; i++) {
    if (num <= dp[i][N]) {
      left = i;
      num = dp[i][N];
    }
  }
  cout << num << endl;
  vector<int> sep;
  int right = N;
  while (left > 0) {
    sep.push_back(left);
    const int L = left;
    left = pre[left][right];
    right = L;
  }
  reverse(sep.begin(), sep.end());
  for (const int l : sep) {
    cout << l << " ";
  }
  cout << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;
using ll = long long;
template <typename T> constexpr T INF = numeric_limits<T>::max() / 8;
constexpr int MAXN = 4000;
int N;
ll sum[MAXN];
ll getsum(const int inf, const int sup) // [inf, sup)
{
  assert(0 <= inf);
  assert(sup <= N);
  assert(inf < sup);
  return sum[sup - 1] - (inf == 0 ? 0LL : sum[inf - 1]);
}
ll dp1[MAXN + 1]
      [MAXN + 1]; // 最後の区間が[i,j)である増加列の項数の最大値 (i < j)
ll dp2[MAXN + 1][MAXN + 1]; // 最後の区切りがi番目でj項以上の増加列であるときの，最後の項の最小値
                            // (j <= i)
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> sum[i];
  }
  for (int i = 1; i < N; i++) {
    sum[i] += sum[i - 1];
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      dp2[i][j] = INF<ll>;
    }
  }
  for (int i = 0; i <= N; i++) {
    dp2[i][0] = -INF<ll>;
  }
  for (int i = 0; i < N; i++) {
    for (int j = N; j >= i + 1; j--) {
      cerr << "(" << i << "," << j << ")" << endl;
      const ll s = getsum(i, j);
      if (dp2[i][i] < s) {
        dp1[i][j] = i + 1;
        dp2[j][i + 1] = s;
      }
    }
  }
  int num = N;
  for (; num >= 0; num--) {
    if (dp2[N][num] < INF<ll>) {
      break;
    }
  }

  cout << num << endl;
  int right = N;
  int left = N - 1;
  vector<int> ans;
  for (; num >= 2; num--) {
    for (; left >= 0; left--) {
      if (dp1[left][right] == num) {
        break;
      }
    }
    ans.push_back(left);
    right = left;
    left--;
  }
  reverse(ans.begin(), ans.end());
  for (const int c : ans) {
    cout << c << " ";
  }
  cout << endl;
  return 0;
}