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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define f0(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define f2(i, a, b) for (int i = a; i > b; i--)
#define f3(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define int long long
#define fi first
#define se second
#define ncr(n, r) fact[n] * modInv(fact[r]) * modInv(fact[n - r])
#define modInv(a) binExp(a, mod - 2)
#define mod 1000000007
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define make_graph(k)                                                          \\
  int x, y;                                                                    \\
  f0(i, 0, k) {                                                                \\
    cin >> x >> y;                                                             \\
    adj[x].pb(y);                                                              \\
    adj[y].pb(x);                                                              \\
  }
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

int mat[3001][3001];
int dp[3001][3001][3];

signed main() {
  fast

#ifndef ONLINE_JUDGE
      freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif

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

  f1(i, 0, r) f1(j, 0, c) mat[i][j] = 0;
  int x, y, v;
  f0(z, 0, k) {
    cin >> x >> y >> v;
    mat[x][y] = v;
  }

  f1(i, 0, r) dp[i][0][0] = dp[i][0][1] = dp[i][0][2] = 0;
  f1(j, 0, c) dp[0][j][0] = dp[0][j][1] = dp[0][j][2] = 0;
  f1(j, 1, c) dp[1][j][0] = max(dp[1][j - 1][0], mat[1][j]);
  f1(j, 1, c) {
    dp[1][j][1] = max(dp[1][j - 1][1], dp[1][j - 1][0] + mat[1][j]);
    dp[1][j][2] = max(dp[1][j - 1][2], dp[1][j - 1][1] + mat[1][j]);
  }

  f1(i, 2, r) {
    f1(j, 1, c) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][2] + mat[i][j]);

      dp[i][j][1] = max(dp[i][j - 1][1], dp[i - 1][j][2] + mat[i][j]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + mat[i][j]);

      dp[i][j][2] = max(dp[i][j - 1][2], dp[i - 1][j][2] + mat[i][j]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + mat[i][j]);
    }
  }

  // f1(i,1,r)
  // {
  // 	f1(j,1,c)
  // 	{
  // 		cout<<dp[i][j][2]<<' ';
  // 	}
  // 	cout<<"\\n";
  // }

  cout << dp[r][c][2];
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define f0(i, a, b) for (int i = a; i < b; i++)
#define f1(i, a, b) for (int i = a; i <= b; i++)
#define f2(i, a, b) for (int i = a; i > b; i--)
#define f3(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define int long long
#define fi first
#define se second
#define ncr(n, r) fact[n] * modInv(fact[r]) * modInv(fact[n - r])
#define modInv(a) binExp(a, mod - 2)
#define mod 1000000007
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define make_graph(k)                                                          \\
  int x, y;                                                                    \\
  f0(i, 0, k) {                                                                \\
    cin >> x >> y;                                                             \\
    adj[x].pb(y);                                                              \\
    adj[y].pb(x);                                                              \\
  }
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

signed main() {
  fast

#ifndef ONLINE_JUDGE
      freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif

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

  int mat[r + 1][c + 1];
  f1(i, 0, r) f1(j, 0, c) mat[i][j] = 0;
  int x, y, v;
  f0(z, 0, k) {
    cin >> x >> y >> v;
    mat[x][y] = v;
  }

  int dp[r + 1][c + 1][3];
  f1(i, 0, r) dp[i][0][0] = dp[i][0][1] = dp[i][0][2] = 0;
  f1(j, 0, c) dp[0][j][0] = dp[0][j][1] = dp[0][j][2] = 0;
  f1(j, 1, c) dp[1][j][0] = max(dp[1][j - 1][0], mat[1][j]);
  f1(j, 1, c) {
    dp[1][j][1] = max(dp[1][j - 1][1], dp[1][j - 1][0] + mat[1][j]);
    dp[1][j][2] = max(dp[1][j - 1][2], dp[1][j - 1][1] + mat[1][j]);
  }

  f1(i, 2, r) {
    f1(j, 1, c) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][2] + mat[i][j]);

      dp[i][j][1] = max(dp[i][j - 1][1], dp[i - 1][j][2] + mat[i][j]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + mat[i][j]);

      dp[i][j][2] = max(dp[i][j - 1][2], dp[i - 1][j][2] + mat[i][j]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + mat[i][j]);
    }
  }
  cout << dp[r][c][2];
}