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

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

int dp[2][DAY + 1][DAY + 1];
vector<vector<pair<int, int>>> data;
int N;
/*
int dfs(int idx, int n1, int n2){
        if(idx >= 366) return 0;
        if(n1 < idx) n1 = 0;
        if(n2 < idx) n2 = 0;
        if(dp[idx][n1][n2] != -1) return dp[idx][n1][n2];
        int ans = dfs(idx + 1, n1, n2);
        if(n1 < idx && n2 < idx){
                for(int i = 0; i < data[idx].size(); i++){
                        for(int j = i + 1; j < data[idx].size(); j++){
                                int n3 = data[idx][i].first;
                                int n4 = data[idx][j].first;
                                ans = max(ans, dfs(idx + 1, max(n3, n4), min(n3,
n4)) + data[idx][i].second + data[idx][j].second);
                        }
                }
        }
        if(n2 < idx){
                for(auto p : data[idx]){
                        int n3 = p.first;
                        ans = max(ans, dfs(idx + 1, max(n1, n3), min(n1, n3)) +
p.second);
                }
        }
        //printf("(%d %d %d) = %d\\n", idx, n1, n2, ans);
        return dp[idx][n1][n2] = ans;
}
*/

int main() {
  while (cin >> N, N) {
    data.assign(366, vector<pair<int, int>>());
    for (int i = 0; i < N; i++) {
      int a, b, w;
      cin >> a >> b >> w;
      data[a].push_back(pair<int, int>(b, w));
    }
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 366; j++) {
        for (int k = 0; k < 366; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
    for (int i = DAY; i > 0; i--) {
      for (int j = 0; j <= DAY; j++) {
        for (int k = 0; k <= DAY; k++) {
          int idx = i;
          int n1 = j;
          int n2 = k;
          if (n1 < idx)
            n1 = 0;
          if (n2 < idx)
            n2 = 0;
          int ci = idx % 2;

          int ans = dp[ci][n1][n2];
          if (n1 < idx && n2 < idx) {
            for (int i = 0; i < data[idx].size(); i++) {
              for (int j = i + 1; j < data[idx].size(); j++) {
                int n3 = data[idx][i].first;
                int n4 = data[idx][j].first;
                ans = max(ans, dp[ci][max(n3, n4)][min(n3, n4)] +
                                   data[idx][i].second + data[idx][j].second);
              }
            }
          }
          if (n2 < idx) {
            for (auto p : data[idx]) {
              int n3 = p.first;
              ans = max(ans, dp[ci][max(n1, n3)][min(n1, n3)] + p.second);
            }
          }
          dp[(i + 1) & 1][j][k] = ans;
        }
      }
    }

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

Error: MLE

Fixed submission:

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

vector<array<int, 3>> input;
map<array<int, 3>, int> dp;
int N;
int dfs(int i, int n1, int n2) {
  if (i >= N)
    return 0;
  if (n1 < input[i][0]) {
    n1 = 0;
  }
  if (n2 < input[i][0]) {
    n2 = 0;
  }
  if (dp.count({i, n1, n2})) {
    return dp[{i, n1, n2}];
  }
  int ans = dfs(i + 1, n1, n2);
  int a2 = 0;
  int nn1 = n1;
  int nn2 = n2;
  if (nn1 < input[i][0]) {
    nn1 = input[i][1];
    a2 += input[i][2];
  } else if (nn2 < input[i][0]) {
    nn2 = input[i][1];
    a2 += input[i][2];
  }
  ans = max(ans, dfs(i + 1, max(nn1, nn2), min(nn1, nn2)) + a2);
  dp[{i, n1, n2}] = ans;
  return ans;
}

int main() {
  while (cin >> N, N) {
    input.clear();
    for (int i = 0; i < N; i++) {
      int a, b, w;
      cin >> a >> b >> w;
      input.push_back({a, b, w});
    }
    sort(input.begin(), input.end());
    dp.clear();
    cout << dfs(0, 0, 0) << endl;
    cout << dp.size() << endl;
  }
}