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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
 <url:https://joi2014ho.contest.atcoder.jp/img/other/joi2014ho/joi2014ho2.pdf>
 ?????????============================================================

 Incredible Okashi Inc. ??????????????????????????????????????????(incredible
 okashi)???????£????????????????????????§?????????
 ????????§?????\\??????IOI ?????¨?????¶???IOI ??????????£????IOI
 ?\\????????????£????????§????????????????£????????????¨????????£?????? IOI
 ??????MM ?¨????????\\???????1 ??????????????£??????????????????MM
 ???????\\????????????????????????§????????§????????????
 ?????¨????????¨?????°?????????????????§??????????§??????§?????????i
 ??????(1???i???M)(1???i???M) ????\\????????????????Pi ?????§?????????

 ??¨????????§???????????????Just Odd Inventions
 ????????\\??£????????????????????????
 ????????????????\\?????????????????\\?????????????(just odd
 inventions)?????????????????¨??§?????????????????§?????\\??????JOI
 ?????¨?????¶??? IOI ??????????\\?????????°???????????????????´??????±???JOI
 ???????????¨???????????¨????????£?????? JOI
 ???????£???????????\\??????¨?????±???NN ?¨????????????? j
 ??????(1???j???N)(1???j???N)?????±????????§??§Cj
 ???????\\?????????°??????????????§????????§?????????????£??????????EjEj
 ?????§?????????
 ????????????NN ?¨?????????±????????????????¨???????(0 ?¨??????\\???NN
 ?¨??????\\???) ???1 ??????????????¨??????
 ?\\?????????????????????±?????°???????????????????????§????£????????????¨????????£??????
 ????\\??????????????????????????????????????????????????\\????????????????????¨???§?????????

 ?????????????\\?????????????????£?????????¨????????´??????IOI
 ?????????????????¨?????§????????????????????§??????????????????????????????
 ????????§????????¨??????????£????????\\?????????????????????????????¨???????????????¨????????±????????????????¨??????????????????§?????????
 ???????????±?????°???????????£????\\??????????????????????IOI
 ????????????????????????????????????????????????????????????????????±????????????????????¨?????????


 ????\\???????????????¨????????±?????§????????¨??????????????????????????¨??????
 IOI
 ?????????????????¨?????§????????????????????§???????±???????????????°????????????????????????

 =================================================================
 ?§£??¬=============================================================

 ???????????????

 dp[i][j] :=
 i???????????±?????§????????¨??????j???????\\?????????\\?????????????°??????±????????????
 ????±???????
 ?????????????????????????\\????????????????????¨???¨???????????¨??£??????????????§
 ================================================================
 */
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll M, N;
  cin >> M >> N;
  vector<ll> P(M);
  vector<ll> C(N), E(N);

  for (int i = 0; i < M; i++)
    cin >> P[i];
  for (int j = 0; j < N; j++)
    cin >> C[j] >> E[j];

  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());

  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, LINF));
  dp[0][0] = 0;
  dp[1][0] = 0;
  for (int j = 1; j <= min(M, C[0]); j++)
    dp[1][j] = E[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= M; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][min(M, j + C[i])] =
          min(dp[i + 1][min(M, j + C[i])], dp[i][j] + E[i]);
    }
  }

  ll res = 0;
  vector<ll> cusum(M + 1, 0);
  for (int i = 0; i < M; i++)
    cusum[i + 1] = cusum[i] + P[i];

  for (int i = 0; i <= M; i++) {
    res = max(res, cusum[i] - dp[N][i]);
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
 <url:https://joi2014ho.contest.atcoder.jp/img/other/joi2014ho/joi2014ho2.pdf>
 ?????????============================================================

 Incredible Okashi Inc. ??????????????????????????????????????????(incredible
 okashi)???????£????????????????????????§?????????
 ????????§?????\\??????IOI ?????¨?????¶???IOI ??????????£????IOI
 ?\\????????????£????????§????????????????£????????????¨????????£?????? IOI
 ??????MM ?¨????????\\???????1 ??????????????£??????????????????MM
 ???????\\????????????????????????§????????§????????????
 ?????¨????????¨?????°?????????????????§??????????§??????§?????????i
 ??????(1???i???M)(1???i???M) ????\\????????????????Pi ?????§?????????

 ??¨????????§???????????????Just Odd Inventions
 ????????\\??£????????????????????????
 ????????????????\\?????????????????\\?????????????(just odd
 inventions)?????????????????¨??§?????????????????§?????\\??????JOI
 ?????¨?????¶??? IOI ??????????\\?????????°???????????????????´??????±???JOI
 ???????????¨???????????¨????????£?????? JOI
 ???????£???????????\\??????¨?????±???NN ?¨????????????? j
 ??????(1???j???N)(1???j???N)?????±????????§??§Cj
 ???????\\?????????°??????????????§????????§?????????????£??????????EjEj
 ?????§?????????
 ????????????NN ?¨?????????±????????????????¨???????(0 ?¨??????\\???NN
 ?¨??????\\???) ???1 ??????????????¨??????
 ?\\?????????????????????±?????°???????????????????????§????£????????????¨????????£??????
 ????\\??????????????????????????????????????????????????\\????????????????????¨???§?????????

 ?????????????\\?????????????????£?????????¨????????´??????IOI
 ?????????????????¨?????§????????????????????§??????????????????????????????
 ????????§????????¨??????????£????????\\?????????????????????????????¨???????????????¨????????±????????????????¨??????????????????§?????????
 ???????????±?????°???????????£????\\??????????????????????IOI
 ????????????????????????????????????????????????????????????????????±????????????????????¨?????????


 ????\\???????????????¨????????±?????§????????¨??????????????????????????¨??????
 IOI
 ?????????????????¨?????§????????????????????§???????±???????????????°????????????????????????

 =================================================================
 ?§£??¬=============================================================

 ???????????????

 dp[i][j] :=
 i???????????±?????§????????¨??????j???????\\?????????\\?????????????°??????±????????????
 ????±???????
 ?????????????????????????\\????????????????????¨???¨???????????¨??£??????????????§
 ================================================================
 */
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll M, N;
  cin >> M >> N;
  vector<ll> P(M);
  vector<ll> C(N), E(N);

  for (int i = 0; i < M; i++)
    cin >> P[i];
  for (int j = 0; j < N; j++)
    cin >> C[j] >> E[j];

  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());

  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, LINF));
  dp[0][0] = 0;
  dp[1][0] = 0;
  for (int j = 1; j <= C[0]; j++)
    dp[1][j] = E[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= M; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j + C[i] > M)
        continue;
      dp[i + 1][j + C[i]] = min(dp[i + 1][j + C[i]], dp[i][j] + E[i]);
    }
  }

  ll res = 0;
  vector<ll> cusum(M + 1, 0);
  for (int i = 0; i < M; i++)
    cusum[i + 1] = cusum[i] + P[i];

  for (int i = 0; i <= M; i++) {
    res = max(res, cusum[i] - dp[N][i]);
  }
  cout << res << endl;
  return 0;
}