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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18

long solve(long r, long c, long m, std::vector<std::vector<long>> field) {
  std::vector<std::vector<std::vector<long>>> dp(
      r, std::vector<std::vector<long>>(
             c, std::vector<long>(5))); // when picked up k
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 5; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  // printf("pass\\n");
  dp[0][0][0] = 0;
  if (field[0][0] > 0) {
    dp[0][0][1] = field[0][0];
  }
  /*
  for(int i = 0; i < r; i++){
      for(int j = 0; j < c; j++){
          printf("(");
          for(int k = 0; k < 4; k++){
              printf("%2ld,", dp[i][j][k]);
          }
          printf("), ");
      }
      printf("\\n");
  }
  printf("\\n");
  //*/
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i > 0) {
        dp[i][j][0] = std::max(dp[i][j][0], dp[i - 1][j][4]);
        dp[i][j][1] = std::max(dp[i][j][1], dp[i - 1][j][4] + field[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j > 0) {
          dp[i][j][k] = std::max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k] =
                std::max(dp[i][j][k], dp[i][j - 1][k - 1] + field[i][j]);
          }
        }
        dp[i][j][4] = std::max(dp[i][j][4], dp[i][j][k]);
      }
    }
  }
  /*
  (int i = 0; i < r; i++){
      for(int j = 0; j < c; j++){
          printf("(");
          for(int k = 0; k < 5; k++){
              printf("%2ld,", dp[i][j][k]);
          }
          printf("), ");
      }
      printf("\\n");
  }
  //*/
  long ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = std::max(ans, dp[r - 1][c - 1][k]);
  }
  return ans;
}

int main() {
  long r, c, k;
  scanf("%ld%ld%ld", &r, &c, &k);
  std::vector<std::vector<long>> field(r, std::vector<long>(c, 0));
  for (int i = 0; i < k; i++) {
    long a, b, v;
    scanf("%ld%ld%ld", &a, &b, &v);
    field[a - 1][b - 1] = v;
  }

  printf("%ld\\n", solve(r, c, k, field));
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18

long solve(long r, long c, long m, std::vector<std::vector<long>> field) {
  long dp[r][c][5]; // when picked up k
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 5; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  dp[0][0][0] = 0;
  if (field[0][0] > 0) {
    dp[0][0][1] = field[0][0];
  }
  /*
  for(int i = 0; i < r; i++){
      for(int j = 0; j < c; j++){
          printf("(");
          for(int k = 0; k < 4; k++){
              printf("%2ld,", dp[i][j][k]);
          }
          printf("), ");
      }
      printf("\\n");
  }
  printf("\\n");
  //*/
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i > 0) {
        dp[i][j][0] = std::max(dp[i][j][0], dp[i - 1][j][4]);
        dp[i][j][1] = std::max(dp[i][j][1], dp[i - 1][j][4] + field[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j > 0) {
          dp[i][j][k] = std::max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k] =
                std::max(dp[i][j][k], dp[i][j - 1][k - 1] + field[i][j]);
          }
        }
        dp[i][j][4] = std::max(dp[i][j][4], dp[i][j][k]);
      }
    }
  }
  /*
  (int i = 0; i < r; i++){
      for(int j = 0; j < c; j++){
          printf("(");
          for(int k = 0; k < 5; k++){
              printf("%2ld,", dp[i][j][k]);
          }
          printf("), ");
      }
      printf("\\n");
  }
  //*/
  long ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = std::max(ans, dp[r - 1][c - 1][k]);
  }
  return ans;
}

int main() {
  long r, c, k;
  scanf("%ld%ld%ld", &r, &c, &k);
  std::vector<std::vector<long>> field(r, std::vector<long>(c, 0));
  for (int i = 0; i < k; i++) {
    long a, b, c;
    scanf("%ld%ld%ld", &a, &b, &c);
    field[a - 1][b - 1] = c;
  }
  printf("%ld\\n", solve(r, c, k, field));
}