Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C, v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  ll dp[4][r + 1][c + 1];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < r + 1; j++) {
      for (int l = 0; l < c + 1; l++) {
        dp[i][j][l] = 0;
      }
    }
  }
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
      }
      dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
      dp[1][i][j] = dp[1][i][j - 1];
      dp[2][i][j] = dp[2][i][j - 1];
      dp[3][i][j] = dp[3][i][j - 1];
      if (memo[i][j] != 0) {
        dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
        dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[i][r][c]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

ll mod(ll a, ll b) {
  ll ret = a % b;
  if (ret < 0)
    ret += b;
  return ret;
}

ll modpow(ll a, ll b, ll c) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  int memo[r + 1][c + 1];
  for (int i = 0; i < r + 1; i++) {
    for (int j = 0; j < c + 1; j++) {
      memo[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int R, C, v;
    cin >> R >> C >> v;
    memo[R][C] = v;
  }
  vector<vector<vector<ll>>> dp(
      4, vector<vector<ll>>(r + 1, vector<ll>(c + 1, 0)));
  for (int i = 1; i < r + 1; i++) {
    for (int j = 1; j < c + 1; j++) {
      for (int l = 0; l < 4; l++) {
        dp[0][i][j] = max(dp[0][i][j], dp[l][i - 1][j]);
      }
      dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]);
      dp[1][i][j] = dp[1][i][j - 1];
      dp[2][i][j] = dp[2][i][j - 1];
      dp[3][i][j] = dp[3][i][j - 1];
      if (memo[i][j] != 0) {
        dp[1][i][j] = max(dp[1][i][j - 1], dp[0][i][j] + memo[i][j]);
        dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + memo[i][j]);
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + memo[i][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[i][r][c]);
  }
  cout << ans << endl;
  return 0;
}