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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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;
}

ll grid[3050][3050];
ll dp[3050][3050][4];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  memset(grid, 0, sizeof(grid));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int rr, cc;
    ll v;
    cin >> rr >> cc >> v;
    --rr;
    --cc;
    grid[rr][cc] = v;
  }
  dp[0][0][1] = grid[0][0];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) // 最初のマスはスキップ
      {
        continue;
      }

      if (i == 0) // 最下段の処理
      {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] = max(dp[i][j - 1][0] + grid[i][j], dp[i][j - 1][1]);
        dp[i][j][2] = max(dp[i][j - 1][1] + grid[i][j], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j - 1][2] + grid[i][j], dp[i][j - 1][3]);
      } else // ２段目以降の処理
      {
        ll from_top = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                          max(dp[i - 1][j][2], dp[i - 1][j][3]));
        if (j == 0) {
          dp[i][j][0] = from_top;
          dp[i][j][1] = from_top + grid[i][j];
        } else {
          dp[i][j][0] = max(dp[i][j - 1][0], from_top);
          dp[i][j][1] = max(from_top + grid[i][j],
                            max(dp[i][j - 1][0] + grid[i][j], dp[i][j - 1][1]));
          dp[i][j][2] = max(dp[i][j - 1][1] + grid[i][j], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j - 1][2] + grid[i][j], dp[i][j - 1][3]);
        }
      }
    }
  }
  // for (int i = 0; i < r; i++)
  // {
  //     for (int j = 0; j < c; j++)
  //     {
  //         cout << grid[i][j] << ' ';
  //     }
  //     cout << endl;
  // }
  // for (int k = 0; k < 4; k++)
  // {
  //     cout << "===========" << endl;
  //     for (int i = 0; i < r; i++)
  //     {
  //         for (int j = 0; j < c; j++)
  //         {
  //             cout << dp[i][j][0] << ' ';
  //         }
  //         cout << endl;
  //     }
  // }
  ll ans = 0;
  for (int l = 0; l < 4; l++) {
    ans = max(ans, dp[r - 1][c - 1][l]);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1'000'000'007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll grid[r][c];
  ll dp[r][c][4];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      grid[i][j] = 0;
      for (int l = 0; l < 4; l++) {
        dp[i][j][l] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int rr, cc;
    ll v;
    cin >> rr >> cc >> v;
    --rr;
    --cc;
    grid[rr][cc] = v;
  }
  dp[0][0][1] = grid[0][0];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) // 最初のマスはスキップ
      {
        continue;
      }

      if (i == 0) // 最下段の処理
      {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] = max(dp[i][j - 1][0] + grid[i][j], dp[i][j - 1][1]);
        dp[i][j][2] = max(dp[i][j - 1][1] + grid[i][j], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j - 1][2] + grid[i][j], dp[i][j - 1][3]);
      } else // ２段目以降の処理
      {
        ll from_top = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                          max(dp[i - 1][j][2], dp[i - 1][j][3]));
        if (j == 0) {
          dp[i][j][0] = from_top;
          dp[i][j][1] = from_top + grid[i][j];
        } else {
          dp[i][j][0] = max(dp[i][j - 1][0], from_top);
          dp[i][j][1] = max(from_top + grid[i][j],
                            max(dp[i][j - 1][0] + grid[i][j], dp[i][j - 1][1]));
          dp[i][j][2] = max(dp[i][j - 1][1] + grid[i][j], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j - 1][2] + grid[i][j], dp[i][j - 1][3]);
        }
      }
    }
  }
  // for (int i = 0; i < r; i++)
  // {
  //     for (int j = 0; j < c; j++)
  //     {
  //         cout << grid[i][j] << ' ';
  //     }
  //     cout << endl;
  // }
  // for (int k = 0; k < 4; k++)
  // {
  //     cout << "===========" << endl;
  //     for (int i = 0; i < r; i++)
  //     {
  //         for (int j = 0; j < c; j++)
  //         {
  //             cout << dp[i][j][0] << ' ';
  //         }
  //         cout << endl;
  //     }
  // }
  ll ans = 0;
  for (int l = 0; l < 4; l++) {
    ans = max(ans, dp[r - 1][c - 1][l]);
  }
  cout << ans << endl;
  return 0;
}