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

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rp(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define all_acc(x) (x).begin(), (x).end(), 0LL
#define int long long
#define absSort(v)                                                             \\
  sort(a.begin(), a.end(),                                                     \\
       [](int i, int j) -> bool { return abs(i) < abs(j); });
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;
double pi = 3.14159265359;

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

// 出力するよ
void output_tate(vector<int> s) {
  int n = s.size();
  for (int i = 0; i < n; i++) {
    // if (i != 0)
    //     cout << " ";
    cout << s[i] << endl;
  }
  // cout << endl;
}

void output_yoko(vector<int> s) {
  int n = s.size();
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << s[i]; //<<endl;
  }
  cout << endl;
}

int dp[3010][3010][4];
int v[3010][3010] = {};

signed main() {
  int r, c, K;
  cin >> r >> c >> K;
  rp(i, K) {
    int ri, ci, vi;
    cin >> ri >> ci >> vi;
    v[ri - 1][ci - 1] = vi;
  }

  rp(i, 3010) rp(j, 3010) rp(l, 4) dp[i][j][l] = -1;
  dp[0][0][0] = 0;

  rp(i, r) {
    rp(j, c) {
      // dp[i][j][k]が非負のときは、一つ選んだ場合を考える。
      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]);
        }
      }
      rp(k, 4) {
        if (dp[i][j][k] >= 0) {
          if (i + 1 < r)
            chmax(dp[i + 1][j][0], dp[i][j][k]);
          if (j + 1 < c)
            chmax(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }

  int ans = -1;
  rp(k, 4) chmax(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rp(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define all_acc(x) (x).begin(), (x).end(), 0LL
#define int long long
#define absSort(v)                                                             \\
  sort(a.begin(), a.end(),                                                     \\
       [](int i, int j) -> bool { return abs(i) < abs(j); });
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;
double pi = 3.14159265359;

// 出力するよ
void output_tate(vector<int> s) {
  int n = s.size();
  for (int i = 0; i < n; i++) {
    // if (i != 0)
    //     cout << " ";
    cout << s[i] << endl;
  }
  // cout << endl;
}

void output_yoko(vector<int> s) {
  int n = s.size();
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << s[i]; //<<endl;
  }
  cout << endl;
}

int MOD = pow(10, 9) + 7;

int kaijou(int n) {
  if (n == 0)
    return 1;
  else {
    return ((n % MOD) * (kaijou(n - 1) % MOD)) % MOD;
  }
}

bool ispow(int n) {
  int rn = sqrt(n);
  if (rn * rn == n)
    return true;
  else
    return false;
}

int res[3001][3001][4] = {}; //={}で全要素０で初期化できるらしい

signed main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<int>> grid(r + 1, vector<int>(c + 1, 0));
  int dp[r][c][4];
  for (int ri = 0; ri < r; ri++) {
    for (int ci = 0; ci < c; ci++) {
      for (int ki = 0; ki < 4; ki++)
        dp[ri][ci][ki] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int ri, ci, vi;
    cin >> ri >> ci >> vi;
    grid[ri - 1][ci - 1] = vi;
  }

  for (int i = 0; i < r; i++) {
    //    output_yoko(grid[i]);
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int l = 0; l <= 3; l++) {
        res[i + 1][j][0] = max(res[i + 1][j][0], res[i][j][l]);
        res[i][j + 1][l] = max(res[i][j + 1][l], res[i][j][l]);
        if (l + 1 <= 3) {
          res[i + 1][j][0] = max(res[i + 1][j][0], res[i][j][l] + grid[i][j]);
          res[i][j + 1][l + 1] =
              max(res[i][j + 1][l + 1], res[i][j][l] + grid[i][j]);
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 3; i++) {
    ans = max(ans, res[r - 1][c - 1][i]);
    // cout<<res[r-1][c-1][i]<<endl;
  }

  cout << ans << endl;
}