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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

int64_t dp[3001][3001][4] = {};
int64_t v[3001][3001] = {};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int r, c, k;
  cin >> r >> c >> k;
  FOR_LT(i, 0, k) {
    int cr, cc, cv;
    cin >> cr >> cc >> cv;
    v[cr][cc] = cv;
  }

  FOR_LE(i, 1, r) {
    FOR_LE(j, 1, c) {
      // cout << i << " " << j << " " << v[i][j];
      {
        int64_t zerov = 0;
        FOR_LE(k, 0, 3) { zerov = max(zerov, dp[i - 1][j][k]); }
        dp[i][j][0] = max(dp[i][j - 1][0], zerov);
      }
      FOR_LE(k, 1, 3) { dp[i][j][k] = dp[i][j - 1][k]; }
      if (v[i][j] != 0) {
        FOR_DW(k, 3, 1) {
          dp[i][j][k] = max(dp[i][j - 1][k], dp[i][j][k - 1] + v[i][j]);
        }
      }
      // cout << " : " << dp[i][j][0] << " " << dp[i][j][1] << " " <<
      // dp[i][j][2] << " " << dp[i][j][3] << endl;
    }
  }

  int64_t ans = 0;
  FOR_LE(k, 0, 3) { ans = max(ans, dp[r][c][k]); }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

vector<map<int, int64_t>> ts(3001);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int r, c, k;
  cin >> r >> c >> k;
  FOR_LT(i, 0, k) {
    int cr, cc, cv;
    cin >> cr >> cc >> cv;
    cr--;
    cc--;
    ts[cr].insert({cc, cv});
  }

  map<int, int64_t> vs;
  vs.insert({0, 0});
  FOR_LT(i, 0, r) {
    map<int, int64_t> nvs;
    for (auto v : vs) {
      int idx = v.first;
      int64_t d = v.second;
      if (nvs.find(idx) == nvs.end()) {
        nvs[idx] = d;
      }

      auto it = ts[i].lower_bound(idx);
      multiset<int64_t> cd;
      while (it != ts[i].end()) {
        cd.insert(it->second);
        if (cd.size() > 3) {
          cd.erase(cd.begin());
        }

        int64_t cv = d;
        for (auto cvd : cd) {
          cv += cvd;
        }

        if (nvs.find(it->first) == nvs.end()) {
          nvs[it->first] = cv;
        }
        nvs[it->first] = max(nvs[it->first], cv);
        it++;
      }
    }

    vs = nvs;
  }

  int64_t ans = 0;
  for (auto v : vs) {
    ans = max(ans, v.second);
  }
  cout << ans << endl;

  return 0;
}