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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll dp[505][10005];

int main() {
  int m, n;
  cin >> m >> n;

  vector<int> v(m);
  rep(i, m) cin >> v[i];

  vector<P> p(n);
  rep(i, n) { cin >> p[i].first >> p[i].second; }

  sort(v.begin(), v.end(), greater<int>());

  int d[10005];
  memset(d, 0, sizeof(d));
  REP(i, 1, m + 1) { d[i] += d[i - 1] + v[i - 1]; }

  // cout << d[m] << endl;
  // cout << " N : " << n << endl;
  // rep(i, n) cout << p[i].first << " " << p[i].second << endl;

  memset(dp, 0, sizeof(dp));
  rep(i, 505) { rep(j, 10005) dp[i][j] = -1; }

  dp[0][0] = 0;

  rep(i, n) {
    rep(j, m + 1) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); }

    rep(j, m) {
      if (dp[i][j] == -1)
        continue;

      int id = j + p[i].first;
      id = min(id, m);
      // cout << id << " <-" << j + p[i].first << " " << j << " " << p[i].first
      // << " " << dp[i][j] + (d[id] - d[j]) - p[i].second << endl;
      dp[i + 1][id] =
          max(dp[i + 1][id],
              max(dp[i][id], dp[i][j] + (d[id] - d[j]) - p[i].second));
    }

    // cout << "--- i:" << i << endl;
    // rep(j, n + 1) {
    // 	rep(k, m + 1) {
    // 		if(dp[j][k] == -1) continue;
    // 		cout << dp[j][k] << " ";
    // 	}
    // 	cout << endl;
    // }
  }

  ll ans = 0;
  rep(i, n + 1) {
    rep(j, m + 1) {
      ans = max(ans, dp[i][j]);
      // cout << dp[i][j] << " ";
    }
    // cout << endl;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll dp[505][10005];

int main() {
  int m, n;
  cin >> m >> n;

  vector<int> v(m);
  rep(i, m) cin >> v[i];

  vector<P> p(n);
  rep(i, n) { cin >> p[i].first >> p[i].second; }

  sort(v.begin(), v.end(), greater<int>());

  int d[1005];
  memset(d, 0, sizeof(d));
  REP(i, 1, m + 1) { d[i] += d[i - 1] + v[i - 1]; }

  memset(dp, 0, sizeof(dp));
  rep(i, 505) {
    rep(j, 10005) dp[i][j] = -1;
    dp[i][0] = 0;
  }

  rep(i, n) {
    rep(j, m + 1) {
      if (dp[i][j] == -1)
        continue;

      int id = j + p[i].first;
      id = min(id, m);
      dp[i + 1][id] = max(dp[i][id], dp[i][j] + (d[id] - d[j]) - p[i].second);
    }

    // rep(j, n + 1) {
    // 	rep(k, m + 1) {
    // 		cout << dp[j][k] << " ";
    // 	}
    // 	cout << endl;
    // }
  }

  ll ans = 0;
  rep(i, n + 1) {
    rep(j, m + 1) {
      ans = max(ans, dp[i][j]);
      // cout << dp[i][j] << " ";
    }
    // cout << endl;
  }

  cout << ans << endl;

  return 0;
}