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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int mj[10010] = {};
PII box[505] = {};
ll dp[505][10010];
int main(void) {
  int M, N;
  ll res = 0;
  cin >> M >> N;
  REP(i, 505) { fill(dp[i], dp[i] + 10010, INF_LL); }
  REP(i, M) { cin >> mj[i]; }
  sort(mj, mj + M, greater<int>());
  REP(i, N) {
    int c, e;
    cin >> c >> e;
    box[i] = {c, e};
  }
  dp[0][0] = 0;
  FOR(i, 1, N + 1) {
    REP(j, M + 1) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - box[i - 1].first)] +
                                       box[i - 1].second);
    }
  }
  ll sum = 0;
  REP(j, M) {
    sum += mj[j];
    res = max(res, sum - dp[N][j + 1]);
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int M, N;
  ll res = 0;
  int mj[10010];
  PII box[505];
  cin >> M >> N;
  REP(i, M) { cin >> mj[i]; }
  sort(mj, mj + M, greater<int>());
  REP(i, N) {
    int c, e;
    cin >> c >> e;
    box[i] = {c, e};
  }
  sort(box, box + M, greater<PII>());
  bool used_b[505] = {};
  int used_m = 0;
  while (1) {
    int add = 0;
    double max_rieki = 0;
    int b = -1;
    REP(i, N) {
      if (used_b[i])
        continue;
      int sum = 0;
      REP(j, box[i].first) {
        if (used_m + j < M)
          sum += mj[used_m + j];
      }
      if (max_rieki < (double)(sum - box[i].second) / (double)box[i].first) {
        add = box[i].first;
        max_rieki = (double)(sum - box[i].second) / (double)box[i].first;
        b = i;
      }
    }
    res += max_rieki * add;
    if (add == 0)
      break;
    if (used_m >= M)
      break;
    used_m += add;
    used_b[b] = true;
  }
  cout << res << endl;
}