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

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
LL floor_usr(LL a, int b) { return (a + (b - 1)) / b; }
const int maxN = 200001;
template <class dt> class dp_info {
public:
  dt val;
  int maxnum;
  priority_queue<dt, vector<dt>, greater<dt>> items;
  dp_info(dt val_, int maxnum_) {
    val = val_;
    rep(i, maxnum_) items.push(0);
    maxnum = maxnum_;
  }
  dp_info replace(dt newitem) {
    dp_info ret(val, maxnum);
    ret.items = items;
    if (!ret.items.empty()) {
      dt minv = ret.items.top();
      if (minv < newitem) {
        ret.items.push(newitem);
        ret.items.pop();
        ret.val = ret.val + newitem - minv;
      }
    } else {
      ret.items.push(newitem);
      ret.val += newitem;
    }
    return ret;
  }
  static dp_info max(const dp_info &a, const dp_info &b) {
    if (a.val > b.val)
      return a;
    else
      return b;
  }
  dp_info get_clear() {
    dp_info ret(val, maxnum);
    return ret;
  }
};
int main() {
  using dt = LL;
  int maxnum = 3;
  dt R, C, K;
  scanf("%lld %lld %lld", &R, &C, &K);
  vector<vector<dt>> v(R, vector<dt>(C, 0));
  vector<vector<vector<dt>>> dp(
      R, vector<vector<dt>>(C, vector<dt>(maxnum + 1, 0)));
  dt r_, c_, v_;
  rep(i, K) {
    scanf("%lld %lld %lld", &r_, &c_, &v_);
    v[r_ - 1][c_ - 1] = v_;
  }
  rep(r, R) {
    if (r == 0) {
      rep(c, C) {
        if (c == 0) {
          dp[r][c][0] = 0;
          dp[r][c][1] = dp[r][c][0] + v[r][c];
          rep(i, maxnum - 1) dp[r][c][i + 2] = dp[r][c][i + 1];
        } else {
          dp[r][c] = dp[r][c - 1];
          rep(k, maxnum + 1) {
            if (k == 0)
              continue;
            else {
              dp[r][c][k] = max(dp[r][c][k], dp[r][c - 1][k - 1] + v[r][c]);
              dp[r][c][k] = max(dp[r][c][k], dp[r][c][k - 1]);
            }
          }
        }
      }
    } else {
      rep(c, C) {
        if (c == 0) {
          dp[r][c][0] = dp[r - 1][c][maxnum];
          dp[r][c][1] = dp[r][c][0] + v[r][c];
          rep(i, maxnum - 1) dp[r][c][i + 2] = dp[r][c][i + 1];
        } else {
          dp[r][c][0] = max(dp[r - 1][c][maxnum], dp[r][c - 1][0]);
          dp[r][c][1] = max(dp[r][c][0] + v[r][c], dp[r][c - 1][1]);
          rep(k, maxnum + 1) {
            if (k <= 1)
              continue;
            else {
              dp[r][c][k] = max(dp[r][c - 1][k], dp[r][c - 1][k - 1] + v[r][c]);
              dp[r][c][k] = max(dp[r][c][k], dp[r][c][k - 1]);
            }
          }
        }
      }
    }
  }
  cout << dp[R - 1][C - 1][maxnum];
  // 真の値より一回小さいnが得られているので、追加するiを選んで最小値を求める
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

#define rep(i, n) for (int i = 0; i < (n); i++)
LL floor_usr(LL a, int b) { return (a + (b - 1)) / b; }
const int maxN = 200001;
template <class dt> class dp_info {
public:
  dt val;
  int maxnum;
  priority_queue<dt, vector<dt>, greater<dt>> items;
  dp_info(dt val_, int maxnum_) {
    val = val_;
    rep(i, maxnum_) items.push(0);
    maxnum = maxnum_;
  }
  dp_info replace(dt newitem) {
    dp_info ret(val, maxnum);
    ret.items = items;
    if (!ret.items.empty()) {
      dt minv = ret.items.top();
      if (minv < newitem) {
        ret.items.push(newitem);
        ret.items.pop();
        ret.val = ret.val + newitem - minv;
      }
    } else {
      ret.items.push(newitem);
      ret.val += newitem;
    }
    return ret;
  }
  static dp_info max(dp_info a, dp_info b) {
    if (a.val > b.val)
      return a;
    else
      return b;
  }
  dp_info get_clear() {
    dp_info ret(val, maxnum);
    return ret;
  }
};
int main() {
  using dt = LL;
  int maxnum = 3;
  dt R, C, K;
  scanf("%lld %lld %lld", &R, &C, &K);
  vector<vector<dt>> v(R, vector<dt>(C, 0));
  vector<vector<vector<dp_info<dt>>>> dp(
      R, vector<vector<dp_info<dt>>>(
             C, vector<dp_info<dt>>(maxnum + 1, dp_info<dt>(0, maxnum))));
  dt r_, c_, v_;
  rep(i, K) {
    scanf("%lld %lld %lld", &r_, &c_, &v_);
    v[r_ - 1][c_ - 1] = v_;
  }
  rep(r, R) {
    if (r == 0) {
      rep(c, C) {
        if (c == 0) {
          dp[r][c][0] = dp_info<dt>(0, maxnum);
          dp[r][c][1] = dp[r][c][0].replace(v[r][c]);
          rep(i, maxnum - 1) dp[r][c][i + 2] = dp[r][c][i + 1];
        } else {
          dp[r][c] = dp[r][c - 1];
          rep(k, maxnum + 1) {
            if (k == 0)
              continue;
            else {
              dp[r][c][k] = dp_info<dt>::max(
                  dp[r][c][k], dp[r][c - 1][k - 1].replace(v[r][c]));
              dp[r][c][k] = dp_info<dt>::max(dp[r][c][k], dp[r][c][k - 1]);
            }
          }
        }
      }
    } else {
      rep(c, C) {
        if (c == 0) {
          dp[r][c][0] = dp[r - 1][c][maxnum].get_clear();
          dp[r][c][1] = dp[r][c][0].replace(v[r][c]);
          rep(i, maxnum - 1) dp[r][c][i + 2] = dp[r][c][i + 1];
        } else {
          dp[r][c][0] = dp_info<dt>::max(dp[r - 1][c][maxnum].get_clear(),
                                         dp[r][c - 1][0]);
          dp[r][c][1] =
              dp_info<dt>::max(dp[r][c][0].replace(v[r][c]), dp[r][c - 1][1]);
          rep(k, maxnum + 1) {
            if (k <= 1)
              continue;
            else {
              dp[r][c][k] = dp_info<dt>::max(
                  dp[r][c - 1][k], dp[r][c - 1][k - 1].replace(v[r][c]));
              dp[r][c][k] = dp_info<dt>::max(dp[r][c][k], dp[r][c][k - 1]);
            }
          }
        }
      }
    }
  }
  cout << dp[R - 1][C - 1][maxnum].val;
  // 真の値より一回小さいnが得られているので、追加するiを選んで最小値を求める
  return 0;
}