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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define ff first
#define ss second
#define double long double
#define prec(y, x) fixed << setprecision(y) << x
#define inf 1e18
#define pi 3.1415926535

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> arr;
  vector<vector<vector<ll>>> dp;
  arr.resize(r + 1);
  dp.resize(r + 1);
  for (ll i = 0; i <= r; i++) {
    arr[i].resize(c + 1, 0);
    dp[i].resize(c + 1);
    for (ll j = 0; j <= c; j++)
      dp[i][j].resize(4, 0);
  }
  // ll arr[r + 1][c + 1]; ll dp[r + 1][c + 1][4];
  for (ll i = 0; i <= r; i++) {
    for (ll j = 0; j <= c; j++) {
      arr[i][j] = 0;
      for (ll k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  while (k--) {
    ll r1, c1, val;
    cin >> r1 >> c1 >> val;
    arr[r1][c1] = val;
  }
  for (ll i = 1; i <= r; i++) {
    for (ll j = 1; j <= c; j++) {
      vector<ll> v;
      v.resize(4, 0);
      if (i == 1 && j == 1) {
        if (arr[i][j] != 0)
          dp[i][j][1] = arr[i][j];
      } else if (i == 1) {

        if (arr[i][j] != 0) {
          dp[i][j][1] = max(dp[i][j - 1][0] + arr[i][j], dp[i][j - 1][1]);
          dp[i][j][2] = max(dp[i][j - 1][1] + arr[i][j], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j - 1][2] + arr[i][j], dp[i][j - 1][3]);
        } else {
          for (ll k = 0; k < 4; k++)
            dp[i][j][k] = dp[i][j - 1][k];
        }
      } else if (j == 1) {
        if (arr[i][j] != 0) {
          ll ans1 = 0;
          for (ll k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][0] = ans1;
          dp[i][j][1] = arr[i][j] + ans1;
        } else {
          ll ans1 = 0;
          for (ll k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][0] = ans1;
        }
      } else {
        if (arr[i][j] == 0) {
          ll ans1 = 0;
          for (ll k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          for (ll k = 0; k < 4; k++)
            dp[i][j][k] = max(ans1, dp[i][j - 1][k]);
        } else {
          ll ans1 = 0;
          for (ll k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][0] = max(dp[i][j - 1][0], ans1);
          dp[i][j][1] = arr[i][j] + max(ans1, dp[i][j - 1][0]);
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
          dp[i][j][2] = max(dp[i][j - 1][2], arr[i][j] + dp[i][j - 1][1]);
          dp[i][j][3] = max(dp[i][j - 1][3], arr[i][j] + dp[i][j - 1][2]);
          // dp[i][j][3] = max(ans1, dp[i][j - 1][2]);
        }
      }
    }
  }
  // for (ll k = 0; k < 4; k++) {
  // 	for (ll i = 1; i <= r; i++)
  // 	{
  // 		for (ll j = 1; j <= c; j++)
  // 		{  cout << dp[i][j][k] << " ";}
  // 		cout << endl;
  // 	}
  // 	cout << endl << endl;
  // }
  ll ans = 0;
  for (ll k = 0; k < 4; k++)
    ans = max(ans, dp[r][c][k]);
  cout << ans;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define ff first
#define ss second
#define double long double
#define prec(y, x) fixed << setprecision(y) << x
#define inf 1e18
#define pi 3.1415926535

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int r, c, k;
  cin >> r >> c >> k;
  ll arr[r + 1][c + 1];
  ll dp[r + 1][c + 1][4];
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      arr[i][j] = 0;
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  while (k--) {
    int r1, c1, val;
    cin >> r1 >> c1 >> val;
    arr[r1][c1] = val;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      vector<ll> v;
      v.resize(4, 0);
      if (i == 1 && j == 1 && arr[i][j] != 0) {
        dp[i][j][1] = arr[i][j];
      } else if (i == 1) {

        if (arr[i][j] != 0) {
          dp[i][j][1] = max(dp[i][j - 1][0] + arr[i][j], dp[i][j - 1][1]);
          dp[i][j][2] = max(dp[i][j - 1][1] + arr[i][j], dp[i][j - 1][2]);
          dp[i][j][3] = max(dp[i][j - 1][2] + arr[i][j], dp[i][j - 1][3]);
        } else {
          for (int k = 0; k < 4; k++)
            dp[i][j][k] = dp[i][j - 1][k];
        }
      } else if (j == 1) {
        if (arr[i][j] != 0) {
          dp[i][j][0] = dp[i - 1][j][0];
          ll ans1 = 0;
          for (int k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][1] = arr[i][j] + ans1;
        } else {
          ll ans1 = 0;
          for (int k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][0] = ans1;
        }
      } else {
        if (arr[i][j] == 0) {
          ll ans1 = 0;
          for (int k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          for (int k = 0; k < 4; k++)
            dp[i][j][k] = max(ans1, dp[i][j - 1][k]);
        } else {
          ll ans1 = 0;
          for (int k = 0; k < 4; k++)
            ans1 = max(ans1, dp[i - 1][j][k]);
          dp[i][j][0] = max(dp[i][j - 1][0], ans1);
          dp[i][j][1] = arr[i][j] + max(ans1, dp[i][j - 1][0]);
          dp[i][j][2] = arr[i][j] + max(ans1, dp[i][j - 1][1]);
          dp[i][j][3] = arr[i][j] + max(ans1, dp[i][j - 1][2]);
        }
      }
    }
  }
  // for (int k = 0; k < 4; k++) {
  // 	for (int i = 1; i <= r; i++)
  // 	{
  // 		for (int j = 1; j <= c; j++)
  // 		{  cout << dp[i][j][k] << " ";}
  // 		cout << endl;
  // 	}
  // 	cout << endl << endl;
  // }
  ll ans = 0;
  for (int k = 0; k < 4; k++)
    ans = max(ans, dp[r][c][k]);
  cout << ans;
  return 0;
}