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

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;
const int N = 1e6 + 10;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}
ll a[3010][3010];
ll dp[3010][3010][5];
void solve() {
  ll r, c, kk;
  cin >> r >> c >> kk;
  // ll a[r+10][c+10];
  memset(a, 0, sizeof(a));
  for (int i = 0; i < kk; i++) {
    ll v, u, val;
    cin >> v >> u >> val;
    a[v][u] = val;
  }
  // ll dp[r+10][c+10][5];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        ll now = dp[i][j][k];
        if (i < r)
          dp[i + 1][j][1] = max(dp[i + 1][j][1], now + a[i + 1][j]);
        if (j < c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], now);
        if (k < 3 && j < c) {
          dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], now + a[i][j + 1]);
        }
      }
    }
  }
  ll ans = max(dp[r][c][1], max(dp[r][c][2], dp[r][c][3]));
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) { return (!b ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
bool cmp(int a, int b) { return a > b; }

const ll mod = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;
const ld pi = 3.141592653589793;
const int N = 1e6 + 10;

long long modpow(long long n, long long k, long long mod) {
  if (k == 0)
    return 1;
  long long r = modpow(n * n % mod, k >> 1, mod);
  if (k & 1)
    r = r * n % mod;
  return r;
}

void solve() {
  ll r, c, kk;
  cin >> r >> c >> kk;
  ll a[r + 10][c + 10];
  memset(a, 0, sizeof(a));
  for (int i = 0; i < kk; i++) {
    ll v, u, val;
    cin >> v >> u >> val;
    a[v][u] = val;
  }
  ll dp[r + 10][c + 10][5];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 1; k <= 3; k++) {
        ll now = dp[i][j][k];
        if (i < r)
          dp[i + 1][j][1] = max(dp[i + 1][j][1], now + a[i + 1][j]);
        if (j < c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], now);
        if (k < 3 && j < c) {
          dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], now + a[i][j + 1]);
        }
      }
    }
  }
  ll ans = max(dp[r][c][1], max(dp[r][c][2], dp[r][c][3]));
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}