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

#include <algorithm>
#include <functional>
#include <stdio.h>
int m, n;
int manj[10000];
int sum[10001];
int dp[501][10001];
using namespace std;
int main(void) {
  scanf("%d %d", &m, &n);
  int i, j, cnt = 0;
  for (i = 0; i < m; i++) {
    sum[i] = 0;
    scanf("%d", &manj[i]);
  }
  sort(manj, manj + m, greater<int>());
  int temp = 0;
  for (i = 0; i < m; i++) {
    temp += manj[i];
    sum[i + 1] += temp;
  } //
  for (i = 0; i < n; i++) {
    for (j = 0; j < 10001; j++) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < n; i++) {
    int c, e;
    scanf("%d %d", &c, &e);
    if (dp[i][c] >= e || dp[i][c] == -1) {
      dp[i][c] = e;
    }
    if (i - 1 >= 0) {
      for (j = 0; j < 10001; j++) {
        if (dp[i][j + c] > dp[i - 1][j] + e ||
            dp[i][j + c] == -1 && dp[i - 1][j] != -1 && j + c <= 10000) {
          dp[i][j + c] = dp[i - 1][j] + e;
          // printf("%d\\n",dp[i-1][j]);
        }
      }
    }
    for (j = 10000; j >= 0; j--) {
      if (dp[i][j] != -1 && dp[i][j] <= dp[i][j - 1] ||
          dp[i][j - 1] == -1 && j - 1 >= 0) {
        dp[i][j - 1] = dp[i][j];
      }
    }
    for (j = 0; j < 10001; j++) {
      if (dp[i][j] > dp[i + 1][j] || dp[i + 1][j] == -1 && j + 1 < 10001) {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  int max = 0;
  /*for(i=0;i<=15;i++){
          printf("%d=%d %d",i,sum[i],dp[n-1][i]);
          printf("\\n");
          if(max<sum[i]-dp[n-1][i] && dp[n-1][i]!=-1){
                  max=sum[i]-dp[n-1][i];
          }
          //printf("%d\\n",max);
  }*/
  for (i = 1; i < 10001; i++) {
    if (max < sum[i] - dp[n - 1][i] && dp[n - 1][i] != -1) {
      max = sum[i] - dp[n - 1][i];
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
int m, n;
int manj[10000];
int sum[10001];
int dp[500][200000];
using namespace std;
int main(void) {
  scanf("%d %d", &m, &n);
  int i, j, cnt = 0;
  for (i = 0; i < m; i++) {
    sum[i] = 0;
    scanf("%d", &manj[i]);
  }
  sort(manj, manj + m, greater<int>());
  int temp = 0;
  for (i = 0; i < m; i++) {
    temp += manj[i];
    sum[i + 1] += temp;
    // printf("%d \\n",sum[i]);
  } //
  for (i = 0; i < n; i++) {
    for (j = 0; j < 200000; j++) {
      dp[i][j] = -1;
    }
  }
  for (i = 0; i < n; i++) {
    int c, e;
    scanf("%d %d", &c, &e);
    if (dp[i][c] >= e || dp[i][c] == -1) {
      dp[i][c] = e;
    }
    if (i - 1 >= 0) {
      for (j = 0; j < 200000; j++) {
        if (dp[i][j + c] > dp[i - 1][j] + e || dp[i][j + c] == -1) {
          if (dp[i - 1][j] != -1) {
            dp[i][j + c] = dp[i - 1][j] + e;
            // printf("%d\\n",dp[i-1][j]);
          }
        }
      }
    }
    for (j = 0; j < 200000; j++) {
      if (dp[i][j] > dp[i + 1][j] || dp[i + 1][j] == -1 && j + 1 < 200000) {
        dp[i + 1][j] = dp[i][j];
      }
    }
    for (j = 200000; j > 0; j--) {
      if (dp[i][j] != -1 && dp[i][j] <= dp[i][j - 1] ||
          dp[i][j - 1] == -1 && j - 1 >= 0) {
        dp[i][j - 1] = dp[i][j];
      }
    }
  }
  int max = 0;
  /*for(i=100000;i<=200000;i++){
          printf("%d=%d %d",i,sum[i],dp[n-1][i]);
          printf("\\n");
          if(max<sum[i]-dp[n-1][i] && dp[n-1][i]!=-1){
                  max=sum[i]-dp[n-1][i];
          }
          //printf("%d\\n",max);
  }*/
  for (i = 0; i < 200000; i++) {
    if (max < sum[i] - dp[n - 1][i] && dp[n - 1][i] != -1) {
      max = sum[i] - dp[n - 1][i];
    }
  }
  printf("%d\\n", max);
  return 0;
}