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

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 0x7fffffffffll;

struct Entry {
  signed long long sum : 40;
  unsigned int len : 12;
  unsigned int prev : 12;
  Entry(ll s = 0, short l = 0, short p = 0) : sum(s), len(l), prev(p) {}
  bool operator<(const Entry &e) const {
    if (sum < e.sum) {
      return true;
    }
    if (sum > e.sum) {
      return false;
    }
    if (len < e.len) {
      return true;
    }
    if (len > e.len) {
      return false;
    }
    return prev < e.prev;
  }
};

Entry dp[4001][4002];
short dp_count[4001] = {0};
int a[4000];

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  dp[0][dp_count[0]++] = Entry(-INF, 0, 0);
  for (int i = 0; i < n; ++i) {
    sort(&(dp[i][0]), &(dp[i][dp_count[i]]));
    for (int j = 1; j < dp_count[i]; ++j) {
      if (dp[i][j - 1].len >= dp[i][j].len) {
        dp[i][j] = dp[i][j - 1];
      }
    }
    ll s = 0;
    for (int j = i + 1; j <= n; ++j) {
      s += a[j - 1];
      Entry *it =
          lower_bound(&(dp[i][0]), &(dp[i][dp_count[i]]), Entry(s, 0, 0));
      if (it == &(dp[i][0])) {
        continue;
      }
      dp[j][dp_count[j]++] = Entry(s, (it - 1)->len + 1, j - i);
    }
  }
  Entry maximum = Entry(0, 0, 0);
  for (int i = 0; i < dp_count[n]; ++i) {
    if (dp[n][i].len > maximum.len) {
      maximum = dp[n][i];
    }
  }
  int anum = 0, p = n;
  while (maximum.prev > 0) {
    p = p - maximum.prev;
    a[anum++] = p;
    for (int i = 0; i < dp_count[p]; ++i) {
      if (dp[p][i].sum < maximum.sum && dp[p][i].len == maximum.len - 1) {
        maximum = dp[p][i];
        break;
      }
    }
  }
  sort(a, a + anum);
  cout << anum << endl;
  for (int i = 1; i < anum; ++i) {
    cout << a[i];
    if (i + 1 != anum) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

struct Entry {
  long long sum;
  short len, prev;
  Entry(ll s = 0, short l = 0, short p = 0) : sum(s), len(l), prev(p) {}
  bool operator<(const Entry &e) const {
    if (sum < e.sum) {
      return true;
    }
    if (sum > e.sum) {
      return false;
    }
    if (len < e.len) {
      return true;
    }
    if (len > e.len) {
      return false;
    }
    return prev < e.prev;
  }
};

Entry pool[4002 * 4003 / 2];
Entry *dp[4001];
short dp_count[4001] = {0};
int a[4000];

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0, j = 0; i <= n; ++i) {
    dp[i] = &(pool[j]);
    j += i + 1;
  }
  dp[0][dp_count[0]++] = Entry(0, 0, 1);
  for (int i = 0; i < n; ++i) {
    sort(&(dp[i][0]), &(dp[i][dp_count[i]]));
    for (int j = 1; j < dp_count[i]; ++j) {
      if (dp[i][j - 1].len >= dp[i][j].len) {
        dp[i][j] = dp[i][j - 1];
      }
    }
    ll s = 0;
    for (int j = i + 1; j <= n; ++j) {
      s += a[j - 1];
      Entry *it =
          lower_bound(&(dp[i][0]), &(dp[i][dp_count[i]]), Entry(s, 0, 0));
      if (it == &(dp[i][0])) {
        continue;
      }
      dp[j][dp_count[j]++] = Entry(s, (it - 1)->len + 1, j - i);
    }
  }
  Entry maximum = Entry(0, 0, 0);
  for (int i = 0; i < dp_count[n]; ++i) {
    if (dp[n][i].len > maximum.len) {
      maximum = dp[n][i];
    }
  }
  int anum = 0, p = n;
  while (p - maximum.prev >= 0) {
    p = p - maximum.prev;
    a[anum++] = p + 1;
    for (int i = 0; i < dp_count[p]; ++i) {
      if (dp[p][i].sum < maximum.sum && dp[p][i].len == maximum.len - 1) {
        maximum = dp[p][i];
        break;
      }
    }
  }
  sort(a, a + anum);
  cout << anum << endl;
  for (int i = 0; i + 1 < anum; ++i) {
    cout << a[i];
    if (i + 2 != anum) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}