Problem p01294 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef deque<int> di;

struct Stat {
  int sum, nxt, i;

  Stat() {}
  Stat(int _sum, int _nxt, int _i) : sum(_sum), nxt(_nxt), i(_i) {}

  bool operator<(const Stat &s0) const {
    return sum > s0.sum || (sum == s0.sum && nxt > s0.nxt);
  }
};

typedef set<Stat> ss;

/* global variables */

int n, d;
di msls[MAX_N];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n >> d;
    if (n == 0)
      break;
    // printf("n=%d, d=%d\\n", n, d);

    ss q;
    int min_sum = INF;

    for (int i = 0; i < n; i++) {
      di &mslsi = msls[i];
      int mn, mj, sum = 0;
      cin >> mn;
      // printf("  i=%d,mn=%d\\n", i, mn);

      mslsi.clear();
      for (int j = 0; j < mn; j++) {
        cin >> mj;
        mslsi.push_back(mj);
        sum += mj;
      }

      if (sum > 0)
        q.insert(Stat(sum, sum - mslsi.back(), i));
      if (min_sum > sum)
        min_sum = sum;
    }

    bool ok = true;

    while (ok && q.size() > 1) {
      ok = false;

      for (ss::iterator sit = q.begin(); sit != q.end(); sit++) {
        Stat u = *sit;
        // printf("u(sum=%d,nxt=%d,i=%d),min_sum=%d\\n",u.sum,u.nxt,u.i,min_sum);

        int max_sum0 = q.begin()->sum;
        if (sit == q.begin()) {
          ss::iterator sit0 = sit;
          max_sum0 = (++sit0)->sum;
        }

        int min_sum0 = min(min_sum, u.nxt);

        if (max_sum0 - min_sum0 <= d) {
          ok = true;
          q.erase(sit);

          di &mslsu = msls[u.i];
          mslsu.pop_back();
          u.sum = u.nxt;
          if (u.nxt > 0)
            u.nxt -= mslsu.back();
          if (u.sum > 0)
            q.insert(u);

          min_sum = min_sum0;
          break;
        }
      }
    }

    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef deque<int> di;

struct Stat {
  int sum, nxt, i;

  Stat() {}
  Stat(int _sum, int _nxt, int _i) : sum(_sum), nxt(_nxt), i(_i) {}

  bool operator<(const Stat &s0) const {
    return nxt < s0.nxt || (nxt == s0.nxt && sum < s0.sum);
  }
};

/* global variables */

int n, d;
di msls[MAX_N];

/* subroutines */

/* main */

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

    priority_queue<Stat> q;
    int min_sum = INF;

    for (int i = 0; i < n; i++) {
      di &mslsi = msls[i];
      int mn, mj, sum = 0;
      cin >> mn;

      mslsi.resize(mn, 0);
      for (int j = 0; j < mn; j++) {
        cin >> mj;
        mslsi[j] = mj;
        sum += mj;
      }

      q.push(Stat(sum, sum - mslsi.back(), i));
      if (min_sum > sum)
        min_sum = sum;
    }

    bool ok = true;

    while (!q.empty()) {
      Stat u = q.top();
      q.pop();

      // printf("u(sum=%d,nxt=%d,i=%d), min_sum=%d\\n", u.sum, u.nxt, u.i,
      // min_sum);
      if (u.sum - min_sum > d) {
        ok = false;
        break;
      }

      di &mslsu = msls[u.i];
      mslsu.pop_back();

      u.sum = u.nxt;
      if (u.nxt > 0)
        u.nxt -= mslsu.back();

      if (min_sum > u.sum)
        min_sum = u.sum;
      if (u.sum > 0)
        q.push(u);
    }

    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}