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

#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 1000000007

long long int res[3010][3010][4] = {};
long long int v[3010][3010] = {};

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  for (int i = 0; i < k; i++) {
    int r, c, tmpv;
    cin >> r >> c >> tmpv;
    r--;
    c--;
    v[r][c] = tmpv;
  }
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int l = 0; l <= 3; l++) {
        // cout << i << " " << j << " " << l << " " << res[i][j][l] << endl;
        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] + v[i][j]);
          res[i][j + 1][l + 1] =
              max(res[i][j + 1][l + 1], res[i][j][l] + v[i][j]);
        }
      }
    }
  }
  cout << max({res[r][c][0], res[r][c][1], res[r][c][2]}) << 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 < 4; i++) {
    ans = max(ans, res[r - 1][c - 1][i]);
    // cout<<res[r-1][c-1][i]<<endl;
  }

  cout << ans << endl;

  // cout<<max({res[r-1][c-1]})<<endl;
  // cout<<1<<endl;

  /*
  for(int ri=0;ri<r;ri++){
      for(int ci=0;ci<c;ci++){
          int num=1;
          for(int ki=1;ki<=3;ki++){
              if(dp[ri][ci][ki]>0) num++;
          }

          for(int ki=num;ki>0;ki--){
              dp[ri][ci][ki]=max(dp[ri][ci][ki],dp[ri][ci][ki-1]+grid[ri][ci]);
          }

          if(ci+1<c){
              for(int ki=0;ki<=3;ki++){
                  dp[ri][ci+1][ki]=max(dp[ri][ci+1][ki],dp[ri][ci][ki]);
              }
          }
          if(ri+1<r){
              for(int ki=0;ki<=3;ki++){
                  dp[ri+1][ci][0]=max(dp[ri+1][ci][0],dp[ri][ci][ki]);
              }
          }
      }
  }

  int res=0;
  for(int ki=0;ki<=3;ki++){
     res=max(res,dp[r-1][c-1][ki]);
  }

  cout<<res<<endl;*/
}