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

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int64_t item[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      item[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    int64_t v;
    cin >> a >> b >> v;
    item[a - 1][b - 1] = v;
  }

  vector<vector<vector<int64_t>>> table;
  // for(int i=0; i<c; i++) {
  //   for(int j=0; j<4; j++) table[0][i].push_back(0);
  // }
  for (int i = 0; i < r; i++) {
    vector<vector<int64_t>> tmp;
    for (int j = 0; j < c; j++) {
      tmp.push_back({0, 0, 0, 0});
    }
    table.push_back(tmp);
  }
  table[0][0][1] = item[0][0];

  for (int i = 1; i < c; i++) {
    table[0][i][0] = table[0][i - 1][0];
    table[0][i][1] = max(table[0][i - 1][1], table[0][i - 1][0] + item[0][i]);
    table[0][i][2] = max(table[0][i - 1][2], table[0][i - 1][1] + item[0][i]);
    table[0][i][3] = max(table[0][i - 1][3], table[0][i - 1][2] + item[0][i]);
  }

  for (int i = 1; i < r; i++) {
    table[i][0][0] =
        *max_element(table[i - 1][0].begin(), table[i - 1][0].end());
    table[i][0][1] = table[i][0][0] + item[i][0];
    // table[i][0][2] = 0;
    // table[i][0][3] = 0;
    for (int j = 1; j < c; j++) {
      table[i][j][0] =
          max(*max_element(table[i - 1][j].begin(), table[i - 1][j].end()),
              table[i][j - 1][0]);
      int64_t tmp2 =
          max(*max_element(table[i - 1][j].begin(), table[i - 1][j].end()) +
                  item[i][j],
              table[i][j - 1][1]);
      table[i][j][1] = max(tmp2, table[i][j - 1][0] + item[i][j]);
      table[i][j][2] = max(table[i][j - 1][2], table[i][j - 1][1] + item[i][j]);
      table[i][j][3] = max(table[i][j - 1][3], table[i][j - 1][2] + item[i][j]);
    }
  }

  int64_t ans =
      *max_element(table[r - 1][c - 1].begin(), table[r - 1][c - 1].end());

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int64_t item[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      item[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    int64_t v;
    cin >> a >> b >> v;
    item[a - 1][b - 1] = v;
  }

  // using masu = tuple<int64_t, int64_t, int64_t, int64_t>;
  // vector<vector<masu>> table;
  vector<vector<vector<int64_t>>> table;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < 4; j++)
      table[0][i].push_back(0);
  }
  table[0][0][1] = item[0][0];

  for (int i = 1; i < c; i++) {
    table[0][i][0] = table[0][i - 1][0];
    table[0][i][1] = max(table[0][i - 1][1], table[0][i - 1][0] + item[0][i]);
    table[0][i][2] = max(table[0][i - 1][2], table[0][i - 1][1] + item[0][i]);
    table[0][i][3] = max(table[0][i - 1][3], table[0][i - 1][2] + item[0][i]);
  }

  for (int i = 1; i < r; i++) {
    table[i][0][0] =
        *max_element(table[i - 1][0].begin(), table[i - 1][0].end());
    table[i][0][1] = 0;
    table[i][0][2] = 0;
    table[i][0][3] = 0;
    for (int j = 1; j < c; j++) {
      table[i][j][0] =
          max(*max_element(table[i - 1][j].begin(), table[i - 1][j].end()),
              table[i][j - 1][0]);
      table[i][j][1] = max(table[i][j - 1][1], table[i][j - 1][0] + item[i][j]);
      table[i][j][2] = max(table[i][j - 1][2], table[i][j - 1][1] + item[i][j]);
      table[i][j][3] = max(table[i][j - 1][3], table[i][j - 1][2] + item[i][j]);
    }
  }

  int64_t ans =
      *max_element(table[r - 1][c - 1].begin(), table[r - 1][c - 1].end());

  cout << ans << endl;

  return 0;
}