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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct P {
  long long size, cost;
};
long long dp[600][10500];
long long a[10500], m, n;
long long d[10500];
P b[600];
long long solve(int i, int j) {
  if (i == n)
    return 0;
  long long sum = 0;
  if (j + b[i].size < m) {
    if (dp[i + 1][j + b[i].size] != -1) {
      sum = (d[j + b[i].size] - d[j] - b[i].cost) + dp[i + 1][j + b[i].size];
    } else {
      sum = (d[j + b[i].size] - d[j] - b[i].cost) +
            (dp[i + 1][j + b[i].size] = solve(i + 1, (j + b[i].size)));
    }
  } else {
    if (dp[i + 1][m] != -1) {
      sum = d[m] - d[j] - b[i].cost + dp[i + 1][m];
    } else {
      sum = d[m] - d[j] - b[i].cost + (dp[i + 1][m] = solve(i + 1, m));
    }
  }
  if (dp[i + 1][j] != -1) {
    sum = max(sum, dp[i + 1][j]);
  } else {
    sum = max(sum, dp[i + 1][j] = solve(i + 1, j));
  }
  return sum;
}
int main() {
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      cin >> a[i];
      d[i] = 0;
    }
    d[m] = 0;
    sort(a, a + m);
    reverse(a, a + m);
    for (int i = 0; i < n; i++)
      cin >> b[i].size >> b[i].cost;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < m; j++)
        dp[i][j] = -1;
    }
    for (int i = 1; i <= m; i++) {
      d[i] = d[i - 1] + a[i - 1];
    }
    long long sum = solve(0, 0);
    if (sum < 0)
      sum = 0;
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct P {
  int size, cost;
};
long long dp[600][10500];
int a[10500], m, n;
long long d[10500];
P b[600];
long long solve(int i, int j) {
  if (j == n)
    return 0;
  long long sum = 0;
  if (j + b[i].size < m) {
    if (dp[i + 1][j + b[i].size] != -1) {
      sum = (d[j + b[i].size] - d[j] - b[i].cost) + dp[i + 1][j + b[i].size];
    } else {
      sum = (d[j + b[i].size] - d[j] - b[i].cost) +
            (dp[i + 1][j + b[i].size] = solve(i + 1, (j + b[i].size)));
    }
  } else {
    if (dp[i + 1][m] != -1) {
      sum = d[m] - d[j] - b[i].cost + dp[i + 1][m];
    } else {
      sum = d[m] - d[j] - b[i].cost + (dp[i + 1][m] = solve(i + 1, m));
    }
  }
  if (dp[i + 1][j] != -1) {
    sum = max(sum, dp[i + 1][j]);
  } else {
    sum = max(sum, solve(i + 1, j));
  }
  return sum;
}
int main() {
  int c;
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      cin >> a[i];
      d[i] = 0;
    }
    d[m] = 0;
    sort(a, a + m);
    reverse(a, a + m);
    for (int i = 0; i < n; i++)
      cin >> b[i].size >> b[i].cost;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        dp[i][j] = -1;
    }
    for (int i = 1; i <= m; i++) {
      d[i] = d[i - 1] + a[i - 1];
    }
    long long sum = solve(0, 0);
    if (sum < 0)
      sum = 0;
    cout << sum << endl;
  }
}