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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
  int n, d;
  while (cin >> n >> d, n) {
    vvi miss(n);
    vi sum(n, 0);
    REP(i, n) {
      int cn;
      cin >> cn;
      REP(j, cn) {
        int a;
        cin >> a;
        miss[i].push_back(a);
        sum[i] += a;
      }
    }
    bool clear = true;
    int ma4 = 0;
    int mi4 = 1e9;
    REP(i, n) {
      ma4 = max(ma4, sum[i]);
      mi4 = min(mi4, sum[i]);
    }
    if (ma4 - mi4 > d) {
      clear = false;
      break;
    }

    while (1) {
      int ma = 0;
      int ma2 = 0;
      int maidx;
      REP(i, sum.size()) {
        if (sum[i] == 0)
          goto res;
        if (ma < sum[i]) {
          ma = sum[i];
          ma2 = miss[i][miss[i].size() - 1];
          maidx = i;
        }
        if (ma == sum[i]) {
          if (ma2 < miss[i][miss[i].size() - 1]) {
            ma = sum[i];
            ma2 = miss[i][miss[i].size() - 1];
            maidx = i;
          }
        }
      }
      REP(i, n) {
        if (i == maidx)
          continue;
        while (sum[i] - miss[i][miss[i].size() - 1] >= ma - d) {
          sum[i] -= miss[i][miss[i].size() - 1];
          miss[i].erase(miss[i].end() - 1);
          if (sum[i] == 0)
            goto res;
        }
      }
      sum[maidx] -= miss[maidx][miss[maidx].size() - 1];
      miss[maidx].erase(miss[maidx].end() - 1);
      int ma3 = 0;
      int mi3 = 1e9;
      REP(i, n) {
        ma3 = max(ma3, sum[i]);
        mi3 = min(mi3, sum[i]);
      }
      if (ma3 - mi3 > d) {
        clear = false;
        break;
      }
    }
  res:
    if (clear)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
  int n, d;
  while (cin >> n >> d, n) {
    vvi miss(n);
    vi sum(n, 0);
    REP(i, n) {
      int cn;
      cin >> cn;
      REP(j, cn) {
        int a;
        cin >> a;
        miss[i].push_back(a);
        sum[i] += a;
      }
    }
    bool clear = true;
    int ma4 = 0;
    int mi4 = 1e9;
    REP(i, n) {
      ma4 = max(ma4, sum[i]);
      mi4 = min(mi4, sum[i]);
    }
    if (ma4 - mi4 > d) {
      clear = false;
      break;
    }

    while (1) {
      int ma = 0;
      int ma2 = 0;
      int maidx;
      REP(i, sum.size()) {
        if (sum[i] == 0)
          goto res;
        if (ma < sum[i]) {
          ma = sum[i];
          ma2 = miss[i][0];
          maidx = i;
        }
        if (ma == sum[i]) {
          if (ma2 < miss[i][0]) {
            ma = sum[i];
            ma2 = miss[i][0];
            maidx = i;
          }
        }
      }
      REP(i, n) {
        if (i == maidx)
          continue;
        while (sum[i] - miss[i][0] >= ma - d) {
          sum[i] -= miss[i][0];
          miss[i].erase(miss[i].begin());
        }
      }
      sum[maidx] -= miss[maidx][0];
      miss[maidx].erase(miss[maidx].begin());
      int ma3 = 0;
      int mi3 = 1e9;
      REP(i, n) {
        ma3 = max(ma3, sum[i]);
        mi3 = min(mi3, sum[i]);
      }
      if (ma3 - mi3 > d) {
        clear = false;
        break;
      }
    }
  res:
    if (clear)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}