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

//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
//---------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000000ll
#define all(x) x.begin(), x.end()
#define fi first
#define se second
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const double pi = acos(-1);
//---------------------------------------------------------------

// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
ll dp[3005][3005][4];
ll t[3005][3005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int r, c, k;
  cin >> r >> c >> k;
  rep(i, k) {
    int a, b, tt;
    cin >> a >> b >> tt;
    a--;
    b--;
    t[a][b] = tt;
  }
  rep(i, r + 1) rep(j, c + 1) rep(l, 4) { dp[i][j][l] = 0; }
  dp[0][0][1] = t[0][0];

  rep(i, r) {
    rep(j, c) {
      for (int l = 0; l < 4; l++) {
        if (i + 1 < r) {
          // 下に移動
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
          // 下に移動して宝をとる
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][l] + t[i + 1][j]);
        }

        if (j + 1 < c) {
          // 右に移動
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
          // 右に移動して宝をとる(今回は制約ありver.)
          if (l + 1 <= 3) {
            dp[i][j + 1][l + 1] =
                max(dp[i][j + 1][l + 1], dp[i][j][l] + t[i][j + 1]);
          }
        }
      }
    }
  }

  // 出力
  cout << *max_element(dp[r - 1][c - 1], dp[r - 1][c - 1] + 4) << endl;
}


Error: -11

Fixed submission:

//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
//---------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1000000000000000000ll
#define all(x) x.begin(), x.end()
#define fi first
#define se second
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const double pi = acos(-1);
//---------------------------------------------------------------

// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll r, c, k;
  cin >> r >> c >> k;
  ll t[3005][3005];
  rep(i, k) {
    int a, b, tt;
    cin >> a >> b >> tt;
    t[a - 1][b - 1] = tt;
  }

  ll dp[3005][3005][4];
  dp[0][0][1] = t[0][0];
  rep(i, r) {
    rep(j, c) {
      for (int l = 0; l < 4; l++) {
        // 下に移動
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][0]);
        // 下に移動して宝をとる
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i + 1][j][k] + t[i + 1][j]);
        // 右に移動
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // 右に移動して宝をとる(今回は制約ありver.)
        if (k + 1 <= 3) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + t[i][j + 1]);
        }
      }
    }
  }

  // 出力
  cout << *max_element(dp[r - 1][c - 1], dp[r - 1][c - 1] + 4) << endl;
  return 0;
}