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

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

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
const ll INF = 100100100100100100;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<vector<vector<ll>>> dp(R + 5,
                                vector<vector<ll>>(C + 5, vector<ll>(5, -1)));
  // ll dp[3005][3005][5];
  vector<vector<ll>> item(R + 5, vector<ll>(C + 5, 0));

  rep(i, R + 1) {
    rep(j, C + 1) { rep(k, 5) dp[i][j][k] = -1; }
  }
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    item[r][c] = v;
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = item[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        if (dp[i][j + 1][k] < dp[i][j][k])
          dp[i][j + 1][k] = dp[i][j][k];
        if (dp[i][j + 1][k + 1] < dp[i][j][k] + item[i][j + 1])
          dp[i][j + 1][k + 1] = dp[i][j][k] + item[i][j + 1];
        ll tmp = -1 * INF;
        rep(l, 4) if (tmp < dp[i][j][l]) tmp = dp[i][j][l];
        dp[i + 1][j][0] = tmp;
        dp[i + 1][j][1] = tmp + item[i + 1][j];
      }
    }
  }

  ll ans = -1;
  rep(i, 4) if (ans < dp[R - 1][C - 1][i]) ans = dp[R - 1][C - 1][i];

  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;

#define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); (i)++)
#define frep(i, m, n) for (ll(i) = (m); (i) <= (ll)(n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) > -1; (i)--)
#define frrep(i, m, n) for (ll(i) = (n); (i) > (ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  ll dp[3005][3005][5];
  vector<vector<ll>> item(R + 5, vector<ll>(C + 5, 0));

  rep(i, R + 1) {
    rep(j, C + 1) { rep(k, 5) dp[i][j][k] = -1; }
  }
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    item[r][c] = v;
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = item[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        if (dp[i][j + 1][k] < dp[i][j][k])
          dp[i][j + 1][k] = dp[i][j][k];
        if (dp[i][j + 1][k + 1] < dp[i][j][k] + item[i][j + 1])
          dp[i][j + 1][k + 1] = dp[i][j][k] + item[i][j + 1];
        ll tmp = -1 * INF;
        rep(l, 4) if (tmp < dp[i][j][l]) tmp = dp[i][j][l];
        dp[i + 1][j][0] = tmp;
        dp[i + 1][j][1] = tmp + item[i + 1][j];
      }
    }
  }

  ll ans = -1 * INF;
  rep(i, 4) if (ans < dp[R - 1][C - 1][i]) ans = dp[R - 1][C - 1][i];

  cout << ans << endl;

  return 0;
}

ll my_abs(ll a) {
  if (a >= 0)
    return a;
  else
    return -1 * a;
}

ll a_n(ll a, ll n) {
  if (n == 0)
    return 1;

  ll ret = a, count = 1;
  while (count * 2 < n) {
    ret *= ret;
    count *= 2;
  }
  if (count == n)
    return ret;
  else
    return (ret * a_n(a, n - count));
}

ll my_gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return my_gcd(b, a % b);
}

ll inv(ll a) { return a_n(a, MOD - 2); }

ll madd(ll a, ll b, ll c) {
  ll ret = (a + b) % MOD;
  return (ret + c) % MOD;
}

ll msub(ll a, ll b) {
  if (a < b)
    return (a - b + MOD) % MOD;
  else
    return (a - b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
  ll ret = (a * b) % MOD;
  return (ret * c) % MOD;
}