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

  auto grid = new lint[3001][3001]();
  auto dp = new lint[3001][3001][4]();

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

  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;

  delete[] dp;
  delete[] grid;
}

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 grid[100][100] = {};
  lint dp[100][100][4] = {};

  lint R, C, K;
  cin >> R >> C >> K;

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

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