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

#pragma region header
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// ===============================================================
// using系
#pragma region header
using namespace std;
using ll = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vvi = vector<vector<int>>;
using Graph = vvi;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
using qul = queue<ll>;
using pql = priority_queue<ll>;
using kaage = priority_queue<int, vector<int>, greater<int>>;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
#pragma endregion
// ========================================================================
// define系

// #define int long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i <= n; i++)

// ========================================================================

// ライブラリ
#pragma region header
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
// 最大公約数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
// 最小公倍数
ll box(double a) {
  ll b = a;
  return b;
}
// 切り捨て
ll fff(double a) {
  ll b = a + 0.5;
  return b;
}
// 四捨五入
ll mch(ll n) {
  if (n == 1)
    return 1;
  else
    return n * mch(n - 1);
}
// 1から整数nまでの階乗を出す(INFで割っていない)
bool prime(ll a) // 素数判定、primeならtrue,違うならfalse
{
  if (a < 2)
    return false;
  else if (a == 2)
    return true;
  else if (a % 2 == 0)
    return false;
  double m = sqrt(a);
  for (int i = 3; i <= m; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }

  // 素数である
  return true;
}
// 素数判定
ll modpow(ll a, ll n, ll mod) {
  ll hi = 1;
  while (n > 0) {
    if (n & 1)
      hi = hi * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return hi;
}
// いろいろやります（ただの前座）
ll mg(ll a, ll mod) { return modpow(a, mod - 2, mod); }
// 割り算の10^9+7等対策で逆元を出します
ll num(ll a) {
  string s = to_string(a);
  ll sum = 0;
  for (int j = 0; j < s.size(); ++j) {
    sum += s[j] - '0';
  }
  return sum;
}
// 整数aのすべての桁の和
#pragma endregion
#pragma endregion
ll dp[3010][3010][7] = {0};

// ========================================================================

#pragma region header
// ある程度のテンプレ(いじって使う)
// これとBFS、あとその時その時で使うものはここに書く
vector<bool> seens;
void dfs(const Graph &G, int v) {
  seens[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seens[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

// ========================================================================
#pragma endregion

/*signed*/ int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vl> d(r + 5, vl(c + 5, 0));
  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    d[x][y] = z;
  }
  dp[1][1][0] = 0;
  if (d[1][1] != 0)
    dp[1][1][1] = d[1][1];
  for (int i = 1; i <= r + 1; i++) {
    for (int j = 1; j <= c + 1; j++) {
      dp[i][j][0] = max({dp[i][j - 1][0], dp[i - 1][j][0], dp[i - 1][j][1],
                         dp[i - 1][j][2], dp[i - 1][j][3]});
      dp[i][j][1] = max({
          dp[i][j - 1][1],
          dp[i][j - 1][0] + d[i][j],
          dp[i - 1][j][0] + d[i][j],
          dp[i - 1][j][1] + d[i][j],
          dp[i - 1][j][2] + d[i][j],
          dp[i - 1][j][3] + d[i][j],
      });
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + d[i][j]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + d[i][j]);
    }
  }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << endl;

  return 0;
}


Error: -11

Fixed submission:

#pragma region header
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// ===============================================================
// using系
#pragma region header
using namespace std;
using ll = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vvi = vector<vector<int>>;
using Graph = vvi;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
using qul = queue<ll>;
using pql = priority_queue<ll>;
using kaage = priority_queue<int, vector<int>, greater<int>>;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
#pragma endregion
// ========================================================================
// define系

// #define int long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i <= n; i++)

// ========================================================================

// ライブラリ
#pragma region header
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
// 最大公約数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
// 最小公倍数
ll box(double a) {
  ll b = a;
  return b;
}
// 切り捨て
ll fff(double a) {
  ll b = a + 0.5;
  return b;
}
// 四捨五入
ll mch(ll n) {
  if (n == 1)
    return 1;
  else
    return n * mch(n - 1);
}
// 1から整数nまでの階乗を出す(INFで割っていない)
bool prime(ll a) // 素数判定、primeならtrue,違うならfalse
{
  if (a < 2)
    return false;
  else if (a == 2)
    return true;
  else if (a % 2 == 0)
    return false;
  double m = sqrt(a);
  for (int i = 3; i <= m; i += 2) {
    if (a % i == 0) {
      return false;
    }
  }

  // 素数である
  return true;
}
// 素数判定
ll modpow(ll a, ll n, ll mod) {
  ll hi = 1;
  while (n > 0) {
    if (n & 1)
      hi = hi * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return hi;
}
// いろいろやります（ただの前座）
ll mg(ll a, ll mod) { return modpow(a, mod - 2, mod); }
// 割り算の10^9+7等対策で逆元を出します
ll num(ll a) {
  string s = to_string(a);
  ll sum = 0;
  for (int j = 0; j < s.size(); ++j) {
    sum += s[j] - '0';
  }
  return sum;
}
// 整数aのすべての桁の和
#pragma endregion
#pragma endregion
ll dp[3005][3005][5] = {0};

// ========================================================================

#pragma region header
// ある程度のテンプレ(いじって使う)
// これとBFS、あとその時その時で使うものはここに書く
vector<bool> seens;
void dfs(const Graph &G, int v) {
  seens[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seens[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

// ========================================================================
#pragma endregion

/*signed*/ int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vl> d(r, vl(c, 0));
  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    d[x][y] = z;
  }
  dp[1][1][0] = 0;
  if (d[1][1] != 0)
    dp[0][0][1] = d[0][0];
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      dp[i][j][0] = max({dp[i][j - 1][0], dp[i - 1][j][0], dp[i][j - 1][1],
                         dp[i][j - 1][2], dp[i][j - 1][3]});
      dp[i][j][1] = max({dp[i][j - 1][1], dp[i][j - 1][0] + d[i - 1][j - 1],
                         dp[i - 1][j][0] + d[i - 1][j - 1]});
      dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + d[i - 1][j - 1]);
      dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + d[i - 1][j - 1]);
    }
  }
  cout << max({dp[r][c][0], dp[r][c][1], dp[r][c][2], dp[r][c][3]}) << endl;

  return 0;
}