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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 1e16
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  ll a[4001], s[4001];
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[i + 1] = s[i] + a[i];
  }
  vector<ll> dp[4001];
  vector<P> prev[4001];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i].push_back(INF);
      prev[i].push_back(P(0, 0));
    }
  }
  dp[0][0] = -INF;
  for (int i = 1; i <= n; i++) {
    int v[4001];
    fill(v, v + i + 1, -1);
    for (int j = 0; j < i; j++) {
      int m1 =
          lower_bound(dp[j].begin(), dp[j].end(), s[i] - s[j]) - dp[j].begin();
      if (v[m1] == -1 || s[i] - s[v[m1]] > s[i] - s[j])
        v[m1] = j;
    }
    for (int j = i; j >= 1; j--) {
      if (v[j] == -1)
        continue;
      if (dp[i][j] > s[i] - s[v[j]]) {
        prev[i][j] = P(v[j], j - 1);
        dp[i][j] = s[i] - s[v[j]];
      }
    }
    for (int j = i - 1; j >= 0; j--) {
      dp[i][j] = min(dp[i][j], dp[i][j + 1]);
    }
  }
  for (int i = n; i >= 1; i--) {
    if (dp[n][i] < INF) {
      cout << i << endl;
      P p = prev[n][i];
      vector<int> v;
      while (p.first > 0) {
        v.push_back(p.first);
        p = prev[p.first][p.second];
      }
      if (v.empty()) {
        cout << endl;
        break;
      }
      for (int j = v.size() - 1; j >= 0; j--) {
        cout << v[j];
        if (j == 0)
          break;
        cout << " ";
      }
      cout << endl;
      break;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 1e16
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  ll a[4001], s[4001];
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[i + 1] = s[i] + a[i];
  }
  ll dp[4001][4001];
  P prev[4001][4001] = {};
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int v[4001];
    fill(v, v + i + 1, -1);
    for (int j = 0; j < i; j++) {
      int m1 = lower_bound(dp[j], dp[j] + j + 1, s[i] - s[j]) - dp[j];
      if (v[m1] == -1 || s[i] - s[v[m1]] > s[i] - s[j])
        v[m1] = j;
    }
    for (int j = i; j >= 0; j--) {
      if (j < i)
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);
      if (j == 0)
        break;
      if (v[j] == -1)
        continue;
      if (dp[i][j] > s[i] - s[v[j]]) {
        prev[i][j] = P(v[j], j - 1);
        dp[i][j] = s[i] - s[v[j]];
      }
    }
  }
  for (int i = n; i >= 1; i--) {
    if (dp[n][i] < INF) {
      cout << i << endl;
      P p = prev[n][i];
      vector<int> v;
      while (p.first > 0) {
        v.push_back(p.first);
        p = prev[p.first][p.second];
      }
      if (v.empty()) {
        cout << endl;
        break;
      }
      for (int j = v.size() - 1; j >= 0; j--) {
        cout << v[j];
        if (j == 0)
          break;
        cout << " ";
      }
      cout << endl;
      break;
    }
  }
  return 0;
}