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

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

long long int Map[3000][3000] = {}, Score[3000][3000][4] = {};

int main() {

  long long int i, j, R, C, K, x, y, ans;
  cin >> R >> C >> K;

  for (i = 0; i < K; i++) {
    cin >> x >> y;
    x -= 1;
    y -= 1;
    cin >> Map[x][y];
  }

  for (j = 0; j < C; j++) {
    for (i = 0; i < R; i++) {

      if (Map[i][j] == 0) {

        if (i == 0 && j == 0) {

        } else if (i == 0) {
          Score[i][j][0] = Score[i][j - 1][0];
          Score[i][j][1] = Score[i][j - 1][1];
          Score[i][j][2] = Score[i][j - 1][2];
          Score[i][j][3] = Score[i][j - 1][3];

        } else if (j == 0) {
          Score[i][j][0] =
              max(Score[i - 1][j][0],
                  (max(Score[i - 1][j][1],
                       max(Score[i - 1][j][2], Score[i][j - 1][3]))));

        } else {
          Score[i][j][0] =
              max(Score[i][j - 1][0],
                  max(Score[i - 1][j][0],
                      max(Score[i - 1][j][1],
                          max(Score[i - 1][j][2], Score[i - 1][j][3]))));
          Score[i][j][1] = Score[i][j - 1][1];
          Score[i][j][2] = Score[i][j - 1][2];
          Score[i][j][3] = Score[i][j - 1][3];
        }

      } else {
        if (i == 0 && j == 0) {
          Score[i][j][1] = Map[i][j];

        } else if (i == 0) {
          Score[i][j][0] = Score[i][j - 1][0];
          Score[i][j][1] =
              max((Map[i][j] + Score[i][j - 1][0]), (Score[i][j - 1][1]));
          Score[i][j][2] =
              max((Map[i][j] + Score[i][j - 1][1]), (Score[i][j - 1][2]));
          Score[i][j][3] =
              max((Map[i][j] + Score[i][j - 1][2]), (Score[i][j - 1][3]));

        } else if (j == 0) {
          Score[i][j][0] =
              max(Score[i - 1][j][0],
                  (max(Score[i - 1][j][1],
                       max(Score[i - 1][j][2], Score[i - 1][j][3]))));
          Score[i][j][1] =
              max(Score[i - 1][j][0],
                  (max(Score[i - 1][j][1],
                       max(Score[i - 1][j][2], Score[i - 1][j][3])))) +
              Map[i][j];

        } else {
          Score[i][j][0] =
              max(Score[i][j - 1][0],
                  (max(Score[i - 1][j][0],
                       max(Score[i - 1][j][1],
                           max(Score[i - 1][j][2], Score[i - 1][j][3])))));
          Score[i][j][1] =
              max((Map[i][j] + Score[i][j][0]), Score[i][j - 1][1]);
          Score[i][j][2] =
              max((Map[i][j] + Score[i][j - 1][1]), Score[i][j - 1][2]);
          Score[i][j][3] =
              max((Map[i][j] + Score[i][j - 1][2]), Score[i][j - 1][3]);
        }
      }
    }
  }

  //    for (j = 0; j < C; j++){
  //        for (i = 0; i < R; i++){
  //
  //        cout << Score[i][j][0] << ' ' << Score[i][j][1] << ' '
  //            << Score[i][j][2] << ' ' << Score[i][j][3] << "\\n";
  //
  //        }
  //    }

  ans = max(Score[R - 1][C - 1][0],
            max(Score[R - 1][C - 1][1],
                max(Score[R - 1][C - 1][2], Score[R - 1][C - 1][3])));

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {

  int i, j, R, C, K, x, y, flag, item[5] = {0};
  cin >> R >> C >> K;

  int Map[30][30] = {0}, Score[30][30] = {0};

  for (i = 0; i < K; i++) {
    cin >> x >> y;
    x -= 1;
    y -= 1;
    cin >> Map[x][y];
  }

  for (j = 0; j < C; j++) {
    for (i = 0; i < R; i++) {

      if (i == 0 && j == 0) {
        Score[i][j] = Map[i][j];

      } else if (i == 0) {
        Score[i][j] = Map[i][j] + Score[i][j - 1];

      } else if (j == 0) {
        Score[i][j] = Map[i][j] + Score[i - 1][j];

      } else {
        Score[i][j] =
            max((Map[i][j] + Score[i - 1][j]), (Map[i][j] + Score[i][j - 1]));
      }
    }
  }

  cout << Score[R - 1][C - 1] << "\\n";

  return 0;
}