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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
long long a[3001][3001];
long long dp[3001][3001][5];
int main() {
  ll r = 0;
  ll c = 0;
  ll n = 0;
  cin >> r >> c >> n;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    ll ir = 0;
    ll ic = 0;
    ll iv = 0;
    cin >> ir >> ic >> iv;
    ir--;
    ic--;
    a[ir][ic] = iv;
  }

  for (int i = 0; i < 3000; i++) {
    for (int j = 0; j < 3000; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  if (a[0][0] != 0) {
    for (int i = 1; i < 4; i++) {
      dp[0][0][i] = a[0][0]; // とる
    }
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        continue;
      }

      if (i != 0) {
        for (int k = 0; k < 4; k++) {
          dp[i][j][k] = max((ll)dp[i][j][k], (ll)dp[i - 1][j][3]); // とらない
        }
        if (a[i][j] != 0) {
          for (int k = 1; k < 4; k++) {
            dp[i][j][k] =
                max((ll)dp[i][j][k], (ll)(dp[i - 1][j][3] + a[i][j])); // とる
          }
        }
      }
      if (j != 0) {
        for (int k = 0; k < 4; k++) {
          dp[i][j][k] = max((ll)dp[i][j][k], (ll)dp[i][j - 1][k]); // とらない
        }
        if (a[i][j] != 0) {
          for (int k = 1; k < 4; k++) {
            dp[i][j][k] = max((ll)dp[i][j][k],
                              (ll)(dp[i][j - 1][k - 1] + a[i][j])); // とる
          }
        }
      }
    }
  }
  ll res = 0;
  res = dp[r - 1][c - 1][3];
  cout << res << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll r = 0;
  ll c = 0;
  ll n = 0;
  cin >> r >> c >> n;
  long long a[3001][3001];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    ll ir = 0;
    ll ic = 0;
    ll iv = 0;
    cin >> ir >> ic >> iv;
    ir--;
    ic--;
    a[ir][ic] = iv;
  }

  long long dp[3001][3001][5];
  for (int i = 0; i < 3000; i++) {
    for (int j = 0; j < 3000; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }

  if (a[0][0] != 0) {
    for (int i = 1; i < 4; i++) {
      dp[0][0][i] = a[0][0]; // とる
    }
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        continue;
      }

      if (i != 0) {
        for (int k = 0; k < 4; k++) {
          dp[i][j][k] = max((ll)dp[i][j][k], (ll)dp[i - 1][j][3]); // とらない
        }
        if (a[i][j] != 0) {
          for (int k = 1; k < 4; k++) {
            dp[i][j][k] =
                max((ll)dp[i][j][k], (ll)(dp[i - 1][j][3] + a[i][j])); // とる
          }
        }
      }
      if (j != 0) {
        for (int k = 0; k < 4; k++) {
          dp[i][j][k] = max((ll)dp[i][j][k], (ll)dp[i][j - 1][k]); // とらない
        }
        if (a[i][j] != 0) {
          for (int k = 1; k < 4; k++) {
            dp[i][j][k] = max((ll)dp[i][j][k],
                              (ll)(dp[i][j - 1][k - 1] + a[i][j])); // とる
          }
        }
      }
    }
  }
  ll res = 0;
  res = dp[r - 1][c - 1][3];
  cout << res << endl;
  return 0;
}