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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;
ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod;
  else
    return (((a) % mod) *
            (power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) %
           mod;
}
inline ll inverse(ll a, ll md) {
  a %= md;
  if (a < 0)
    a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += md;
  return u;
}

// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
// mt19937
// mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); //for
// randomized solution
const int ce = 1e9 + 7;
ll fast_mod(ll input) { return input < ce ? input : input % ce; }
int a[3001 + 1][3001 + 1];
int dp[3002][3002][4];
int32_t main() {
  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
#endif

  int r, c, k;
  cin >> r >> c >> k;

  memset(a, 0, sizeof(a));
  for (int i = 1; i <= k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    a[x][y] = val;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = dp[i][j - 1][k];
      }
      for (int k = 0; k <= 3; k++) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
      }
      if (a[i][j] != 0) {
        for (int k = 0; k <= 3; k++) {
          dp[i][j][1] = max(dp[i - 1][j][k] + a[i][j], dp[i][j][1]);
        }
        for (int k = 1; k <= 3; k++) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k <= 3; k++)
    ans = max(ans, dp[r][c][k]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;
ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod;
  else
    return (((a) % mod) *
            (power(((((a) % mod) * ((a) % mod)) % mod), b / 2) % mod)) %
           mod;
}
inline ll inverse(ll a, ll md) {
  a %= md;
  if (a < 0)
    a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += md;
  return u;
}

// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
// mt19937
// mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); //for
// randomized solution
const int ce = 1e9 + 7;
ll fast_mod(ll input) { return input < ce ? input : input % ce; }

int32_t main() {
  IOS

      int r,
      c, k;
  cin >> r >> c >> k;
  int a[r + 1][c + 1];
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= k; i++) {
    int x, y, val;
    cin >> x >> y >> val;
    a[x][y] = val;
  }
  int dp[r + 1][c + 1][4];
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = dp[i][j - 1][k];
      }
      for (int k = 0; k <= 3; k++) {
        dp[i][j][0] = dp[i - 1][j][k];
      }
      if (a[i][j] != 0) {
        for (int k = 0; k <= 3; k++) {
          dp[i][j][1] = max(dp[i - 1][j][k] + a[i][j], dp[i][j][1]);
        }
        for (int k = 1; k <= 3; k++) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + a[i][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k <= 3; k++)
    ans = max(ans, dp[r][c][k]);
  cout << ans << endl;
}