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

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const double inf = 1e12;

const long double eps = 1e-8;

const i64 MAXN = 3'000 + 5;

i64 rows, cols, k;

vector<map<i64, i64>> z;
i64 dp[MAXN][MAXN][5];

inline i64 sum(set<pair<i64, i64>> &q) {
  i64 R = 0;
  for (auto t : q) {
    R -= t.first;
  }
  return R;
}

inline pair<i64, i64> sum3(set<pair<i64, i64>> &q) {
  i64 R = 0;
  i64 cnt = 0;
  i64 last = 0;
  for (auto t : q) {
    R -= t.first;
    last = max(last, t.second);
    cnt += 1;
    if (cnt == 3) {
      break;
    }
  }
  return {R, last};
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  cin >> rows >> cols >> k;

  z.resize(rows);

  for (i64 i = 0; i < k; i++) {
    i64 r, c, v;
    cin >> r >> c >> v;
    r -= 1;
    c -= 1;
    z[r][c] = v;
  }

  for (i64 r = rows - 1; r >= 0; r--) {
    for (i64 c = cols - 1; c >= 0; c--) {
      dp[r][c][0] = dp[r + 1][c][3];
      dp[r][c][1] = max(dp[r][c][0], dp[r][c + 1][1]);
      if (z[r].count(c)) {
        dp[r][c][1] = max(dp[r][c][1], dp[r + 1][c][3] + z[r][c]);
      }

      for (i64 k = 2; k <= 3; k++) {
        dp[r][c][k] =
            max(dp[r][c][k - 1],
                max(dp[r][c + 1][k],
                    dp[r][c + 1][k - 1] + (z[r].count(c) ? z[r][c] : 0)));
      }
    }
  }

  cout << dp[0][0][3] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const double inf = 1e12;

const long double eps = 1e-8;

const i64 MAXN = 400'000 + 5;

i64 rows, cols, k;

vector<map<i64, i64>> z;
vector<vector<i64>> dp;

inline i64 sum(set<pair<i64, i64>> &q) {
  i64 R = 0;
  for (auto t : q) {
    R -= t.first;
  }
  return R;
}

inline i64 sum3(set<pair<i64, i64>> &q) {
  i64 R = 0;
  i64 cnt = 0;
  for (auto t : q) {
    R -= t.first;
    cnt += 1;
    if (cnt == 3) {
      break;
    }
  }
  return R;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  cin >> rows >> cols >> k;

  z.resize(rows);

  for (i64 i = 0; i < k; i++) {
    i64 r, c, v;
    cin >> r >> c >> v;
    r -= 1;
    c -= 1;
    z[r][c] = v;
  }

  dp = vector<vector<i64>>(rows + 1, vector<i64>(cols + 1));
  for (i64 r = rows - 1; r >= 0; r--) {

    for (i64 c = cols - 1; c >= 0; c--) {
      dp[r][c] = dp[r][c + 1];

      set<pair<i64, i64>> q;
      for (i64 u = c; u < cols; u++) {
        if (z[r].count(u)) {
          q.insert({-z[r][u], u});
        }
        dp[r][c] = max(dp[r][c], dp[r + 1][u] + sum3(q));
      }
    }
  }

  cout << dp[0][0] << endl;

  return 0;
}