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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

ll dp[3000][3000][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> a(r, vector<ll>(c, 0));
  int y, x;
  ll v;
  for (int i = 0; i < k; ++i) {
    cin >> y >> x >> v;
    --y;
    --x;
    a[y][x] = v;
  }

  fill_n((ll *)dp, r * c * 4, 0LL);
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      for (int l = 0; l <= 3; ++l) {
        if (j < c - 1)
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
        if (j < c - 1 && l < 3 && a[i][j])
          dp[i][j + 1][l + 1] = max(dp[i][j + 1][l + 1], dp[i][j][l] + a[i][j]);
        if (i < r - 1)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
        if (i < r - 1 && l < 3 && a[i][j])
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l] + a[i][j]);
      }
    }
  }
  if (a[r - 1][c - 1])
    for (int l = 0; l <= 2; ++l)
      dp[r - 1][c - 1][l] += a[r - 1][c - 1];

  cout << *max_element(dp[r - 1][c - 1], dp[r][0]) << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> a(r, vector<ll>(c, 0));
  int y, x;
  ll v;
  for (int i = 0; i < k; ++i) {
    cin >> y >> x >> v;
    --y;
    --x;
    a[y][x] = v;
  }

  ll dp[r][c][4];
  fill(dp[0][0], dp[r][0], 0LL);
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      for (int l = 0; l <= 3; ++l) {
        if (j < c - 1)
          dp[i][j + 1][l] = max(dp[i][j + 1][l], dp[i][j][l]);
        if (j < c - 1 && l < 3 && a[i][j])
          dp[i][j + 1][l + 1] = max(dp[i][j + 1][l + 1], dp[i][j][l] + a[i][j]);
        if (i < r - 1)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l]);
        if (i < r - 1 && l < 3 && a[i][j])
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][l] + a[i][j]);
      }
    }
  }
  if (a[r - 1][c - 1])
    for (int l = 0; l <= 2; ++l)
      dp[r - 1][c - 1][l] += a[r - 1][c - 1];

  cout << *max_element(dp[r - 1][c - 1], dp[r][0]) << endl;
}