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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Data {
public:
  int t1, t2, r1, r2;
  bool operator<(const Data &d) const { return t1 < d.t1; }
};

int n;
vector<Data> d;
vector<int> nextTV;

vector<vector<int>> memo;

int solve(int a, int b) {
  if (a == n + 1 || b == n + 1)
    return 0;
  if (a == n && b == n)
    return 0;
  if (memo[a][b] != -1)
    return memo[a][b];

  int ret = 0;
  ret = max(ret, solve(a, b + 1));
  ret = max(ret, solve(a + 1, b));
  if (a == b) {
    ret = max(ret, d[a].r1 + solve(nextTV[a], b + 1));
    ret = max(ret, d[b].r2 + solve(a + 1, nextTV[b]));
  } else if (a > b) {
    ret = max(ret, d[b].r2 + solve(a, nextTV[b]));
  } else {
    ret = max(ret, d[a].r1 + solve(nextTV[a], b));
  }

  return memo[a][b] = ret;
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;

    d.resize(n + 1);
    for (int i = 0; i < n; ++i) {
      string s;
      int h1, m1, h2, m2;
      char c;
      cin >> s >> h1 >> c >> m1 >> h2 >> c >> m2 >> d[i].r1 >> d[i].r2;
      d[i].t1 = h1 * 60 + m1;
      d[i].t2 = h2 * 60 + m2;
    }
    d[n].t1 = INT_MAX / 2;
    d[n].t2 = INT_MAX / 2 + 1;
    d[n].r1 = d[n].r2 = 0;
    sort(d.begin(), d.end());

    nextTV.resize(n + 1);
    for (int i = 0; i <= n; ++i) {
      Data tmp;
      tmp.t1 = d[i].t2;
      nextTV[i] = lower_bound(d.begin(), d.end(), tmp) - d.begin();
    }

    memo.assign(n + 1, vector<int>(n + 1, -1));
    cout << solve(0, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Data {
public:
  int t1, t2, r1, r2;
  bool operator<(const Data &d) const { return t1 < d.t1; }
};

int n;
vector<Data> d;
vector<int> nextTV;

vector<vector<int>> memo;

int solve(int a, int b) {
  if (a == n + 1 || b == n + 1)
    return 0;
  if (a == n && b == n)
    return 0;
  if (memo[a][b] != -1)
    return memo[a][b];

  int ret = 0;
  ret = max(ret, solve(a, b + 1));
  ret = max(ret, solve(a + 1, b));
  if (a == b) {
    ret = max(ret, d[a].r1 + solve(nextTV[a], b + 1));
    ret = max(ret, d[b].r2 + solve(a + 1, nextTV[b]));
  } else if (a > b) {
    ret = max(ret, d[b].r2 + solve(a, nextTV[b]));
  } else {
    ret = max(ret, d[a].r1 + solve(nextTV[a], b));
  }
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;

    d.resize(n + 1);
    for (int i = 0; i < n; ++i) {
      string s;
      int h1, m1, h2, m2;
      char c;
      cin >> s >> h1 >> c >> m1 >> h2 >> c >> m2 >> d[i].r1 >> d[i].r2;
      d[i].t1 = h1 * 60 + m1;
      d[i].t2 = h2 * 60 + m2;
    }
    d[n].t1 = INT_MAX / 2;
    d[n].t2 = INT_MAX / 2 + 1;
    d[n].r1 = d[n].r2 = 0;
    sort(d.begin(), d.end());

    nextTV.resize(n + 1);
    for (int i = 0; i <= n; ++i) {
      Data tmp;
      tmp.t1 = d[i].t2;
      nextTV[i] = lower_bound(d.begin(), d.end(), tmp) - d.begin();
    }

    memo.assign(n + 1, vector<int>(n + 1, -1));
    cout << solve(0, 0) << endl;
  }
}