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

#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second

int main() {
  int n, m;
  while (cin >> m >> n, m || n) {
    vector<pii> query;
    vector<int> ans;
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      query.push_back(pii(t, i + 1));
    }

    queue<pii> cq;
    vector<stack<pii>> park;
    for (int i = 0; i < m; i++)
      park.push_back(stack<pii>());
    for (int i = 0; i < 20000; i++) {
      if (i % 10 == 0 && i / 10 < query.size()) {
        cq.push(query[i / 10]);
      }
      // out
      for (int j = 0; j < m; j++) {
        while (!park[j].empty() && park[j].top().fi <= i) {
          ans.push_back(park[j].top().se);
          park[j].pop();
        }
      }
      // in
      while (!cq.empty()) {
        bool flg = true;
        bool bkflg = true;
        for (int j = 0; j < m && flg; j++) {
          if (park[j].empty()) {
            park[j].push(cq.front());
            park[j].top().fi += i;
            cq.pop();
            flg = false;
          }
          bkflg &= park[j].size() == 2;
        }

        int mn = 20000, indx = -1;
        for (int j = 0; j < m && flg && !bkflg; j++) {
          if (park[j].size() == 1 && park[j].top().fi > i + cq.front().fi) {
            if (mn > abs(park[j].top().fi - (i + cq.front().fi))) {
              mn = abs(park[j].top().fi - (i + cq.front().fi));
              indx = j;
            }
          }
        }

        if (indx != -1) {
          park[indx].push(cq.front());
          park[indx].top().fi += i;
          cq.pop();
          flg = false;
        }

        for (int j = 0; j < m && flg && !bkflg; j++) {
          if (park[j].size() == 1) {
            if (mn > abs(park[j].top().fi - (i + cq.front().fi))) {
              mn = abs(park[j].top().fi - (i + cq.front().fi));
              indx = j;
            }
          }
        }

        if (flg && !bkflg) {
          park[indx].push(cq.front());
          park[indx].top().fi += i;
          cq.pop();
        }

        if (bkflg)
          break;
      }
    }

    for (int i = 0; i < ans.size() - 1; i++) {
      cout << ans[i] << " ";
    }
    cout << ans.back() << endl;
  }
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second

int main() {
  int n, m;
  while (cin >> m >> n, m || n) {
    vector<pii> query;
    vector<int> ans;
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      query.push_back(pii(t, i + 1));
    }

    queue<pii> cq;
    vector<stack<pii>> park;
    for (int i = 0; i < m; i++)
      park.push_back(stack<pii>());
    for (int i = 0; i < 20000; i++) {
      if (i % 10 == 0 && i / 10 < query.size()) {
        cq.push(query[i / 10]);
      }
      // out
      for (int j = 0; j < m; j++) {
        while (!park[j].empty() && park[j].top().fi <= i) {
          ans.push_back(park[j].top().se);
          park[j].pop();
        }
      }
      // in
      if (!cq.empty()) {
        bool flg = true;
        bool bkflg = true;
        for (int j = 0; j < m && flg; j++) {
          if (park[j].empty()) {
            park[j].push(cq.front());
            park[j].top().fi += i;
            cq.pop();
            flg = false;
          }
          bkflg &= park[j].size() == 2;
        }

        int mn = 20000, indx = -1;
        for (int j = 0; j < m && flg && !bkflg; j++) {
          if (park[j].size() == 1) {
            if (mn > abs(park[j].top().fi - (i + cq.front().fi))) {
              mn = abs(park[j].top().fi - (i + cq.front().fi));
              indx = j;
            }
          }
        }

        if (flg && !bkflg) {
          park[indx].push(cq.front());
          park[indx].top().fi += i;
          cq.pop();
        }

        if (bkflg)
          break;
      }
    }

    for (int i = 0; i < ans.size() - 1; i++) {
      cout << ans[i] << " ";
    }
    cout << ans.back() << endl;
  }
}