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

#include <bits/stdc++.h>
using namespace std;

using lint = long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using VVV = vector<vector<vector<T>>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define repu(i, a, b, s) for (int i = int(a); i < int(b); i += int(s))
#define repd(i, a, b, s) for (int i = int(a); i > int(b); i += int(s))
#define endl "\\n"
constexpr lint INF = 2e18;
constexpr lint MOD = 1e9 + 7;

void solve() {
  lint R, C, K;
  cin >> R >> C >> K;

  VV<lint> grid(R, V<lint>(C, -1));

  rep(i, K) {
    lint r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    grid[r][c] = v;
  }

  VVV<lint> dp(R, VV<lint>(C, V<lint>(4, 0)));

  if (grid[0][0] != -1)
    dp[0][0][1] = grid[0][0];

  rep(y, R) {
    rep(x, C) {

      // 選ばない
      if (0 <= y - 1) {
        rep(i, 4) { dp[y][x][0] = max(dp[y][x][0], dp[y - 1][x][i]); }
      }
      if (0 <= x - 1) {
        rep(i, 4) { dp[y][x][i] = max(dp[y][x][i], dp[y][x - 1][i]); }
      }

      // 選ぶ
      if (grid[y][x] != -1) {
        if (0 <= y - 1) {
          rep(i, 4) {
            dp[y][x][1] = max(dp[y][x][1], dp[y - 1][x][i] + grid[y][x]);
          }
        }
      }
      if (0 <= x - 1) {
        repu(i, 1, 4, 1) {
          dp[y][x][i] = max(dp[y][x][i], dp[y][x - 1][i - 1] + grid[y][x]);
        }
      }
    }
  }

  lint ans = -INF;
  rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using lint = long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define repu(i, a, b, s) for (int i = int(a); i < int(b); i += int(s))
#define repd(i, a, b, s) for (int i = int(a); i > int(b); i += int(s))
#define endl "\\n"
constexpr lint INF = 2e18;
constexpr lint MOD = 1e9 + 7;

void solve() {
  lint R, C, K;
  cin >> R >> C >> K;

  lint grid[R][C];
  rep(i, R) {
    rep(j, C) { grid[i][j] = -1; }
  }

  rep(i, K) {
    lint r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    grid[r][c] = v;
  }

  lint dp[R][C][4];
  rep(y, R) {
    rep(x, C) {
      rep(i, 4) { dp[y][x][i] = 0; }
    }
  }

  if (grid[0][0] != -1)
    dp[0][0][1] = grid[0][0];

  rep(y, R) {
    rep(x, C) {

      // 選ばない
      if (0 <= y - 1) {
        rep(i, 4) { dp[y][x][0] = max(dp[y][x][0], dp[y - 1][x][i]); }
      }
      if (0 <= x - 1) {
        rep(i, 4) { dp[y][x][i] = max(dp[y][x][i], dp[y][x - 1][i]); }
      }

      // 選ぶ
      if (grid[y][x] != -1) {
        if (0 <= y - 1) {
          rep(i, 4) {
            dp[y][x][1] = max(dp[y][x][1], dp[y - 1][x][i] + grid[y][x]);
          }
        }
      }
      if (0 <= x - 1) {
        repu(i, 1, 4, 1) {
          dp[y][x][i] = max(dp[y][x][i], dp[y][x - 1][i - 1] + grid[y][x]);
        }
      }
    }
  }

  lint ans = -INF;
  rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  solve();
  return 0;
}