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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 2000000000
typedef long long ll;

ll D[3000][3000], DP[3000][3000][4];

ll smax(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

ll smax2(ll *a) { return smax(smax(a[0], a[1]), smax(a[2], a[3])); }

int main() {
  int R, C, K;
  cin >> R >> C;
  cin >> K;

  REP(i, 3000) {
    REP(j, 3000) { D[i][j] = 0; }
  }

  int r, c;
  ll v;

  for (int i = 0; i < K; i++) {
    cin >> r >> c;
    cin >> v;
    D[r - 1][c - 1] = v;
    // cout << "a" << endl;
  }

  REP(i, 3000) {
    REP(j, 3000) {
      REP(k, 4) { DP[i][j][k] = 0; }
    }
  }

  // cout << "aa" << endl;

  REP(i, R) {
    REP(j, C) {
      if (i != 0 && j != 0) {
        DP[i][j][0] = smax(smax2(DP[i - 1][j]), DP[i][j - 1][0]);
        DP[i][j][1] =
            smax(DP[i][j - 1][0] + D[i][j],
                 smax(DP[i][j - 1][1], smax2(DP[i - 1][j]) + D[i][j]));
        DP[i][j][2] = smax(DP[i][j - 1][1] + D[i][j], DP[i][j - 1][2]);
        DP[i][j][3] = smax(DP[i][j - 1][2] + D[i][j], DP[i][j - 1][3]);
      } else if (i != 0 && j == 0) {
        DP[i][j][0] = smax2(DP[i - 1][j]);
        DP[i][j][1] = smax2(DP[i - 1][j]) + D[i][j];
      } else if (i == 0 && j != 0) {
        DP[i][j][0] = DP[i][j - 1][0];
        DP[i][j][1] = smax(DP[i][j - 1][0] + D[i][j], DP[i][j - 1][1]);
        DP[i][j][2] = smax(DP[i][j - 1][1] + D[i][j], DP[i][j - 1][2]);
        DP[i][j][3] = smax(DP[i][j - 1][2] + D[i][j], DP[i][j - 1][3]);
      } else {
        DP[i][j][0] = 0;
        DP[i][j][1] = D[i][j];
      }
    }
  }
  cout << smax2(DP[R - 1][C - 1]) << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 2000000000
typedef long long ll;

ll smax(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

ll smax2(ll *a) { return smax(smax(a[0], a[1]), smax(a[2], a[3])); }

int main() {
  int R, C, K;
  cin >> R >> C;
  cin >> K;
  ll D[3000][3000];

  REP(i, 3000) {
    REP(j, 3000) { D[i][j] = 0; }
  }

  int r, c;
  ll v;

  for (int i = 0; i < K; i++) {
    cin >> r >> c;
    cin >> v;
    D[r - 1][c - 1] = v;
    // cout << "a" << endl;
  }

  ll DP[3000][3000][4];

  REP(i, 3000) {
    REP(j, 3000) {
      REP(k, 4) { DP[i][j][k] = 0; }
    }
  }

  // cout << "aa" << endl;

  REP(i, R) {
    REP(j, C) {
      if (i != 0 && j != 0) {
        DP[i][j][0] = smax(smax2(DP[i - 1][j]), DP[i][j - 1][0]);
        DP[i][j][1] =
            smax(DP[i][j - 1][0] + D[i][j],
                 smax(DP[i][j - 1][1], smax2(DP[i - 1][j]) + D[i][j]));
        DP[i][j][2] = smax(DP[i][j - 1][1] + D[i][j], DP[i][j - 1][2]);
        DP[i][j][3] = smax(DP[i][j - 1][2] + D[i][j], DP[i][j - 1][3]);
      } else if (i != 0 && j == 0) {
        DP[i][j][0] = smax2(DP[i - 1][j]);
        DP[i][j][1] = smax2(DP[i - 1][j]) + D[i][j];
      } else if (i == 0 && j != 0) {
        DP[i][j][0] = DP[i][j - 1][0];
        DP[i][j][1] = smax(DP[i][j - 1][0] + D[i][j], DP[i][j - 1][1]);
        DP[i][j][2] = smax(DP[i][j - 1][1] + D[i][j], DP[i][j - 1][2]);
        DP[i][j][3] = smax(DP[i][j - 1][2] + D[i][j], DP[i][j - 1][3]);
      } else {
        DP[i][j][0] = 0;
        DP[i][j][1] = D[i][j];
      }
    }
  }
  cout << smax2(DP[R - 1][C - 1]) << endl;
}