Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

void solve() {
  ll r, c, k;
  cin >> r >> c >> k;

  ll a[r + 3][c + 3];
  memset(a, 0, sizeof(a));

  ll dp[r + 3][c + 3][4];
  memset(dp, 0, sizeof(dp));

  for (ll i = 0; i < k; i++) {
    ll t1, t2, t3;
    cin >> t1 >> t2 >> t3;
    a[t1][t2] = t3;
  }

  // cout<<dp[1][1];

  for (ll i = r; i >= 1; i--) {
    for (ll j = c; j >= 1; j--) {
      // for(ll k1 = 0;k1<4;k1++){
      //     dp[i][j][k1] = max(dp[i+1][j][k1],dp[i][j+1][k1]);
      // }
      // if(a[i][j]){
      //     dp[i][j][0] = max()
      // }

      dp[i][j][0] = max({dp[i + 1][j][0], dp[i + 1][j][1], dp[i + 1][j][2],
                         dp[i + 1][j][3], dp[i][j + 1][0]});
      dp[i][j][1] = max(
          {dp[i][j][0] + a[i][j], dp[i][j + 1][0] + a[i][j], dp[i][j + 1][1]});
      dp[i][j][2] = max(dp[i][j + 1][2], dp[i][j + 1][1] + a[i][j]);
      dp[i][j][3] = max(dp[i][j + 1][3], dp[i][j + 1][2] + a[i][j]);
    }
  }

  // cout<<dp[1][2][2]<<endl;

  cout << max({dp[1][1][0], dp[1][1][1], dp[1][1][2], dp[1][1][3]});
}

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(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

void solve() {
  ll r, c, k;
  cin >> r >> c >> k;

  ll a[r + 3][c + 3];
  memset(a, 0, sizeof(a));

  vector<vector<vector<ll>>> dp(r + 3,
                                vector<vector<ll>>(c + 3, vector<ll>(4)));
  // memset(dp,0,sizeof(dp));

  for (ll i = 0; i < k; i++) {
    ll t1, t2, t3;
    cin >> t1 >> t2 >> t3;
    a[t1][t2] = t3;
  }

  // cout<<dp[1][1];

  for (ll i = r; i >= 1; i--) {
    for (ll j = c; j >= 1; j--) {
      // for(ll k1 = 0;k1<4;k1++){
      //     dp[i][j][k1] = max(dp[i+1][j][k1],dp[i][j+1][k1]);
      // }
      // if(a[i][j]){
      //     dp[i][j][0] = max()
      // }

      dp[i][j][0] = max({dp[i + 1][j][0], dp[i + 1][j][1], dp[i + 1][j][2],
                         dp[i + 1][j][3], dp[i][j + 1][0]});
      dp[i][j][1] = max(
          {dp[i][j][0] + a[i][j], dp[i][j + 1][0] + a[i][j], dp[i][j + 1][1]});
      dp[i][j][2] = max(dp[i][j + 1][2], dp[i][j + 1][1] + a[i][j]);
      dp[i][j][3] = max(dp[i][j + 1][3], dp[i][j + 1][2] + a[i][j]);
    }
  }

  // cout<<dp[1][2][2]<<endl;

  cout << max({dp[1][1][0], dp[1][1][1], dp[1][1][2], dp[1][1][3]});
}

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(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}