prithivMLmods/Coder-Stat huggingface dataset:
Problem p02585 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
  ll n, k;
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    int x = i;
    now++;
    while (!vis[x]) {
      vis[x] = true;
      V[now].push_back(c[x]);
      x = p[x];
    }
  }
  ll ans = -2e18;
  for (int i = 1; i <= now; i++) {
    ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
    int num = 0;
    for (auto it : V[i])
      q[++num] = it;
    for (auto it : V[i])
      q[++num] = it;

    for (int j = 1; j <= k1; j++) {
      ll po = 0, mx = -2e18, no = 0;
      for (int l = 1; l <= num; l++) {
        no += q[l];
        if (l - po > j)
          no -= q[++po];
        mx = max(mx, no);
      }
      u[j] = mx;
      ans = max(ans, mx);
    }

    for (auto it : V[i])
      sum += it;
    ll t = (k1 - 1) / siz;
    k1 -= t * siz;
    if (sum > 0)
      sum1 += t * sum;
    for (int j = 1; j <= k1; j++)
      ans = max(ans, sum1 + u[j]);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
ll c[N], p[N], q[N << 1], u[N << 1];
vector<ll> V[N];
bool vis[N];
int main() {
  ll n, k;
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  int now = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    int x = i;
    now++;
    while (!vis[x]) {
      vis[x] = true;
      V[now].push_back(c[x]);
      x = p[x];
    }
  }
  ll ans = -2e18;
  for (int i = 1; i <= now; i++) {
    ll sum = 0, sum1 = 0, k1 = k, siz = V[i].size();
    int num = 0;
    for (auto it : V[i])
      q[++num] = it;
    for (auto it : V[i])
      q[++num] = it;

    for (int j = 1; j <= min(siz, k1); j++) {
      ll po = 0, mx = -2e18, no = 0;
      for (int l = 1; l <= num; l++) {
        no += q[l];
        if (l - po > j)
          no -= q[++po];
        mx = max(mx, no);
      }
      u[j] = mx;
      ans = max(ans, mx);
    }

    for (auto it : V[i])
      sum += it;
    ll t = (k1 - 1) / siz;
    k1 -= t * siz;
    if (sum > 0)
      sum1 += t * sum;
    for (int j = 1; j <= k1; j++)
      ans = max(ans, sum1 + u[j]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int n, k;
int p[5000];
int c[5000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];

  long long ans = *max_element(c, c + n);

  rep(i, n) {
    unordered_map<int, pair<int, long long>> vis;
    vis.reserve(n);
    int cur = i;
    long long s = 0;

    rep(j, k) {
      s += c[cur];
      cur = p[cur];

      ans = max(ans, s);

      auto it = vis.find(cur);
      if (it != vis.end()) {
        int cyc = j - it->second.first;
        long long cycs = s - it->second.second;

        ans = max(ans, s + (k - j - 1) / cyc * cycs);
      }
      vis[cur] = make_pair(j, s);
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int n, k;
int p[5000];
int c[5000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;
  rep(i, n) cin >> p[i], p[i]--;
  rep(i, n) cin >> c[i];

  long long ans = *max_element(c, c + n);

  rep(i, n) {
    unordered_map<int, pair<int, long long>> vis;
    vis.reserve(n);
    int cur = i;
    long long s = 0;

    rep(j, min(2 * n, k)) {
      s += c[cur];
      cur = p[cur];

      ans = max(ans, s);

      auto it = vis.find(cur);
      if (it != vis.end()) {
        int cyc = j - it->second.first;
        long long cycs = s - it->second.second;

        ans = max(ans, s + (k - j - 1) / cyc * cycs);
      }
      vis[cur] = make_pair(j, s);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main() {
  ll n, k, ans = -10000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) cin >> c[i];
  if (k) {
    vector<ll> dp(n);
    for (ll i = 0; i < n; i++) {
      ll now = i;
      rep(j, k) {
        now = p[now];
        dp[i] += c[now];
        ans = max(ans, dp[i]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main() {
  ll n, k, ans = -10000000000;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) cin >> c[i];
  vector<ll> dp(n);
  for (ll i = 0; i < n; i++) {
    ll now = i, start = i, count = 0;
    rep(j, k) {
      now = p[now];
      count++;
      dp[i] += c[now];
      ans = max(ans, dp[i]);
      if (now == start) {
        j = k;
        ll loop = (k - 1) / count;
        dp[i] = dp[i] * loop;
        ll kp = k - (count * loop);
        rep(r, kp) {
          now = p[now];
          dp[i] += c[now];
          ans = max(ans, dp[i]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
//
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

int a[N], c[N];
bool vst[N];
vector<int> vec[N];

int main() {
  ios::sync_with_stdio(0);

  int n, lim;
  cin >> n >> lim;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    if (vst[i])
      continue;
    int o = i;
    while (!vst[o]) {
      vst[o] = 1;
      vec[i].push_back(o);
      o = a[o];
    }
  }

  ll ans = -INF_LL;
  for (int i = 1; i <= n; i++) {
    if (vec[i].empty())
      continue;
    int siz = vec[i].size();
    if (siz != 0 && siz <= n)
      while (1)
        ;

    static ll s[N];
    s[0] = 0;
    for (int j = 1; j <= siz; j++)
      s[j] = s[j + siz] = c[vec[i][j - 1]];
    for (int j = 1; j <= 2 * siz; j++)
      s[j] += s[j - 1];

    ll sum = s[siz];
    for (int j = 1; j <= min(siz, lim); j++) {
      ll maxs = -INF_LL;
      for (int k = 1; k <= siz; k++)
        maxs = max(maxs, s[k + j - 1] - s[k - 1]);
      if (sum > 0)
        maxs += sum * ((lim - j) / siz);
      ans = max(ans, maxs);
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

int a[N], c[N];
bool vst[N];
vector<int> vec[N];

int main() {
  ios::sync_with_stdio(0);

  int n, lim;
  cin >> n >> lim;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= n; i++) {
    if (vst[i])
      continue;
    int o = i;
    while (!vst[o]) {
      vst[o] = 1;
      vec[i].push_back(o);
      o = a[o];
    }
  }

  ll ans = -INF_LL;
  for (int i = 1; i <= n; i++) {
    if (vec[i].empty())
      continue;
    int siz = vec[i].size();

    static ll s[N];
    s[0] = 0;
    for (int j = 1; j <= siz; j++)
      s[j] = s[j + siz] = c[vec[i][j - 1]];
    for (int j = 1; j <= 2 * siz; j++)
      s[j] += s[j - 1];

    ll sum = s[siz];
    for (int j = 1; j <= min(siz, lim); j++) {
      ll maxs = -INF_LL;
      for (int k = 1; k <= siz; k++)
        maxs = max(maxs, s[k + j - 1] - s[k - 1]);
      if (sum > 0)
        maxs += sum * ((lim - j) / siz);
      ans = max(ans, maxs);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}

int main() {

  int N, K;
  cin >> N >> K;

  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> points[i];
  }

  for (int i = 1; i <= N; i++) {
    vector<int> nodes;
    int initIdx = i;
    if (memo[i].first != 0)
      continue;

    int currIdx = i;
    ll point_sum = 0;
    int cnt = 0;
    do {
      cnt++;
      currIdx = arr[currIdx];
      point_sum += points[currIdx];
      nodes.push_back(currIdx);
    } while (currIdx != initIdx);

    for (int i = 0; i < nodes.size(); i++) {
      memo[nodes[i]] = {cnt, point_sum};
    }
  }

  ll maxim = -10e20;
  for (int i = 1; i <= N; i++) {
    int tempK = K;
    ll sum = 0;

    int currIdx = i;
    for (int j = 0; j < tempK; j++) {
      currIdx = arr[currIdx];
      sum += points[currIdx];
      maxim = max(maxim, sum);
    }
  }

  cout << maxim << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int arr[5001];
int points[5001];
pair<int, ll> memo[5001]; // {횟수,총점수}

int main() {

  int N, K;
  cin >> N >> K;

  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i <= N; i++) {
    cin >> points[i];
  }

  for (int i = 1; i <= N; i++) {
    vector<int> nodes;
    int initIdx = i;
    if (memo[i].first != 0)
      continue;

    int currIdx = i;
    ll point_sum = 0;
    int cnt = 0;
    do {
      cnt++;
      currIdx = arr[currIdx];
      point_sum += points[currIdx];
      nodes.push_back(currIdx);
    } while (currIdx != initIdx);

    for (int i = 0; i < nodes.size(); i++) {
      memo[nodes[i]] = {cnt, point_sum};
    }
  }

  ll maxim = -10e20;
  for (int i = 1; i <= N; i++) {
    int tempK = K;
    ll sum = 0;
    double multiple = (double)tempK / memo[i].first;
    if (multiple > 2) {
      if (memo[i].second > 0) {
        sum += ((int)multiple - 1) * memo[i].second;
      }
      tempK -= ((int)multiple - 1) * memo[i].first;
    }

    int currIdx = i;
    for (int j = 0; j < tempK; j++) {
      currIdx = arr[currIdx];
      sum += points[currIdx];
      maxim = max(maxim, sum);
    }
  }

  cout << maxim << endl;
}
Problem p02585 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  vector<int> visited(N);
  vector<vector<int>> VV;
  for (int i = 0; i < N; i++)
    if (!visited[i]) {
      vector<int> v = {i};
      int pt = P[i];
      while (pt != i) {
        v.push_back(pt);
        pt = P[pt];
      }
      VV.push_back(v);
    }

  int64_t ans = -1e18;
  for (auto &v : VV) {
    int sz = v.size();
    for (int r = 0; r < sz; r++) {
      vector<int64_t> S(sz + 1);
      for (int i = 0; i < sz; i++)
        S[i + 1] = S[i] + C[v[i]];
      for (int t = 0; t < sz; t++) {
        int64_t lmn = (t ? 0 : 1);
        int64_t lmx = (K + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        int64_t l = (S[sz] > 0 ? lmx : lmn);
        int64_t res = l * S[sz] + S[t];
        ans = max(ans, res);
      }
      rotate(v.begin(), v.begin() + 1, v.end());
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> P(N);
  for (int i = 0; i < N; i++)
    cin >> P[i], P[i]--;
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C[i];

  vector<int> visited(N);
  vector<vector<int>> VV;
  for (int i = 0; i < N; i++)
    if (!visited[i]) {
      vector<int> v = {i};
      int pt = P[i];
      while (pt != i) {
        v.push_back(pt);
        pt = P[pt];
      }
      for (int a : v)
        visited[a] = 1;
      VV.push_back(v);
    }

  int64_t ans = -1e18;
  for (auto &v : VV) {
    int sz = v.size();
    for (int r = 0; r < sz; r++) {
      vector<int64_t> S(sz + 1);
      for (int i = 0; i < sz; i++)
        S[i + 1] = S[i] + C[v[i]];
      for (int t = 0; t < sz; t++) {
        int64_t lmn = (t ? 0 : 1);
        int64_t lmx = (K + sz - t) / sz - 1;
        if (lmn > lmx)
          continue;
        int64_t l = (S[sz] > 0 ? lmx : lmn);
        int64_t res = l * S[sz] + S[t];
        ans = max(ans, res);
      }
      rotate(v.begin(), v.begin() + 1, v.end());
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02585 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> p(n);
  vector<ll> c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> c[i];

  vector<vector<int>> v(n);

  for (int i = 0; i < n; i++) {
    int j = i;
    while (p[j] != i) {
      v[i].push_back(p[j]);
      j = p[j];
    }
    v[i].push_back(i);
  }

  /*
  for(int i = 0; i < n; i++) {
      for(auto x : v[i]) {
          cout << x << " ";
      }
      cout << endl;
  }
   */

  ll ans = LONG_LONG_MIN;
  for (int i = 0; i < n; i++) {
    int s = v[i].size();

    ll tmp = 0;
    ll wa = 0;

    int q = k / s;
    int r = k % s;

    vector<ll> mx(s, LONG_LONG_MIN);
    mx[0] = c[v[i][0]];
    wa = c[v[i][0]];

    for (int j = 1; j < s; j++) {
      wa += c[v[i][j]];
      mx[j] = max(mx[j - 1], wa);
    }

    // for(auto x : v[i]) wa += c[x];

    if (wa >= 0 && q >= 1)
      tmp = wa * (ll)(q - 1);
    tmp += max(mx[s - 1], wa + mx[q]);

    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> p(n);
  vector<ll> c(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; i++)
    cin >> c[i];

  vector<vector<int>> v(n);

  for (int i = 0; i < n; i++) {
    int j = i;
    while (p[j] != i) {
      v[i].push_back(p[j]);
      j = p[j];
    }
    v[i].push_back(i);
  }

  /*
  for(int i = 0; i < n; i++) {
      for(auto x : v[i]) {
          cout << x << " ";
      }
      cout << endl;
  }
   */

  ll ans = LONG_LONG_MIN;
  for (int i = 0; i < n; i++) {
    int s = v[i].size();

    ll tmp = 0;
    ll wa = 0;

    int q = k / s;
    int r = k % s;

    vector<ll> mx(s, LONG_LONG_MIN);
    mx[0] = c[v[i][0]];
    wa = c[v[i][0]];

    for (int j = 1; j < s; j++) {
      wa += c[v[i][j]];
      mx[j] = max(mx[j - 1], wa);
    }

    // for(auto x : v[i]) wa += c[x];

    if (wa >= 0 && q >= 1)
      tmp = wa * (ll)(q - 1);

    if (q == 0)
      tmp += mx[r - 1];
    if (q != 0 && r == 0)
      tmp += mx[s - 1];
    if (q != 0 && r >= 1)
      tmp += max(mx[s - 1], wa + mx[r - 1]);

    ans = max(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll v[3000][3000], dp[3000][3000][4];

int main(void) {
  int r, c, t;
  cin >> r >> c >> t;
  fill(v[0], v[r + 1], 0);
  rep(i, t) {
    int a, b;
    cin >> a >> b;
    cin >> v[a - 1][b - 1];
  }
  fill(dp[0][0], dp[r][0], 0);
  if (v[0][0] > 0)
    dp[0][0][2] += v[0][0];
  rep(i, r) {
    rep(j, c) {
      ll val = v[i][j];
      rep(k, 4) {
        if (i > 0) {
          dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
          dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll v[3000][3000], dp[3000][3000][4];

int main(void) {
  int r, c, t;
  cin >> r >> c >> t;
  fill(v[0], v[r], 0);
  rep(i, t) {
    int a, b;
    cin >> a >> b;
    cin >> v[a - 1][b - 1];
  }
  fill(dp[0][0], dp[r][0], 0);
  if (v[0][0] > 0)
    dp[0][0][2] += v[0][0];
  rep(i, r) {
    rep(j, c) {
      ll val = v[i][j];
      rep(k, 4) {
        if (i > 0) {
          dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j][k]);
          dp[i][j][2] = max(dp[i][j][2], dp[i - 1][j][k] + val);
        }
        if (j > 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k > 0) {
            dp[i][j][k - 1] = max(dp[i][j][k - 1], dp[i][j - 1][k] + val);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(k, 4) ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int M = 1e3;
int n, m, mat[M][M];
long long memo[M][M][4];

long long solve(int i, int j, int cnt) {
  if (i == n || j == m)
    return 0;
  long long &r = memo[i][j][cnt];
  if (r == -1) {
    r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
    if (cnt < 3 && mat[i][j])
      r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  memset(memo, -1, sizeof memo);
  int k;
  cin >> n >> m >> k;
  while (k--) {
    int i, j, x;
    cin >> i >> j >> x;
    mat[i - 1][j - 1] = x;
  }
  cout << solve(0, 0, 0) << '\n';
}

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

const int M = 3e3;
int n, m, mat[M][M];
long long memo[M][M][4];

long long solve(int i, int j, int cnt) {
  if (i == n || j == m)
    return 0;
  long long &r = memo[i][j][cnt];
  if (r == -1) {
    r = max(solve(i + 1, j, 0), solve(i, j + 1, cnt));
    if (cnt < 3 && mat[i][j])
      r = max(r, mat[i][j] + max(solve(i + 1, j, 0), solve(i, j + 1, cnt + 1)));
  }
  return r;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  memset(memo, -1, sizeof memo);
  int k;
  cin >> n >> m >> k;
  while (k--) {
    int i, j, x;
    cin >> i >> j >> x;
    mat[i - 1][j - 1] = x;
  }
  cout << solve(0, 0, 0) << '\n';
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<ll> r(K), c(K), v(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
  vector<vector<ll>> point(R + 2, vector<ll>(C + 2));
  rep(i, R + 2) {
    rep(j, C + 2) { point[i][j] = 0; }
  }
  rep(i, K) { point[r[i]][c[i]] = v[i]; }
  ll dp[R + 2][C + 2][5];
  for (ll i = 0; i <= R + 1; i++) {
    for (ll j = 0; j <= C + 1; j++) {
      for (ll k = 0; k < 5; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][1][1] = point[1][1];
  for (ll i = 1; i <= R; i++) {
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i][j + 1][k + 1] = max(dp[i][j][k] + point[i][j + 1],
                                  max(dp[i][j + 1][k + 1], dp[i][j][k + 1]));
        dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
      }
    }
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + point[i + 1][j]);
      }
    }
  }
  ll ans = max(dp[R][C][0], max(dp[R][C][1], max(dp[R][C][2], dp[R][C][3])));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<ll> r(K), c(K), v(K);
  rep(i, K) cin >> r[i] >> c[i] >> v[i];
  vector<vector<ll>> point(R + 2, vector<ll>(C + 2));
  rep(i, R + 2) {
    rep(j, C + 2) { point[i][j] = 0; }
  }
  rep(i, K) { point[r[i]][c[i]] = v[i]; }
  vector<vector<vector<ll>>> dp(R + 2,
                                vector<vector<ll>>(C + 2, vector<ll>(5)));
  for (ll i = 0; i <= R + 1; i++) {
    for (ll j = 0; j <= C + 1; j++) {
      for (ll k = 0; k < 5; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[1][1][1] = point[1][1];
  for (ll i = 1; i <= R; i++) {
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i][j + 1][k + 1] = max(dp[i][j][k] + point[i][j + 1],
                                  max(dp[i][j + 1][k + 1], dp[i][j][k + 1]));
        dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
      }
    }
    for (ll j = 1; j <= C; j++) {
      for (ll k = 0; k <= 3; k++) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + point[i + 1][j]);
      }
    }
  }
  ll ans = max(dp[R][C][0], max(dp[R][C][1], max(dp[R][C][2], dp[R][C][3])));
  cout << ans << endl;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> arr(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  rep(i, k) {
    cin >> t1 >> t2 >> t3;
    t1--, t2--;
    arr[t1][t2] = t3;
  }
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
        chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
        if (p < 3) {
          chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
        }
        chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
      }
    }
  }
  ll ans = 0;
  rep(p, 4) { chmax(ans, dp[r][c][p]); }
  rep(i, 4) { cout << dp[1][4][i] << endl; }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
// #define ll long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 998244353;
const int INF = 1e9;
const ll INFLONG = 1e18;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> arr(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(4, 0)));
  rep(i, k) {
    cin >> t1 >> t2 >> t3;
    t1--, t2--;
    arr[t1][t2] = t3;
  }
  rep(i, r) {
    rep(j, c) {
      rep(p, 4) {
        chmax(dp[i + 1][j + 1][0], dp[i][j + 1][p]);
        chmax(dp[i + 1][j + 1][p], dp[i + 1][j][p]);
        if (p < 3) {
          chmax(dp[i + 1][j + 1][p + 1], dp[i + 1][j][p] + arr[i][j]);
        }
        chmax(dp[i + 1][j + 1][1], dp[i][j + 1][p] + arr[i][j]);
      }
    }
  }
  ll ans = 0;
  rep(p, 4) { chmax(ans, dp[r][c][p]); }
  cout << ans << endl;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];

bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }

long long dp(int r, int c, int rem) {
  if (memo[r][c][rem] != -1)
    return memo[r][c][rem];

  memo[r][c][rem] = 0;
  if (r == R && c == C) {
    if (rem > 0 && grid[r][c] != 0)
      memo[r][c][rem] = grid[r][c];

    else
      memo[r][c][rem] = 0;

    return memo[r][c][rem];
  }

  else {
    if (exists(r + 1, c)) {
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);

      else
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
    }

    if (exists(r, c + 1)) {
      memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] =
            max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
    }

    return memo[r][c][rem];
  }
}

int main() {
  scanf("%d%d%d", &R, &C, &K);

  for (int i = 1; i <= K; i++) {
    int ri, ci, vi;
    scanf("%d%d%d", &ri, &ci, &vi);
    grid[ri][ci] = vi;
  }

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++)
        memo[i][j][k] = -1;
    }
  }

  printf("%lld", dp(1, 1, 3));
}

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

int R, C, K;
long long memo[3005][3005][10];
int grid[3005][3005];

bool exists(int r, int c) { return (1 <= r && r <= R) && (1 <= c && c <= C); }

long long dp(int r, int c, int rem) {
  if (memo[r][c][rem] != -1)
    return memo[r][c][rem];

  memo[r][c][rem] = 0;
  if (r == R && c == C) {
    if (rem > 0 && grid[r][c] != 0)
      memo[r][c][rem] = grid[r][c];

    else
      memo[r][c][rem] = 0;

    return memo[r][c][rem];
  }

  else {
    if (exists(r + 1, c)) {
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3) + grid[r][c]);

      else
        memo[r][c][rem] = max(memo[r][c][rem], dp(r + 1, c, 3));
    }

    if (exists(r, c + 1)) {
      memo[r][c][rem] = max(memo[r][c][rem], dp(r, c + 1, rem));
      if (rem > 0 && grid[r][c] != 0)
        memo[r][c][rem] =
            max(memo[r][c][rem], dp(r, c + 1, rem - 1) + grid[r][c]);
    }

    return memo[r][c][rem];
  }
}

int main() {
  scanf("%d%d%d", &R, &C, &K);

  for (int i = 1; i <= K; i++) {
    int ri, ci, vi;
    scanf("%d%d%d", &ri, &ci, &vi);
    grid[ri][ci] = vi;
  }

  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++)
        memo[i][j][k] = -1;
    }
  }

  printf("%lld", dp(1, 1, 3));
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

typedef long long int64;

const int MAXN = 3000;

int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];

#define chkmax(x, y) x = std::max(x, y)

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("e.in", "r", stdin);
  //     freopen("e.out", "w", stdout);
  // #endif

  int i, j, k;
  int x, y;

  scanf("%d %d %d", &n, &m, &q);
  for (i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    scanf("%d", &a[x][y]);
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      for (k = 1; k < 4; k++) {
        chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
        chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
      }
      for (k = 1; k < 4; k++)
        chkmax(f[i][j][k], f[i][j][k - 1]);
      for (k = 0; k < 4; k++)
        chkmax(f[i][j][k], f[i][j - 1][k]);
    }
    for (j = 1; j <= m; j++)
      for (k = 0; k < 4; k++)
        f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
  }
  printf("%lld\n", f[n][m][3]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

typedef long long int64;

const int MAXN = 3000 + 10;

int n, m, q;
int a[MAXN][MAXN];
int64 f[MAXN][MAXN][4];

#define chkmax(x, y) x = std::max(x, y)

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("e.in", "r", stdin);
  //     freopen("e.out", "w", stdout);
  // #endif

  int i, j, k;
  int x, y;

  scanf("%d %d %d", &n, &m, &q);
  for (i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    scanf("%d", &a[x][y]);
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      for (k = 1; k < 4; k++) {
        chkmax(f[i][j][k], f[i - 1][j][3] + a[i][j]);
        chkmax(f[i][j][k], f[i][j - 1][k - 1] + a[i][j]);
      }
      for (k = 1; k < 4; k++)
        chkmax(f[i][j][k], f[i][j][k - 1]);
      for (k = 0; k < 4; k++)
        chkmax(f[i][j][k], f[i][j - 1][k]);
    }
    for (j = 1; j <= m; j++)
      for (k = 0; k < 4; k++)
        f[i][j][k] = std::max(f[i][j][k], f[i][j - 1][k]);
  }
  printf("%lld\n", f[n][m][3]);
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll vec[3100][3100][4] = {};
  ll p[3100][3100] = {};
  for (int i = 0; i < k; i++) {
    int a, b;
    ll point;
    cin >> a >> b >> point;
    p[a][b] = point;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
      vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
                          vec[i - 1][j][2], vec[i - 1][j][3]});
      vec[i][j][1] =
          max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
      vec[i][j][1] =
          max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
                                  vec[i - 1][j][2], vec[i - 1][j][3]}) +
                                 p[i][j]});
      vec[i][j][2] =
          max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
      // vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
      vec[i][j][3] =
          max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
      // vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
    }
  }
  cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
       << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<vector<ll>>> vec(3100, vector<vector<ll>>(3100, vector<ll>(4)));
  vector<vector<ll>> p(3100, vector<ll>(3100));
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      vec[i][j][0] = 0;
      vec[i][j][1] = 0;
      vec[i][j][2] = 0;
      vec[i][j][3] = 0;
      p[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    ll point;
    cin >> a >> b >> point;
    p[a][b] = point;
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      vec[i][j][0] = max(vec[i][j][0], vec[i][j - 1][0]);
      vec[i][j][0] = max({vec[i][j][0], vec[i - 1][j][0], vec[i - 1][j][1],
                          vec[i - 1][j][2], vec[i - 1][j][3]});
      vec[i][j][1] =
          max({vec[i][j][1], vec[i][j - 1][0] + p[i][j], vec[i][j - 1][1]});
      vec[i][j][1] =
          max({vec[i][j][1], max({vec[i - 1][j][0], vec[i - 1][j][1],
                                  vec[i - 1][j][2], vec[i - 1][j][3]}) +
                                 p[i][j]});
      vec[i][j][2] =
          max({vec[i][j][2], vec[i][j - 1][1] + p[i][j], vec[i][j - 1][2]});
      // vec[i][j][2]=max({vec[i][j][2],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
      vec[i][j][3] =
          max({vec[i][j][3], vec[i][j - 1][2] + p[i][j], vec[i][j - 1][3]});
      // vec[i][j][3]=max({vec[i][j][1],max({vec[i][j-1][0],vec[i][j-1][1],vec[i][j-1][2],vec[i][j-1][3]})+p[i][j]});
    }
  }
  cout << max(vec[r][c][0], max(vec[r][c][1], max(vec[r][c][2], vec[r][c][3])))
       << endl;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 2e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

ll dp[N][N][4], n, m, k, a[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    ll 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++) {
      for (int x = 0; x <= 3; x++) {

        ll res = 0;
        for (int y = 0; y <= 3; y++) {
          res = max(res, dp[i - 1][j][y]);
        }
        res = max(res, dp[i][j - 1][x]);
        dp[i][j][x] = res;

        if (!x)
          continue;
        res = 0;
        for (int y = 0; y <= 3; y++)
          res = max(res, dp[i - 1][j][y]);
        res = max(res, dp[i][j - 1][x - 1]);
        dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
      }
    }
  }
  cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 3e3 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

ll dp[N][N][4], n, m, k, a[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    ll 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++) {
      for (int x = 0; x <= 3; x++) {

        ll res = 0;
        for (int y = 0; y <= 3; y++) {
          res = max(res, dp[i - 1][j][y]);
        }
        res = max(res, dp[i][j - 1][x]);
        dp[i][j][x] = res;

        if (!x)
          continue;
        res = 0;
        for (int y = 0; y <= 3; y++)
          res = max(res, dp[i - 1][j][y]);
        res = max(res, dp[i][j - 1][x - 1]);
        dp[i][j][x] = max(res + a[i][j], dp[i][j][x]);
      }
    }
  }
  cout << max({dp[n][m][1], dp[n][m][3], dp[n][m][2], dp[n][m][0]});
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3000][3000][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
  if (i > R || i < 1 || j > C || j < 1)
    return 0;
  if (dp[i][j][picked] != -1)
    return dp[i][j][picked];

  dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
  dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);

  return dp[i][j][picked];
}
void solve() {
  ll K;
  cin >> R >> C >> K;
  vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  cout << rec(1, 1, 0, grid) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 7;
ll R, C;
ll dp[3001][3001][4];
// dp[i][j][picked] : denotes the max value picked from (i,j) to (R,C) with the
// constraints given
ll rec(int i, int j, int picked, vector<vector<ll>> &grid) {
  if (i > R || i < 1 || j > C || j < 1)
    return 0;
  if (dp[i][j][picked] != -1)
    return dp[i][j][picked];

  dp[i][j][picked] = max(dp[i][j][picked], rec(i + 1, j, 0, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], grid[i][j] + rec(i + 1, j, 0, grid));
  dp[i][j][picked] = max(dp[i][j][picked], rec(i, j + 1, picked, grid));
  if (picked < 3)
    dp[i][j][picked] =
        max(dp[i][j][picked], rec(i, j + 1, picked + 1, grid) + grid[i][j]);

  return dp[i][j][picked];
}
void solve() {
  ll K;
  cin >> R >> C >> K;
  vector<vector<ll>> grid(R + 1, vector<ll>(C + 1, 0));
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    grid[r][c] = v;
  }
  cout << rec(1, 1, 0, grid) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int r, c, k;
  cin >> r >> c >> k;

  vector<vector<ll>> item(r, vector<ll>(c));
  ll dp[3100][3100][4];
  for (int i = 0; i < r + 100; i++) {
    for (int j = 0; j < c + 100; j++) {
      for (int ii = 0; ii < 4; ii++) {
        dp[i][j][ii] = 0;
      }
    }
  }

  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    item[x][y] = z;
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        if (item[i][j] == 0) {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
        } else {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
          if (ii < 3) {
            dp[i][j + 1][ii + 1] =
                max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
          }
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        ans = max(ans, dp[i][j][ii]);
      }
    }
  }

  cout << ans << '\n';
}

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int r, c, k;
  cin >> r >> c >> k;

  vector<vector<ll>> item(r, vector<ll>(c));
  vector<vector<vector<ll>>> dp(r + 100,
                                vector<vector<ll>>(c + 100, vector<ll>(4, 0)));
  /* ll dp[3100][3100][4];
   for (int i = 0; i < r + 100; i++) {
     for (int j = 0; j < c + 100; j++) {
       for (int ii = 0; ii < 4; ii++) {
         dp[i][j][ii] = 0;
       }
     }
   }*/

  for (int i = 0; i < k; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    item[x][y] = z;
  }

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        if (item[i][j] == 0) {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
        } else {
          dp[i][j + 1][ii] = max(dp[i][j + 1][ii], dp[i][j][ii]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii]);
          if (ii < 3) {
            dp[i][j + 1][ii + 1] =
                max(dp[i][j + 1][ii + 1], dp[i][j][ii] + item[i][j]);
            dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][ii] + item[i][j]);
          }
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int ii = 0; ii < 4; ii++) {
        ans = max(ans, dp[i][j][ii]);
      }
    }
  }

  cout << ans << '\n';
}
Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

// ll dp[3010][3010][5];
vector<VVL> dp(5, VVL(3010, VL(3010, 0)));
ll tbl[3010][3010];

void init() {
  rep(i, 0, 3010) rep(j, 0, 3010) {
    // rep(k, 0, 5) dp[i][j][k] = 0;
    tbl[i][j] = 0;
  }
}

int main() {

  init();

  // input
  ll R, C, K;
  cin >> R >> C >> K;
  VL r(K), c(K), v(K);
  rep(i, 0, K) {
    cin >> r[i] >> c[i] >> v[i];
    tbl[r[i] - 1][c[i] - 1] = v[i];
  }

  rep(i, 0, R + 1) {
    rep(j, 0, C + 1) {
      // (i, j) -> (i+1, j)
      if (i < R) {
        // i, j で取らない
        rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // i, j で取る
        rep(k, 0, 3) dp[i + 1][j][0] =
            max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
      }
      // (i, j) -> (i, j+1)
      if (j < C) {
        // i, jで取らない
        rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // i, jで取る
        rep(k, 0, 3) dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[R][C][i], ans);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

// ll dp[3010][3010][5];
vector<VVL> dp(3010, VVL(3010, VL(5, 0)));
ll tbl[3010][3010];

void init() {
  rep(i, 0, 3010) rep(j, 0, 3010) {
    // rep(k, 0, 5) dp[i][j][k] = 0;
    tbl[i][j] = 0;
  }
}

int main() {

  init();

  // input
  ll R, C, K;
  cin >> R >> C >> K;
  VL r(K), c(K), v(K);
  rep(i, 0, K) {
    cin >> r[i] >> c[i] >> v[i];
    tbl[r[i] - 1][c[i] - 1] = v[i];
  }

  rep(i, 0, R + 1) {
    rep(j, 0, C + 1) {
      // (i, j) -> (i+1, j)
      if (i < R) {
        // i, j で取らない
        rep(k, 0, 4) dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // i, j で取る
        rep(k, 0, 3) dp[i + 1][j][0] =
            max(dp[i + 1][j][0], dp[i][j][k] + tbl[i][j]);
      }
      // (i, j) -> (i, j+1)
      if (j < C) {
        // i, jで取らない
        rep(k, 0, 4) dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        // i, jで取る
        rep(k, 0, 3) dp[i][j + 1][k + 1] =
            max(dp[i][j + 1][k + 1], dp[i][j][k] + tbl[i][j]);
      }
    }
  }

  ll ans = 0;
  rep(i, 0, 4) ans = max(dp[R][C][i], ans);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  map<int, map<int, ll>> goods;

  int r, c, v;
  for (int i = 0; i < K; ++i) {
    cin >> r >> c >> v;

    goods[r - 1][c - 1] = v;
  }

  ll dp[2][C][4];
  memset(dp, 0, sizeof(dp));

  for (int y = 0; y < R; ++y) {
    int cy = y % 2;
    int by = (y + 1) % 2;

    for (int x = 0; x < C; ++x) {
      ll mv = LLONG_MIN;

      for (int c = 0; c <= 3; ++c) {
        mv = max(mv, dp[by][x][c]);
      }

      dp[cy][x][0] = mv;

      if (goods[y][x] > 0) {
        dp[cy][x][1] = mv + goods[y][x];
      }
    }

    for (int x = 1; x < C; ++x) {
      for (int c = 0; c <= 3; ++c) {
        dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);

        if (c < 3 && goods[y][x] > 0) {
          dp[cy][x][c + 1] =
              max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
        }
      }
    }
  }

  ll ans = LLONG_MIN;

  for (int c = 0; c <= 3; ++c) {
    ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  int goods[R][C];
  memset(goods, 0, sizeof(goods));
  // map<int, map<int, ll>> goods;

  int r, c, v;
  for (int i = 0; i < K; ++i) {
    cin >> r >> c >> v;

    goods[r - 1][c - 1] = v;
  }

  ll dp[2][C][4];
  memset(dp, 0, sizeof(dp));

  for (int y = 0; y < R; ++y) {
    int cy = y % 2;
    int by = (y + 1) % 2;

    for (int x = 0; x < C; ++x) {
      ll mv = LLONG_MIN;

      for (int c = 0; c <= 3; ++c) {
        mv = max(mv, dp[by][x][c]);
      }

      dp[cy][x][0] = mv;

      if (goods[y][x] > 0) {
        dp[cy][x][1] = mv + goods[y][x];
      }
    }

    for (int x = 1; x < C; ++x) {
      for (int c = 0; c <= 3; ++c) {
        dp[cy][x][c] = max(dp[cy][x][c], dp[cy][x - 1][c]);

        if (c < 3 && goods[y][x] > 0) {
          dp[cy][x][c + 1] =
              max(dp[cy][x][c + 1], dp[cy][x - 1][c] + goods[y][x]);
        }
      }
    }
  }

  ll ans = LLONG_MIN;

  for (int c = 0; c <= 3; ++c) {
    ans = max(ans, dp[(R - 1) % 2][C - 1][c]);
  }

  cout << ans << endl;

  return 0;
}