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

#include <iostream>
#include <queue>
#include <vector>
// #include<unordered_map>
int main() {
  int R, C, K;
  std::cin >> R >> C >> K;
  std::vector<std::vector<unsigned long long>> mat(
      R, std::vector<unsigned long long>(C, 0));
  std::vector<std::vector<bool>> flags(R, std::vector<bool>(C, 1));
  std::vector<std::vector<std::vector<unsigned long long>>> dp(
      R, std::vector<std::vector<unsigned long long>>(
             C, std::vector<unsigned long long>(4, 0)));
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    std::cin >> r >> c >> v;
    mat[r - 1][c - 1] = v;
  }
  std::queue<std::pair<int, int>> searchQ;
  searchQ.push(std::pair<int, int>(0, 0));
  flags[0][0] = 0;
  // dp[0][0][1]=mat[0][0];
  while (!searchQ.empty()) {
    std::pair<int, int> index = searchQ.front();
    int y = index.first, x = index.second;
    searchQ.pop();
    if (index.second + 1 < C) {
      for (int i = 0; i < 4; ++i) {
        if (dp[y][x + 1][i] < dp[y][x][i]) {
          dp[y][x + 1][i] = dp[y][x][i];
        }
      }
      if (mat[y][x] > 0) {
        for (int i = 0; i < 3; ++i) {
          unsigned long long buf = (dp[y][x][i] + mat[y][x]);
          if (dp[y][x + 1][i + 1] < buf) {
            dp[y][x + 1][i + 1] = buf;
          }
        }
      }
      if (flags[y][x + 1] == 1) {
        searchQ.push(std::pair<int, int>(y, x + 1));
        flags[y][x + 1] = 0;
      }
    }
    if (index.first + 1 < R) {
      unsigned long long max = 0;
      for (int i = 0; i < 4; ++i) {
        unsigned long long buf = dp[y][x][i];
        if (mat[y][x] > 0 && i < 3) {
          buf += mat[y][x];
        }
        if (max < buf) {
          max = buf;
        }
      }
      dp[y + 1][x][0] = max;
      if (flags[y + 1][x] == 1) {
        searchQ.push(std::pair<int, int>(y + 1, x));
        flags[y + 1][x] = 0;
      }
    }
  }
  unsigned long long ans = 0;
  for (int i = 0; i < 4; ++i) {
    unsigned long long buf = dp[R - 1][C - 1][i];
    if (mat[R - 1][C - 1] && i < 3) {
      buf += mat[R - 1][C - 1];
    }
    if (ans < buf) {
      ans = buf;
    }
  }
  std::cout << ans << std::endl;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
// #include<unordered_map>
int main() {
  int R, C, K;
  std::cin >> R >> C >> K;
  std::vector<std::vector<unsigned long long>> mat(
      R, std::vector<unsigned long long>(C, 0));
  std::vector<std::vector<bool>> flags(R, std::vector<bool>(C, 1));
  std::vector<std::vector<std::vector<unsigned long long>>> dp(
      R, std::vector<std::vector<unsigned long long>>(
             C, std::vector<unsigned long long>(4, 0)));
  for (int i = 0; i < K; ++i) {
    int r, c, v;
    std::cin >> r >> c >> v;
    mat[r - 1][c - 1] = v;
  }
  std::queue<std::pair<int, int>> searchQ;
  searchQ.push(std::pair<int, int>(0, 0));
  flags[0][0] = 0;
  // dp[0][0][1]=mat[0][0];
  while (!searchQ.empty()) {
    std::pair<int, int> index = searchQ.front();
    int y = index.first, x = index.second;
    searchQ.pop();
    if (index.second + 1 < C) {
      for (int i = 0; i < 4; ++i) {
        if (dp[y][x + 1][i] < dp[y][x][i]) {
          dp[y][x + 1][i] = dp[y][x][i];
        }
      }
      if (mat[y][x] > 0) {
        for (int i = 0; i < 3; ++i) {
          unsigned long long buf = (dp[y][x][i] + mat[y][x]);
          if (dp[y][x + 1][i + 1] < buf) {
            dp[y][x + 1][i + 1] = buf;
          }
        }
      }
      if (flags[y][x + 1] == 1) {
        searchQ.push(std::pair<int, int>(y, x + 1));
        flags[y][x + 1] = 0;
      }
    }
    if (index.first + 1 < R) {
      unsigned long long max = 0;
      for (int i = 0; i < 4; ++i) {
        unsigned long long buf = dp[y][x][i];
        if (mat[y][x] > 0 && i < 3) {
          buf += mat[y][x];
        }
        if (max < buf) {
          max = buf;
        }
      }
      dp[y + 1][x][0] = max;
      if (flags[y + 1][x] == 1) {
        searchQ.push(std::pair<int, int>(y, x + 1));
        flags[y + 1][x] = 0;
      }
    }
  }
  unsigned long long ans = 0;
  for (int i = 0; i < 4; ++i) {
    unsigned long long buf = dp[R - 1][C - 1][i];
    if (mat[R - 1][C - 1] && i < 3) {
      buf += mat[R - 1][C - 1];
    }
    if (ans < buf) {
      ans = buf;
    }
  }
  std::cout << ans << std::endl;
}