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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  int n, d;
  while (cin >> n >> d, n || d) {
    stack<int> stk[n];
    int sum[n];
    REP(i, n) sum[i] = 0;
    REP(i, n) {
      int m;
      cin >> m;
      REP(j, m) {
        int a;
        cin >> a;
        stk[i].push(a);
        sum[i] += a;
      }
    }
    bool ok = 0;
    while (1) {
      int ma = 0;
      int ma2 = 0;
      int id = -1;
      int mi = INF;
      REP(i, n) {
        if (sum[i] > ma) {
          ma2 = ma;
          ma = sum[i], id = i;
        } else if (sum[i] > ma2) {
          ma2 = sum[i];
        }
        mi = min(mi, sum[i]);
      }
      if (ma - mi > d)
        break;
      if (id == -1) {
        ok = 1;
        break;
      }
      bool f = 0;
      REP(i, n) {
        if (stk[i].size() == 0)
          continue;
        if (i == id) {
          if (ma2 - (sum[i] - stk[i].top()) > d)
            continue;
        } else {
          if (ma - (sum[i] - stk[i].top()) > d)
            continue;
        }
        sum[i] -= stk[i].top();
        stk[i].pop();
        f = 1;
        break;
      }
      // REP(i,n) cout << (stk[i].size()?stk[i].top():-1) << " ";
      // cout << endl;
      // REP(i,n) cout << sum[i] << ":";
      // cout << endl;
      if (!f)
        break;
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  int n, d;
  while (cin >> n >> d, n || d) {
    stack<int> stk[n];
    int sum[n];
    REP(i, n) sum[i] = 0;
    REP(i, n) {
      int m;
      cin >> m;
      REP(j, m) {
        int a;
        cin >> a;
        stk[i].push(a);
        sum[i] += a;
      }
    }
    bool update = 1;
    bool ok = 0;
    while (1) {
      int hoge = 0;
      int id = -1;
      int ma = 0;
      int mi = INF;
      REP(i, n) {
        if (hoge < sum[i] - stk[i].top()) {
          hoge = sum[i] - stk[i].top();
          id = i;
        }
        ma = max(ma, sum[i]);
        mi = min(mi, sum[i]);
      }
      if (ma - mi > d)
        break;
      if (id == -1) {
        ok = 1;
        break;
      }
      // REP(i,n) cout << stk[i].top() << " ";
      // cout << endl;
      sum[id] -= stk[id].top();
      stk[id].pop();
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}