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>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
int DP[3005][3005][4];
int C[3005][3005];
void solve() {
  int r, c, k1, i, j, k;
  cin >> r >> c >> k1;
  memset(C, 0, sizeof(C));
  memset(DP, 0, sizeof(DP));
  for (i = 0; i < k1; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    C[x][y] = v;
  }
  DP[1][1][1] = C[1][1];
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      for (k = 1; k <= 3; k++) {
        DP[i][j][1] = max(DP[i][j][1], C[i][j] + DP[i - 1][j][k]);
        DP[i][j][k] = max(DP[i][j][k], DP[i][j - 1][k]);
        DP[i][j][k] = max(DP[i][j][k], C[i][j] + DP[i][j - 1][k - 1]);
      }
    }
  }
  // for(i=1;i<=r;i++)
  // {
  // for(j=1;j<=c;j++)
  // {
  // for(k=1;k<=3;k++)
  // {
  // trace(i,j,k,DP[i][j][k]);
  // }
  // }
  // }
  cout << max(DP[r][c][1], max(DP[r][c][2], DP[r][c][3])) << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: 0

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>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#define int long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define mod 998244353
#define bug cout << "HY" << endl
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define numize(A) A.numize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int, int> P;
int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return (power((a * a) % mod, b / 2)) % mod;
  return (a * (power((a * a) % mod, b / 2)) % mod) % mod;
}
int modInv(int n) { return power(n, MOD - 2); }
int nCr(int n, int r, int p = MOD) {
  if (r == 0)
    return 1;
  int Fact[n + 1];
  Fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    Fact[i] = (Fact[i - 1] * i) % p;
  }
  return (((Fact[n] * modInv(Fact[r])) % MOD) * (modInv(Fact[n - r]) % MOD)) %
         MOD;
}
double PI = 3.14159265359;
void solve() {
  int r, c, k1, i, j, k;
  cin >> r >> c >> k1;
  int C[r + 5][c + 5];
  memset(C, 0, sizeof(C));
  int DP[r + 5][c + 5][4];
  memset(DP, 0, sizeof(DP));
  for (i = 0; i < k1; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    C[x][y] = v;
  }
  DP[1][1][1] = C[1][1];
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      for (k = 1; k <= 3; k++) {
        DP[i][j][1] = max(DP[i][j][1], C[i][j] + DP[i - 1][j][k]);
        DP[i][j][k] = max(DP[i][j][k], DP[i][j - 1][k]);
        DP[i][j][k] = max(DP[i][j][k], C[i][j] + DP[i][j - 1][k - 1]);
      }
    }
  }
  // for(i=1;i<=r;i++)
  // {
  // for(j=1;j<=c;j++)
  // {
  // for(k=1;k<=3;k++)
  // {
  // trace(i,j,k,DP[i][j][k]);
  // }
  // }
  // }
  cout << max(DP[r][c][1], max(DP[r][c][2], DP[r][c][3])) << endl;
}
int32_t main() {
  boost;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}