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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define INF 100000000000000000ll
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}

typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll a[3005][3005];
ll dp[3005][3005][4];

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll n, m, k;
  cin >> n >> m >> k;

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      a[i][j] = 0;
    }
  }

  for (int i = 0; i < k; ++i) {
    ll a1, b1, c1;
    cin >> a1 >> b1 >> c1;
    a[a1][b1] = c1;
  }

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      for (int k = 0; k < 4; ++k) {
        dp[i][j][k] = 0;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j]) {
        dp[i][j][0] = 0;
        dp[i][j][1] = max(dp[i][j - 1][1],
                          a[i][j] + 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] =
            max(dp[i][j][1], max(dp[i][j - 1][1], a[i][j] + dp[i][j - 1][0]));
        dp[i][j][2] = max(dp[i][j - 1][2], a[i][j] + dp[i][j - 1][1]);
        dp[i][j][3] = max(dp[i][j - 1][3], a[i][j] + dp[i][j - 1][2]);
      } else {
        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] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
        dp[i][j][3] = dp[i][j - 1][3];
      }
    }
  }

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

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define INF 100000000000000000ll
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}

typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
#define MAXN 200005
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output

int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ll n, m, k;
  cin >> n >> m >> k;
  ll a[n + 1][m + 1];
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      a[i][j] = 0;
    }
  }

  for (int i = 0; i < k; ++i) {
    ll a1, b1, c1;
    cin >> a1 >> b1 >> c1;
    a[a1][b1] = c1;
  }

  ll dp[n + 1][m + 1][4];
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      for (int k = 0; k < 4; ++k) {
        dp[i][j][k] = 0;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (a[i][j]) {
        dp[i][j][0] = 0;
        dp[i][j][1] = max(dp[i][j - 1][1],
                          a[i][j] + 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] = max(dp[i][j][1], max(dp[i][j-1][1], a[i][j] +
        // dp[i][j-1][0]));
        dp[i][j][2] = max(dp[i][j - 1][2], a[i][j] + dp[i][j - 1][1]);
        dp[i][j][3] = max(dp[i][j - 1][3], a[i][j] + dp[i][j - 1][2]);
      } else {
        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] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
        dp[i][j][3] = dp[i][j - 1][3];
      }
    }
  }

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

  return 0;
}