Problem p01088 (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 = 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 int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N;
vector<int> P;

PII dp[100][50000];
bool cmp(const PII &l, const PII &r) {
  if (l.first == r.first)
    return l.second > r.second;
  return l.first < r.first;
}

PII solve(int n, int x, int sum, int num) {
  if (n == N)
    return PII(0, 0);
  if (dp[n][x].first != -1)
    return dp[n][x];

  PII res = PII(0, INF);

  // ????????????
  res = max(res, solve(n + 1, x, sum, num), cmp);

  // ?°????????????£?????????
  {
    int kozeni = (P[n] + 500) % 1000;
    if (kozeni <= x) {
      PII tmp = solve(n + 1, x - kozeni, sum + P[n], num + 1);
      tmp.first++;
      tmp.second += P[n];
      res = max(res, tmp, cmp);
    }
  }

  // ?????????????????£?????????
  {
    int kozeni = (1000 - P[n] % 1000) % 1000;
    int get = 0;
    if (kozeni >= 500) {
      get = 1;
      kozeni -= 500;
    }

    PII tmp = solve(n + 1, x + kozeni, sum + P[n], num + get);
    tmp.first += get;
    tmp.second += P[n];
    res = max(res, tmp, cmp);
  }

  return dp[n][x] = res;
}

int main() {
  while (cin >> N, N) {
    fill(dp[0], dp[0] + 100 * 50000, PII(-1, -1));
    P.resize(N);
    rep(i, N) cin >> P[i];

    PII ans = solve(0, 0, 0, 0);
    cout << ans.first << " " << ans.second << endl;
  }
}

Error: WA

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 = 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 int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int N;
vector<int> P;

PII dp[100][50000];

bool operator<(const PII &l, const PII &r) {
  cerr << "A" << endl;
  if (l.first == r.first)
    return l.second > r.second;
  return l.first < r.first;
}

PII solve(int n, int x, int sum, int num) {
  if (n == N)
    return PII(0, 0);
  if (dp[n][x].first != -1)
    return dp[n][x];

  PII res = PII(0, INF);

  // ????????????
  res = max(res, solve(n + 1, x, sum, num));

  // ?°????????????£?????????
  {
    int kozeni = (P[n] + 500) % 1000;
    if (kozeni <= x) {
      PII tmp = solve(n + 1, x - kozeni, sum + P[n], num + 1);
      tmp.first++;
      tmp.second += P[n];
      res = max(res, tmp);
    }
  }

  // ?????????????????£?????????
  {
    int kozeni = (1000 - P[n] % 1000) % 1000;
    int get = 0;
    if (kozeni >= 500) {
      get = 1;
      kozeni -= 500;
    }

    PII tmp = solve(n + 1, x + kozeni, sum + P[n], num + get);
    tmp.first += get;
    tmp.second += P[n];
    res = max(res, tmp);
  }

  return dp[n][x] = res;
}

int main() {
  cerr << (PII(5, 10) < PII(5, 5)) << endl;
  while (cin >> N, N) {
    fill(dp[0], dp[0] + 100 * 50000, PII(-1, -1));
    P.resize(N);
    rep(i, N) cin >> P[i];

    PII ans = solve(0, 0, 0, 0);
    cout << ans.first << " " << ans.second << endl;
  }
}