Problem p00825 (C++) — buggy submission, verdict: Memory Limit Exceeded

// template {{{

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

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

template <typename T> using max_pq = priority_queue<T, vector<T>, less<T>>;

template <typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline bool chmax(T &x, U y) {
  if (x >= y)
    return false;
  x = y;
  return true;
}

template <typename T, typename U> inline bool chmin(T &x, U y) {
  if (x <= y)
    return false;
  x = y;
  return true;
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  std::sort(std::begin(c), std::end(c));
  c.erase(std::unique(all(c)), std::end(c));
}

// }}}

using tp = tuple<int, int, int>;

int n;
tp a[1000];
int dp[2][366][366];

/* MLE tsurai.
int calc(int i, int j, int k)
{
        if (i == n) return 0;
        int &res = dp[i][j][k];
        if (~res) return res;

        int x, y, z;
        tie(x, y, z) = a[i];

        chmax(res, calc(i + 1, j, k));
        if (x >= j) chmax(res, z + calc(i + 1, y, k));
        if (x >= k) chmax(res, z + calc(i + 1, j, y));

        return res;
}
*/

int main() {
  while (cin >> n, n) {
    rep(i, n) {
      int x, y, z;
      cin >> x >> y >> z;
      a[i] = tie(--x, y, z);
    }
    sort(a, a + n);

    /* memoka saiki shitai.
    memset(dp, -1, sizeof(dp));
    cout << calc(0, 0, 0) << endl;
    */

    // kanashimi.
    memset(dp, 0, sizeof(dp));
    rep(i, n) {
      int src = i & 1, dst = ~i & 1;
      memset(dp[dst], 0, sizeof(dp[dst]));
      int x, y, z;
      tie(x, y, z) = a[i];
      rep(j, x + 1) rep(k, 366) {
        chmax(dp[dst][y][k], dp[src][j][k] + z);
        chmax(dp[dst][k][y], dp[src][k][j] + z);
      }
      rep(j, 366) rep(k, 366) { chmax(dp[dst][j][k], dp[src][j][k]); }
    }

    int res = 0;
    rep(i, 366) rep(j, 366) { chmax(res, dp[n & 1][i][j]); }

    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

// template {{{

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

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

template <typename T> using max_pq = priority_queue<T, vector<T>, less<T>>;

template <typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline bool chmax(T &x, U y) {
  if (x >= y)
    return false;
  x = y;
  return true;
}

template <typename T, typename U> inline bool chmin(T &x, U y) {
  if (x <= y)
    return false;
  x = y;
  return true;
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  std::sort(std::begin(c), std::end(c));
  c.erase(std::unique(all(c)), std::end(c));
}

// }}}

using tp = tuple<int, int, int>;

int n;
tp a[1000];
int dp[1000][366][366];

int calc(int i, int j, int k) {
  if (i == n)
    return 0;
  int &res = dp[i][j][k];
  if (~res)
    return res;

  int x, y, z;
  tie(x, y, z) = a[i];

  chmax(res, calc(i + 1, j, k));
  if (x >= j)
    chmax(res, z + calc(i + 1, y, k));
  if (x >= k)
    chmax(res, z + calc(i + 1, j, y));

  return res;
}

int main() {
  while (cin >> n, n) {
    rep(i, n) {
      int x, y, z;
      cin >> x >> y >> z;
      a[i] = tie(--x, y, z);
    }
    sort(a, a + n);

    memset(dp, -1, sizeof(dp));
    cout << calc(0, 0, 0) << endl;
  }
}