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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mem(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

const int MX = 3010;

ll dp[MX][MX][4];
ll v[MX][MX];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    ll a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    v[a][b] = d;
  }
  rep(i, r) rep(j, c) rep(k, 4) dp[i][j][k] = -1e18;
  dp[0][0][0] = 0;
  rep(i, r) {
    rep(j, c) {
      for (int k = 2; k >= 0; --k) {
        if (dp[i][j][k] >= 0)
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + v[i][j]);
      }
      rep(k, 4) {
        if (dp[i][j][k] >= 0) {
          if (i + 1 < r)
            dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
          if (j + 1 < c)
            dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) { ans = max(ans, dp[r - 1][c - 1][i]); }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mem(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll v[r][c];
  mem(v, 0);
  rep(i, k) {
    ll a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    v[a][b] = d;
  }
  ll dp[r][c][4];
  mem(dp, -1);
  dp[0][0][0] = 0;
  rep(i, r) {
    rep(j, c) {
      for (int k = 2; k >= 0; --k) {
        if (dp[i][j][k] >= 0)
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + v[i][j]);
      }
      rep(k, 4) {
        if (dp[i][j][k] >= 0) {
          if (i + 1 < r)
            dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
          if (j + 1 < c)
            dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) { ans = max(ans, dp[r - 1][c - 1][i]); }
  cout << ans << endl;
  return 0;
}