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

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

int main() {
  for (int n; cin >> n && n;) {
    vector<string> ps(n);
    vector<int> ts(n);
    for (int i = 0; i < n; i++) {
      cin >> ps[i];
      int w, s;
      cin >> w >> s;
      ts[i] = (w * 24 * 60 + s / 100 * 60 + s % 100) % (7 * 24 * 60);
    }
    int m;
    cin >> m;
    vector<int> fs(n);
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      fs[find(begin(ps), end(ps), s) - begin(ps)] = 1;
    }

    for (int i = 0; i < n; i++)
      ts.push_back(ts[i] + 7 * 24 * 60);
    vector<int> is(n * 2);
    iota(begin(is), end(is), 0);
    sort(begin(is), end(is), [&](int i, int j) { return ts[i] < ts[j]; });

    int res = -1;
    for (int l = 0, r = 7 * 24 * 60; l < 30; l++, r++) {
      vector<int> dp(r + 1);
      int p = 0;
      for (int i : is) {
        int t = ts[i];
        if (l <= t && t + 30 <= r) {
          for (; p < t; p++)
            dp[t] = max(dp[t], dp[p]);
          dp[t + 30] = max(dp[t + 30], dp[t] + (fs[i] ? 1000 : 1));
        }
      }
      for (; p < r; p++)
        dp[r] = max(dp[r], dp[p]);
      if (dp[r] / 1000 != m)
        continue;
      res = max(res, dp[r] / 1000 + dp[r] % 1000);
    }

    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

int main() {
  for (int n; cin >> n && n;) {
    vector<string> ps(n);
    vector<int> ts(n);
    for (int i = 0; i < n; i++) {
      cin >> ps[i];
      int w, s;
      cin >> w >> s;
      ts[i] = (w * 24 * 60 + s / 100 * 60 + s % 60) % (7 * 24 * 60);
    }
    int m;
    cin >> m;
    vector<int> fs(n);
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      fs[find(begin(ps), end(ps), s) - begin(ps)] = 1;
    }

    for (int i = 0; i < n; i++)
      ts.push_back(ts[i] + 7 * 24 * 60);
    vector<int> is(n * 2);
    iota(begin(is), end(is), 0);
    sort(begin(is), end(is), [&](int i, int j) { return ts[i] < ts[j]; });

    int res = -1;
    for (int l = 0, r = 7 * 24 * 60; l < 1440; l++, r++) {
      {
        vector<int> dp(r + 1);
        int p = 0;
        for (int i : is)
          if (fs[i]) {
            int t = ts[i];
            if (l <= t && t + 30 <= r) {
              for (; p < t; p++)
                dp[t] = max(dp[t], dp[p]);
              dp[t + 30] = max(dp[t + 30], dp[t] + 1);
            }
          }
        for (; p < r; p++)
          dp[r] = max(dp[r], dp[p]);
        if (dp[r] != m)
          continue;
      }
      {
        vector<int> dp(r + 1);
        int p = 0;
        for (int i : is) {
          int t = ts[i];
          if (l <= t && t + 30 <= r) {
            for (; p < t; p++)
              dp[t] = max(dp[t], dp[p]);
            dp[t + 30] = max(dp[t + 30], dp[t] + 1);
          }
        }
        for (; p < r; p++)
          dp[r] = max(dp[r], dp[p]);
        res = max(res, dp[r]);
      }
    }

    cout << res << endl;
  }
}