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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
int main() {
  ll n, d;
  while (cin >> n >> d, n) {
    ll used[11111] = {};
    ll cost[11111] = {};
    ll cnt = 0;
    priority_queue<P> q1, q2;
    ll mi = 11111111;
    for (int i = 0; i < n; i++) {
      ll m;
      cin >> m;
      ll sum = 0;
      for (int j = 0; j < m; j++) {
        cin >> cost[cnt];
        sum += cost[cnt];
        cnt++;
      }
      mi = min(mi, sum);
      q1.push(P(sum, cnt - 1));
      q2.push(P(sum - cost[cnt - 1], cnt - 1));
    }
    if (q1.top().first - mi > d) {
      cout << "No" << endl;
      return 0;
    }
    ll f = 1;
    cnt = 1;
    while (!q1.empty() || !q2.empty()) {
      // cout<<cnt<<" "<<q1.size()<<" "<<q2.size()<<" "<<q1.top().first<<endl;
      cnt++;
      while (!q2.empty()) {
        ll sum = q2.top().first;
        ll k = q2.top().second;
        if (used[k] || q1.top().second == k) {
          q2.pop();
          continue;
        }
        if (q1.top().first - sum <= d) {
          q2.pop();
          used[k] = 1;
          if (sum - cost[k - 1] >= 0) {
            q2.push(P(sum - cost[k - 1], k - 1));
            q1.push(P(sum, k - 1));
          }
          // cout<<"q2 "<<sum<<" "<<k<<endl;
          continue;
        }
        break;
      }
      while (!q1.empty() && used[q1.top().second]) {
        q1.pop();
      }
      if (q1.empty())
        break;
      ll ma = q1.top().first;
      ll nm = q1.top().second;
      // cout<<"q1 "<<ma<<" "<<nm<<endl;
      q1.pop();
      while (!q1.empty() && used[q1.top().second]) {
        q1.pop();
      }
      if (q1.top().first - (ma - cost[nm]) > d) {
        f = 0;
        break;
      }
      if (ma - cost[nm] > 0) {
        q1.push(P(ma - cost[nm], nm - 1));
        q2.push(P(ma - cost[nm] - cost[nm - 1], nm - 1));
      }
      used[nm] = 1;
    }
    cout << (f ? "Yes" : "No") << endl;
    ;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
int main() {
  ll n, d;
  cin >> n >> d;
  ll used[11111] = {};
  ll cost[11111] = {};
  ll cnt = 0;
  priority_queue<P> q1, q2;
  ll mi = 11111111;
  for (int i = 0; i < n; i++) {
    ll m;
    cin >> m;
    ll sum = 0;
    for (int j = 0; j < m; j++) {
      cin >> cost[cnt];
      sum += cost[cnt];
      cnt++;
    }
    mi = min(mi, sum);
    q1.push(P(sum, cnt - 1));
    q2.push(P(sum - cost[cnt - 1], cnt - 1));
  }
  if (q1.top().first - mi > d) {
    cout << "No" << endl;
    return 0;
  }
  ll f = 1;
  cnt = 1;
  while (!q1.empty() || !q2.empty()) {
    cout << cnt << " " << q1.size() << " " << q2.size() << endl;
    cnt++;
    while (!q2.empty()) {
      ll sum = q2.top().first;
      ll k = q2.top().second;
      if (used[k] || q1.top().second == k) {
        q2.pop();
        continue;
      }
      cout << "q2 " << sum << " " << k << endl;
      if (q1.top().first - sum <= d) {
        q2.pop();
        used[k] = 1;
        if (sum - cost[k - 1] >= 0)
          q2.push(P(sum - cost[k - 1], k - 1));
        continue;
      }
      break;
    }
    while (!q1.empty() && used[q1.top().second]) {
      q1.pop();
    }
    ll ma = q1.top().first;
    ll nm = q1.top().second;
    cout << "q1 " << ma << " " << nm << endl;
    q1.pop();
    while (!q1.empty() && used[q1.top().second]) {
      q1.pop();
    }
    if (q1.top().first - (ma - cost[nm]) > d) {
      f = 0;
      break;
    }
    if (ma - cost[nm] >= 0)
      q1.push(P(ma - cost[nm], nm - 1));
    used[nm] = 1;
  }
  cout << (f ? "Yes" : "No") << endl;
  ;
  return 0;
}