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


// Problem :
// 			E - Picking Goods
//
//
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int dp[3005][3005][4];
int a[3005][3005];
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  memset(dp, 0, sizeof(dp));
  memset(a, 0, sizeof(a));
  rep(i, 0, k) {
    int x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int maxi = dp[i - 1][j][0];
      maxi = max(dp[i - 1][j][1], maxi);
      maxi = max(dp[i - 1][j][2], maxi);
      maxi = max(dp[i - 1][j][3], maxi);
      if (a[i][j] == 0) {
        dp[i][j][0] = max(maxi, dp[i][j - 1][0]);
        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];
      } else {
        dp[i][j][0] = max(maxi, dp[i][j - 1][0]);
        dp[i][j][1] = max(maxi + a[i][j], dp[i][j - 1][0] + a[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
        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]);
      }
    }
  }
  int ans = dp[n][m][0];
  ans = max(ans, dp[n][m][1]);
  ans = max(ans, dp[n][m][2]);
  ans = max(ans, dp[n][m][3]);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: WA

Fixed submission:


// Problem :
// 			E - Picking Goods
//
//
// Contest : AtCoder - AtCoder Beginner Contest 175
// URL : https://atcoder.jp/contests/abc175/tasks/abc175_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define int long long
#define db double
#define mod 1000000007
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define maxn 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int dp[n + 1][m + 1][4];
  int a[n + 1][m + 1];
  memset(dp, 0, sizeof(dp));
  memset(a, 0, sizeof(a));
  rep(i, 0, k) {
    int x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int maxi = dp[i - 1][j][0];
      maxi = max(dp[i - 1][j][1], maxi);
      maxi = max(dp[i - 1][j][2], maxi);
      maxi = max(dp[i - 1][j][3], maxi);
      if (a[i][j] == 0) {
        dp[i][j][0] = max(maxi, dp[i][j - 1][0]);
        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];
      } else {
        dp[i][j][0] = max(maxi, dp[i][j - 1][0]);
        dp[i][j][1] = max(maxi + a[i][j], dp[i][j - 1][0] + a[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
        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]);
      }
    }
  }
  int ans = dp[n][m][0];
  ans = max(ans, dp[n][m][1]);
  ans = max(ans, dp[n][m][2]);
  ans = max(ans, dp[n][m][3]);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}