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

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

ll dp[3001][3001][4];
vector<vl> grid(3001, vl(3001, 0));

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;

  while (k--) {
    ll a, b, cc;
    cin >> a >> b >> cc;
    grid[a - 1][b - 1] = cc;
  }

  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][1] = grid[0][0];
  dp[0][0][0] = 0;
  for (ll j = 1; j < c; j++) {
    dp[0][j][1] = max(dp[0][j - 1][1], dp[0][j - 1][0] + grid[0][j]);
    dp[0][j][2] = max(dp[0][j - 1][1] + grid[0][j], dp[0][j - 1][2]);
    if (j > 1)
      dp[0][j][3] = max(dp[0][j - 1][2] + grid[0][j], dp[0][j - 1][3]);
  }
  for (ll i = 1; i < r; i++) {
    dp[i][0][1] = grid[i][0];
  }
  for (ll i = 1; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll k = 0; k < 4; k++) {
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + grid[i][j]);
      }
      if (j >= 1)
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
      if (j >= 2)
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
      ll ma = dp[i][j][1];
      if (j >= 1)
        dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][0] + grid[i][j]);
      dp[i][j][1] = max(ma, dp[i][j][1]);
    }
  }
  ll ans = 0;
  for (ll k = 0; k < 4; k++)
    ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << "\\n";
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vl> grid(r, vl(c, 0));
  while (k--) {
    ll a, b, cc;
    cin >> a >> b >> cc;
    grid[a - 1][b - 1] = cc;
  }
  ll dp[r][c][4];
  for (ll i = 0; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][1] = grid[0][0];
  dp[0][0][0] = 0;
  for (ll j = 1; j < c; j++) {
    dp[0][j][1] = max(dp[0][j - 1][1], dp[0][j - 1][0] + grid[0][j]);
    dp[0][j][2] = max(dp[0][j - 1][1] + grid[0][j], dp[0][j - 1][2]);
    if (j > 1)
      dp[0][j][3] = max(dp[0][j - 1][2] + grid[0][j], dp[0][j - 1][3]);
  }
  for (ll i = 1; i < r; i++) {
    dp[i][0][1] = grid[i][0];
  }
  for (ll i = 1; i < r; i++) {
    for (ll j = 0; j < c; j++) {
      for (ll k = 0; k < 4; k++) {
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + grid[i][j]);
      }
      if (j >= 1)
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
      if (j >= 2)
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
    }
  }
  ll ans = 0;
  for (ll k = 0; k < 4; k++)
    ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << "\\n";
}