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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct T {
  int s, e, c;
  T(int _s, int _e, int _c) {
    s = _s;
    e = _e;
    c = _c;
  };

  bool operator<(const T &t) const { return s < t.s; }
};

const int MAX_N = 1001;
const int DAY = 366;
int N;

int dp[2][DAY][DAY];
vector<T> vec;

void input() {
  for (int i = 0; i < N; i++) {
    int s, e, c;
    cin >> s >> e >> c;
    vec.push_back(T(s, e, c));
  }
  sort(vec.begin(), vec.end());
  /*
  for(int i = 0 ; i < vec.size() ; i++){
    cout << "i = " << i << " vec[i].s = " << vec[i].s << endl;
  }
  */
}

void init() {
  vec.clear();
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < DAY; j++) {
      for (int k = 0; k < DAY; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
}

void solve() {
  init();
  input();

  int res = 0;
  int now = 0;
  int next = 1;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < DAY; j++) {
      for (int k = 0; k < DAY; k++) {
        dp[next][j][k] = max(dp[next][j][k], dp[now][j][k]);
        int a, b, c;
        a = b = c = 0;
        if (j < vec[i].s)
          a = dp[next][vec[i].e][k] =
              max(dp[next][vec[i].e][k], dp[now][j][k] + vec[i].c);
        if (k < vec[i].s)
          b = dp[next][j][vec[i].e] =
              max(dp[next][j][vec[i].e], dp[now][j][k] + vec[i].c);
        res = max(res, max(a, max(b, c)));
      }
    }

    for (int j = 0; j < DAY; j++) {
      for (int k = 0; k < DAY; k++) {
        dp[now][j][k] = dp[next][j][k];
        dp[next][j][k] = 0;
      }
    }
  }
  cout << res << endl;
}

int main() {
  while (cin >> N, N) {
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct T {
  int s, e, c;
  T(int _s, int _e, int _c) {
    s = _s;
    e = _e;
    c = _c;
  };

  bool operator<(const T &t) const { return s < t.s; }
};

const int MAX_N = 1001;
const int DAY = 366;
int N;

int dp[MAX_N][DAY][DAY];
vector<T> vec;

void input() {
  for (int i = 0; i < N; i++) {
    int s, e, c;
    cin >> s >> e >> c;
    vec.push_back(T(s, e, c));
  }
  sort(vec.begin(), vec.end());
  /*
  for(int i = 0 ; i < vec.size() ; i++){
    cout << "i = " << i << " vec[i].s = " << vec[i].s << endl;
  }
  */
}

void init() {
  vec.clear();
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < DAY; j++) {
      for (int k = 0; k < DAY; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
}

void solve() {
  init();
  input();

  int res = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < DAY; j++) {
      for (int k = 0; k < DAY; k++) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        int a, b, c;
        a = b = c = 0;
        if (j < vec[i].s)
          a = dp[i + 1][vec[i].e][k] =
              max(dp[i + 1][vec[i].e][k], dp[i][j][k] + vec[i].c);
        if (k < vec[i].s)
          b = dp[i + 1][j][vec[i].e] =
              max(dp[i + 1][j][vec[i].e], dp[i][j][k] + vec[i].c);
        res = max(res, max(a, max(b, c)));
      }
    }
  }
  cout << res << endl;
}

int main() {
  while (cin >> N, N) {
    solve();
  }
  return 0;
}