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

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

ll box[3001][3001];
ll dp[3001][3001][4];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    ll a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    box[a][b] = v;
  }

  dp[0][0][0] = 0;
  dp[0][0][1] = box[0][0];

  repd(j, 1, c) {
    dp[0][j][0] = dp[0][j - 1][0];
    repd(m, 1, 4) {
      if (box[0][j] == 0) {
        rep(m, 4) { dp[0][j][m] = dp[0][j - 1][m]; }
        continue;
      }
      if (m == 1 || dp[0][j - 1][m - 1] != 0) {
        dp[0][j][m] = max(dp[0][j - 1][m - 1] + box[0][j], dp[0][j - 1][m]);
      }
    }
  }

  repd(i, 1, r) {
    rep(j, c) {
      ll temp = 0;
      rep(m, 4) { temp = max(temp, dp[i - 1][j][m]); }
      if (j == 0) {
        dp[i][j][0] = temp;
        dp[i][j][1] = temp + box[i][j];
      } else {
        if (box[i][j] == 0) {
          dp[i][j][0] = max(temp, dp[i][j - 1][0]);
          repd(m, 1, 4) { dp[i][j][m] = dp[i][j - 1][m]; }
          continue;
        }
        dp[i][j][0] = max(temp, dp[i][j - 1][0]);
        repd(m, 1, 4) {
          if (m == 1 || dp[i][j - 1][m - 1] != 0) {
            dp[i][j][m] = max(dp[i][j - 1][m - 1] + box[i][j], dp[i][j - 1][m]);
          }
        }
        dp[i][j][1] = max(dp[i][j][1], temp + box[i][j]);
      }
    }
  }

  /*
  rep(i, r) {
      rep(j, c) {
          rep(m, 4) {
              cout << i << "," << j << "," << m << ":" << dp[i][j][m] << endl;
          }
      }
  }
  */

  ll ans = 0;
  rep(m, 4) { ans = max(ans, dp[r - 1][c - 1][m]); }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

ll box[3001][3001];
ll dp[3001][3001][4];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    ll a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    box[a][b] = v;
  }

  dp[0][0][0] = 0;
  dp[0][0][1] = box[0][0];

  repd(j, 1, c) {
    dp[0][j][0] = dp[0][j - 1][0];
    repd(m, 1, 4) {
      if (box[0][j] == 0) {
        rep(m, 4) { dp[0][j][m] = dp[0][j - 1][m]; }
        continue;
      }
      if (m == 1 || dp[0][j - 1][m - 1] != 0) {
        dp[0][j][m] = max(dp[0][j - 1][m - 1] + box[0][j], dp[0][j - 1][m]);
      }
    }
  }

  repd(index, 1, r) {
    int i = index % 2;
    rep(j, c) {
      ll temp = 0;
      rep(m, 4) { temp = max(temp, dp[i - 1][j][m]); }
      if (j == 0) {
        dp[i][j][0] = temp;
        dp[i][j][1] = temp + box[i][j];
      } else {
        if (box[i][j] == 0) {
          dp[i][j][0] = max(temp, dp[i][j - 1][0]);
          repd(m, 1, 4) { dp[i][j][m] = dp[i][j - 1][m]; }
          continue;
        }
        dp[i][j][0] = max(temp, dp[i][j - 1][0]);
        repd(m, 1, 4) {
          if (m == 1 || dp[i][j - 1][m - 1] != 0) {
            dp[i][j][m] = max(dp[i][j - 1][m - 1] + box[i][j], dp[i][j - 1][m]);
          }
        }
        dp[i][j][1] = max(dp[i][j][1], temp + box[i][j]);
      }
    }
  }

  rep(i, r) {
    rep(j, c) {
      rep(m, 4) {
        cout << i << "," << j << "," << m << ":" << dp[i][j][m] << endl;
      }
    }
  }

  ll ans = 0;
  rep(m, 4) { ans = max(ans, dp[r - 1][c - 1][m]); }
  cout << ans << endl;
  return 0;
}