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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <iomanip>
#include <iostream>
#include <limits>
#include <map> // map
#include <numeric>
#include <queue> // queue, priority_queue
#include <set>   // set
#include <stack> // stack
#include <stdio.h>
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) { return max(max(a, b), c); }
ll Min(ll(a), ll(b), ll(c)) { return min(min(a, b), c); }
ll v[3002][3002];
ll s[4][3002][3002];
int main() {
  ll R, C, K;
  cin >> R >> C >> K;

  rep(i, 3002) {
    rep(j, 3002) { v[i][j] = 0; }
  }
  rep(i, K) {
    ll r, c;
    cin >> r >> c;
    cin >> v[r - 1][c - 1];
  }

  s[0][0][0] = 0;
  s[1][0][0] = 0;
  s[2][0][0] = 0;
  s[3][0][0] = 0;
  if (v[0][0] > 0) {
    s[1][0][0] = v[0][0];
  }
  rep(i, R) {
    if (i != 0) {
      if (v[i][0] == 0) {

        s[0][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]);
        rep(k, 3) { s[k + 1][i][0] = 0; }

      } else {
        s[0][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]);
        s[1][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]) + v[i][0];
        s[2][i][0] = 0;
        s[3][i][0] = 0;
      }
    }
    for (ll j = 1; j < C; j++) {
      if (v[i][j] == 0) {
        rep(k, 4) {

          s[k][i][j] = s[k][i][j - 1];

          if (i != 0) {
            rep(d, 4) { s[0][i][j] = max(s[0][i][j], s[d][i - 1][j]); }
          }
        }
      } else {

        s[0][i][j] = s[0][i][j - 1];
        s[1][i][j] = max(s[0][i][j - 1] + v[i][j], s[1][i][j - 1]);
        s[2][i][j] = max(s[1][i][j - 1] + v[i][j], s[2][i][j - 1]);
        s[3][i][j] = max(s[2][i][j - 1] + v[i][j], s[3][i][j - 1]);
        if (i != 0) {
          rep(d, 4) {
            s[0][i][j] = max(s[0][i][j], s[d][i - 1][j]);
            s[1][i][j] = max(s[1][i][j], s[d][i - 1][j] + v[i][j]);
          }
        }
      }
    }
  }
  ll an = 0;
  rep(i, 4) { an = max(an, s[i][R - 1][C - 1]); }
  cout << an << endl;
}


Error: -11

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <iomanip>
#include <iostream>
#include <limits>
#include <map> // map
#include <numeric>
#include <queue> // queue, priority_queue
#include <set>   // set
#include <stack> // stack
#include <stdio.h>
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) { return max(max(a, b), c); }
ll Min(ll(a), ll(b), ll(c)) { return min(min(a, b), c); }

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  ll v[3002][3002];
  rep(i, 3002) {
    rep(j, 3002) { v[i][j] = 0; }
  }
  rep(i, K) {
    ll r, c;
    cin >> r >> c;
    cin >> v[r - 1][c - 1];
  }
  ll s[4][3002][3002];
  s[0][0][0] = 0;
  s[1][0][0] = 0;
  s[2][0][0] = 0;
  s[3][0][0] = 0;
  if (v[0][0] > 0) {
    s[1][0][0] = v[0][0];
  }
  rep(i, R) {
    if (i != 0) {
      if (v[i][0] == 0) {

        s[0][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]);
        rep(k, 3) { s[k + 1][i][0] = 0; }

      } else {
        s[0][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]);
        s[1][i][0] = max(s[0][i - 1][0], s[1][i - 1][0]) + v[i][0];
        s[2][i][0] = 0;
        s[3][i][0] = 0;
      }
    }
    for (ll j = 1; j < C; j++) {
      if (v[i][j] == 0) {
        rep(k, 4) {

          s[k][i][j] = s[k][i][j - 1];

          if (i != 0) {
            rep(d, 4) { s[0][i][j] = max(s[0][i][j], s[d][i - 1][j]); }
          }
        }
      } else {

        s[0][i][j] = s[0][i][j - 1];
        s[1][i][j] = max(s[0][i][j - 1] + v[i][j], s[1][i][j - 1]);
        s[2][i][j] = max(s[1][i][j - 1] + v[i][j], s[2][i][j - 1]);
        s[3][i][j] = max(s[2][i][j - 1] + v[i][j], s[3][i][j - 1]);
        if (i != 0) {
          rep(d, 4) {
            s[0][i][j] = max(s[0][i][j], s[d][i - 1][j]);
            s[1][i][j] = max(s[1][i][j], s[d][i - 1][j] + v[i][j]);
          }
        }
      }
    }
  }
  ll an = 0;
  rep(i, 4) { an = max(an, s[i][R - 1][C - 1]); }
  cout << an << endl;
}