Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

int dp[3005][3005][5];
int val[3005][3005];

class EPickingGoods {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    CLR(dp, 0);
    CLR(val, 0);
    int R, C, K;
    cin >> R >> C >> K;
    REP(i, K) {
      int r, c, v;
      cin >> r >> c >> v;
      r--, c--;
      val[r][c] = v;
    }

    dp[0][0][1] = val[0][0];
    REP(i, R) {
      REP(j, C) {
        REP(k, 4) {
          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] + val[i + 1][j]);
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + val[i][j + 1]);
        }
      }
    }

    int res = 0;
    REP(k, 4) { res = max(res, dp[R - 1][C - 1][k]); }
    cout << res << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  EPickingGoods solver;
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class EPickingGoods {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int R, C, K;
    cin >> R >> C >> K;
    map<pair<int, int>, int> val;
    REP(i, K) {
      int r, c, v;
      cin >> r >> c >> v;
      r--, c--;
      val[{r, c}] = v;
    }

    vector<vector<int>> cur(C, vector<int>(4, -1));
    vector<vector<int>> next(C, vector<int>(4, -1));
    cur[0][0] = 0;
    REP(i, R) {
      REP(j, C) {
        REP(k, 4) { next[j][k] = -1; }
      }
      REP(j, C) {
        REP(k, 4) {
          if (cur[j][k] == -1)
            continue;
          // 拾わない
          if (i + 1 < R)
            next[j][0] = max(next[j][0], cur[j][k]);
          if (j + 1 < C)
            cur[j + 1][k] = max(cur[j + 1][k], cur[j][k]);

          // 拾う
          if (k < 3) {
            if (i + 1 < R)
              next[j][0] = max(next[j][0], cur[j][k] + val[{i, j}]);
            if (j + 1 < C)
              cur[j + 1][k + 1] =
                  max(cur[j + 1][k + 1], cur[j][k] + val[{i, j}]);
          }
        }
      }
      if (i < R - 1)
        cur = next;
    }

    int res = 0;
    REP(i, 4) {
      int tmp = cur[C - 1][i];
      if (i < 3)
        tmp += val[{R - 1, C - 1}];
      res = max(res, tmp);
    }
    cout << res << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  EPickingGoods solver;
  solver.solve(in, out);
  return 0;
}