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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

signed main() {

  int d, n;
  cin >> d >> n;
  vector<int> t(d);
  for (int i = 0; i < d; i++)
    cin >> t[i];
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];

  vector<vector<int>> dp(d + 1, vector<int>(n, -1));
  for (int j = 0; j < n; j++) {
    if (a[j] <= t[0] && t[0] <= b[j])
      dp[0][j] = 0;
  }

  for (int i = 0; i < d - 1; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[i][j] == -1)
        continue;

      for (int k = 0; k < n; k++) {
        if (a[k] <= t[i + 1] && t[i + 1] <= b[k]) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + (int)abs(c[j] - c[k]));
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[d - 1][i]);
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

signed main() {

  while (1) {
    int n;
    cin >> n;

    vector<pair<int, int>> ans(n);
    for (int i = 0; i < n; i++) {
      int bangou;
      cin >> bangou;
      ans[i].second = bangou;
      int score = 0;
      int pre = 0; // 1 := スペア 2 := ストライク
      for (int j = 0; j < 9; j++) {
        int val;
        cin >> val;
        if (val == 10) {
          if (pre > 0)
            score += val;
          pre = 2;
          score += val;

          continue;
        } else {
          if (pre > 0)
            score += val;
          score += val;
        }

        int val2;
        cin >> val2;
        if (val + val2 == 10) {
          if (pre >= 2)
            score += val2;
          pre = 1;
          score += val2;
        } else {
          if (pre >= 2)
            score += val2;
          score += val2;
        }
      }

      int val;
      cin >> val;
      if (pre > 0)
        score += val;
      score += val;
      int val2;
      cin >> val2;
      if (pre >= 2)
        score += val2;
      score += val2;
      if (val + val2 == 10) {
        int val3;
        cin >> val3;
        score += val3;
      }

      ans[i].first = -score;
    }

    sort(ans.begin(), ans.end());
    for (int i = 0; i < n; i++)
      cout << ans[i].first << " " << ans[i].second << endl;
  }

  return 0;
}