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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//----------------------------------------------------------------
ll dp[3010][3010][8];

int main(void) {
  int R, C, K;
  cin >> R >> C >> K;
  vll2(item, R + 1, C + 1, -1);
  rep(i, K) {
    int a, b;
    ll v;
    cin >> a >> b >> v;
    item[a][b] = v;
  }

  rep(i, 3010) {
    rep(j, 3010) {
      rep(k, 8) { dp[i][j][k] = 0; }
    }
  }

  REP(i, 1, R + 1) {
    REP(j, 1, C + 1) {
      ll tmp = 0;
      rep(k, 4) { chmax(tmp, dp[i - 1][j][k]); }
      chmax(dp[i][j][0], tmp);
      if (item[i][j] > 0)
        chmax(dp[i][j][1], tmp + item[i][j]);

      rep(k, 4) {
        chmax(dp[i][j][k], dp[i][j - 1][k]);
        if (k != 0 && item[i][j] > 0) {
          chmax(dp[i][j][k], dp[i][j - 1][k - 1] + item[i][j]);
        }
      }
    }
  }

  ll ans = 0;
  rep(i, 4) { chmax(ans, dp[R][C][i]); }
  prt(ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec vector<int>
#define vecll vector<ll>
#define vecd vector<double>
#define vecst vector<string>
#define vecb vector<bool>
#define v2(v, n, m, init) vector<vector<int>> v(n, vector<int>(m, init))
#define vb2(v, n, m, init) vector<vector<bool>> v(n, vector<bool>(m, init))
#define vll2(v, n, m, init) vector<vector<ll>> v(n, vector<ll>(m, init))

#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define REP(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define arr(var, n)                                                            \\
  vec var(n);                                                                  \\
  rep(i, n) { cin >> var[i]; }
#define arrll(var, n)                                                          \\
  vecll var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define arrst(var, n)                                                          \\
  vecst var(n);                                                                \\
  rep(i, n) { cin >> var[i]; }
#define all(var) (var).begin(), (var).end()
#define sortall(var) sort(all(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb(var) push_back(var)
#define prt(var) cout << (var) << "\\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\\n"
#define prtall(v)                                                              \\
  rep(i, v.size()) { cout << v[i] << (i != v.size() - 1 ? " " : "\\n"); }
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//----------------------------------------------------------------

int main(void) {
  int R, C, K;
  cin >> R >> C >> K;
  vll2(item, R + 1, C + 1, -1);
  rep(i, K) {
    int a, b;
    ll v;
    cin >> a >> b >> v;
    item[a][b] = v;
  }

  ll dp[R + 1][C + 1][4];
  rep(i, R + 1) {
    rep(j, C + 1) {
      rep(k, 4) { dp[i][j][k] = 0; }
    }
  }

  REP(i, 1, R + 1) {
    REP(j, 1, C + 1) {
      ll tmp = 0;
      rep(k, 4) { chmax(tmp, dp[i - 1][j][k]); }
      chmax(dp[i][j][0], tmp);
      if (item[i][j] > 0)
        chmax(dp[i][j][1], tmp + item[i][j]);

      rep(k, 4) {
        chmax(dp[i][j][k], dp[i][j - 1][k]);
        if (k != 0 && item[i][j] > 0) {
          chmax(dp[i][j][k], dp[i][j - 1][k - 1] + item[i][j]);
        }
      }
    }
  }

  ll ans = 0;
  rep(i, 4) { chmax(ans, dp[R][C][i]); }
  prt(ans);
}