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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")

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

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
int n, m, k;
long long dp[3005][3005][4];
int idx[3005][3005];
vector<pair<int, long long>> v[3002];
long long solve(int i, int j, int picked) {
  if (i == n + 1) {
    return 0;
  }
  if (~dp[i][j][picked])
    return dp[i][j][picked];

  if (v[i].empty())
    solve(i + 1, j, 0);
  long long ans = 0;
  auto it =
      lower_bound(v[i].begin(), v[i].end(), make_pair(j, 0ll)) - v[i].begin();
  if (it != v[i].size()) {
    auto x = v[i][it];
    if (x.first >= j and picked < 3) {
      ans = max(ans, x.second + solve(i, x.first + 1, picked + 1));
      ans = max(ans, x.second + solve(i + 1, x.first, 0));
    }
    ans = max(ans, solve(i, x.first + 1, picked));
  }
  ans = max(ans, solve(i + 1, j, 0));
  return dp[i][j][picked] = ans;
}
int32_t main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y, price;
    cin >> x >> y >> price;
    v[x].push_back(make_pair(y, price));
  }
  for (int i = 0; i <= 3000; i++) {
    sort(v[i].begin(), v[i].end());
    //     for(int j = 0; j <= 3000; j++) {
    //         auto it = lower_bound(v[i].begin(), v[i].end(), make_pair(j,
    //         0ll)) - v[i].begin();
    //         // if(it == v[i].size())
    //             // break;
    //         idx[i][j] = it;
    //     }
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0, 0);
}


Error: TLE

Fixed submission:

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

#ifdef LOCAL
#include "debug.h"
#define input                                                                  \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#else
#define debug(...) 4
#define input 4
#endif

using ll = long long;
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
int n, m, k;
long long dp[3005][3005][4];
vector<pair<int, long long>> v[3002];
long long solve(int i, int j, int picked) {
  if (i == n + 1) {
    return 0;
  }
  if (~dp[i][j][picked])
    return dp[i][j][picked];

  if (v[i].empty() || picked == 3) {
    return dp[i][j][picked] = solve(i + 1, j, 0);
  }
  long long ans = 0;
  auto it =
      lower_bound(v[i].begin(), v[i].end(), make_pair(j, 0ll)) - v[i].begin();
  for (int iter = it; iter < v[i].size(); iter++) {
    auto x = v[i][iter];
    if (x.first >= j and picked < 3) {
      ans = max(ans, x.second + solve(i, x.first + 1, picked + 1));
      ans = max(ans, x.second + solve(i + 1, x.first, 0));
    }
    ans = max(ans, solve(i, x.first + 1, picked));
  }
  ans = max(ans, solve(i + 1, j, 0));
  return dp[i][j][picked] = ans;
}
int32_t main() {
  input;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int x, y, price;
    cin >> x >> y >> price;
    v[x].push_back(make_pair(y, price));
  }
  for (int i = 0; i <= 3000; i++)
    sort(v[i].begin(), v[i].end());
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0, 0);
}