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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pvec(v)                                                                \\
  for (auto e : v)                                                             \\
    cout << e << " ";
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 10000000000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

ll power(ll x, ll y, ll p = MOD) {
  ll res = 1LL;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1LL)
      res = (res * x) % p;
    y = y >> 1LL;
    x = (x * x) % p;
  }
  return res;
}

ll dp[3005][3005][4];
int r, c, k;

bool valid(int x, int y) {
  if (x < 0 || y < 0 || x >= r || y >= c)
    return false;
  return true;
}

int main() {
  // IO

  cin >> r >> c >> k;

  ll arr[r][c];

  memset(arr, 0LL, sizeof(arr));

  int x, y;
  ll v;
  bool p = false;
  for (int i = 0; i < k; ++i) {
    cin >> x >> y >> v;
    --x;
    --y;
    if (x == 0 && y == 0) {
      p = true;
    }
    arr[x][y] = v;
  }

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      for (int k = 0; k < 4; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }

  dp[0][0][0] = 0;

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (arr[i][j]) {
        for (int k = 2; k >= 0; --k) {
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + arr[i][j]);
        }
      }

      for (int k = 0; k < 4; ++k) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }

  ll mx = 0;
  for (int i = 0; i < 4; ++i) {
    mx = max(mx, dp[r - 1][c - 1][i]);
  }

  cout << mx << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pvec(v)                                                                \\
  for (auto e : v)                                                             \\
    cout << e << " ";
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define INF 10000000000000005
#define MOD 1000000007
#define all(v) v.begin(), v.end()

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

ll power(ll x, ll y, ll p = MOD) {
  ll res = 1LL;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1LL)
      res = (res * x) % p;
    y = y >> 1LL;
    x = (x * x) % p;
  }
  return res;
}

ll dp[100][100][4];
int r, c, k;

bool valid(int x, int y) {
  if (x < 0 || y < 0 || x >= r || y >= c)
    return false;
  return true;
}

int main() {
  // IO

  cin >> r >> c >> k;

  ll arr[r][c];

  memset(arr, 0LL, sizeof(arr));

  int x, y;
  ll v;
  bool p = false;
  for (int i = 0; i < k; ++i) {
    cin >> x >> y >> v;
    --x;
    --y;
    if (x == 0 && y == 0) {
      p = true;
    }
    arr[x][y] = v;
  }

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      for (int k = 0; k < 4; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }

  dp[0][0][0] = 0;

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (arr[i][j]) {
        for (int k = 2; k >= 0; --k) {
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + arr[i][j]);
        }
      }

      for (int k = 0; k < 4; ++k) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }

  ll mx = 0;
  for (int i = 0; i < 4; ++i) {
    mx = max(mx, dp[r - 1][c - 1][i]);
  }

  cout << mx << endl;

  return 0;
}