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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll dp[3010 + 1][3010 + 1][4];
ll V[3010 + 1][3010 + 1];

int main() {
  fastio ll R, C, K;
  cin >> R >> C >> K;
  rep(i, 0, R) rep(j, 0, C) rep(k, 0, 3) dp[i][j][k] = -1;
  rep(i, 0, R) rep(j, 0, C) V[i][j] = 0;
  rep(i, 0, K - 1) {
    ll r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }
  if (V[1][1] > 0) {
    dp[1][1][0] = 0;
    dp[1][1][1] = V[1][1];
  } else {
    dp[1][1][0] = 0;
  }
  rep(i, 1, R) {
    rep(j, 1, C) {
      if (i == 1 && j == 1)
        continue;
      if (i > 1) {
        if (V[i][j] > 0) {
          rep(k, 0, 3) {
            if (dp[i - 1][j][k] >= 0) {
              dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
              dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + V[i][j]);
            }
          }
        } else {
          rep(k, 0, 3) {
            if (dp[i - 1][j][k] >= 0) {
              dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
            }
          }
        }
      }
      if (j > 1) {
        if (V[i][j] > 0) {
          rep(k, 0, 3) {
            if (dp[i][j - 1][k] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
            }
          }
          rep(k, 1, 3) {
            if (dp[i][j - 1][k - 1] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + V[i][j]);
            }
          }
        } else {
          rep(k, 0, 3) {
            if (dp[i][j - 1][k] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
            }
          }
        }
      }
    }
  }
  /*
  rep(k,0,3){
      cout<<endl;
      rep(i,1,R){
          rep(j,1,C){
              cout<<dp[i][j][k]<<' ';
          }
          cout<<endl;
      }
  }*/
  ll ans = 0;
  rep(i, 0, 3) { ans = max(ans, dp[R][C][i]); }
  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio ll R, C, K;
  cin >> R >> C >> K;
  ll dp[3010 + 1][3010 + 1][4];
  rep(i, 0, R) rep(j, 0, C) rep(k, 0, 3) dp[i][j][k] = -1;
  ll V[3010 + 1][3010 + 1];
  rep(i, 0, R) rep(j, 0, C) V[i][j] = 0;
  rep(i, 0, K - 1) {
    ll r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }
  if (V[1][1] > 0) {
    dp[1][1][0] = 0;
    dp[1][1][1] = V[1][1];
  } else {
    dp[1][1][0] = 0;
  }
  rep(i, 1, R) {
    rep(j, 1, C) {
      if (i == 1 && j == 1)
        continue;
      if (i > 1) {
        if (V[i][j] > 0) {
          rep(k, 0, 3) {
            if (dp[i - 1][j][k] >= 0) {
              dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
              dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + V[i][j]);
            }
          }
        } else {
          rep(k, 0, 3) {
            if (dp[i - 1][j][k] >= 0) {
              dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
            }
          }
        }
      }
      if (j > 1) {
        if (V[i][j] > 0) {
          rep(k, 0, 3) {
            if (dp[i][j - 1][k] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
            }
          }
          rep(k, 1, 3) {
            if (dp[i][j - 1][k - 1] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + V[i][j]);
            }
          }
        } else {
          rep(k, 0, 3) {
            if (dp[i][j - 1][k] >= 0) {
              dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
            }
          }
        }
      }
    }
  }
  /*
  rep(k,0,3){
      cout<<endl;
      rep(i,1,R){
          rep(j,1,C){
              cout<<dp[i][j][k]<<' ';
          }
          cout<<endl;
      }
  }*/
  ll ans = 0;
  rep(i, 0, 3) { ans = max(ans, dp[R][C][i]); }
  cout << ans << endl;

  return 0;
}