Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
#define br "\n"
#define MAX 1000001
#define ff first
#define ss second

vector<vll> v;
ll dp[3001][3001][4];

void solve() {
  ll n, m, cunt;
  cin >> n >> m >> cunt;
  v.resize(n + 1, vll(m + 1, 0));
  ll dp[n + 1][m + 1][4];
  rep(i, 0, n + 1) {
    rep(j, 0, m + 1) { rep(k, 0, 4) dp[i][j][k] = 0; }
  }
  rep(i, 0, cunt) {
    ll a, b, val;
    cin >> a >> b >> val;
    v[a][b] = val;
  }
  // for(auto x:v){
  //     for(auto y:x) cout << y << " ";
  //     cout << br;
  // }

  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      ll mx = 0;
      rep(k, 1, 4) mx = max(dp[i - 1][j][k], mx);
      rep(k, 1, 4) dp[i][j][k] = max(
          mx + v[i][j], max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + v[i][j]));
      // cout << dp[i][j][1] << " ";
    }
    // cout << br;
  }
  // for(auto x:sum){
  //     for(auto y:x) cout << y << " ";
  //     cout << br;
  // }
  ll ans = 0;
  rep(i, 0, 4) ans = max(ans, dp[n][m][i]);
  cout << ans << br;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin >> t;
  rep(i, 0, t) { solve(); }
}

Fixed submission:
#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define pre(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<bool> vb;
#define br "\n"
#define MAX 1000001
#define ff first
#define ss second

vector<vll> v;
ll dp[3001][3001][4];

void solve() {
  ll n, m, cunt;
  cin >> n >> m >> cunt;
  v.resize(n + 1, vll(m + 1, 0));
  rep(i, 0, n + 1) {
    rep(j, 0, m + 1) { rep(k, 0, 4) dp[i][j][k] = 0; }
  }
  rep(i, 0, cunt) {
    ll a, b, val;
    cin >> a >> b >> val;
    v[a][b] = val;
  }
  // for(auto x:v){
  //     for(auto y:x) cout << y << " ";
  //     cout << br;
  // }

  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      ll mx = 0;
      rep(k, 1, 4) mx = max(dp[i - 1][j][k], mx);
      rep(k, 1, 4) dp[i][j][k] = max(
          mx + v[i][j], max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + v[i][j]));
      // cout << dp[i][j][1] << " ";
    }
    // cout << br;
  }
  // for(auto x:sum){
  //     for(auto y:x) cout << y << " ";
  //     cout << br;
  // }
  ll ans = 0;
  rep(i, 0, 4) ans = max(ans, dp[n][m][i]);
  cout << ans << br;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin >> t;
  rep(i, 0, t) { solve(); }
}