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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, w, d;
  for (; cin >> n >> w >> d, w;) {
    vector<pair<int, int>> cake;
    cake.push_back(make_pair(w, d));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      pair<int, int> tmp = cake[p];
      cake.erase(cake.begin() + p);
      int size = s % (tmp.first + tmp.second);
      if (size < tmp.first) {
        cake.push_back(make_pair(min(size, tmp.first - size), tmp.second));
        cake.push_back(make_pair(max(size, tmp.first - size), tmp.second));
      } else {
        cake.push_back(
            make_pair(tmp.first, min((size - tmp.first),
                                     tmp.second - (size - tmp.first))));
        cake.push_back(
            make_pair(tmp.first, max((size - tmp.first),
                                     tmp.second - (size - tmp.first))));
      }
    }
    //      sort(cake.begin(),cake.end(),isgreater);
    for (int i = 0; i < cake.size() - 1; i++)
      for (int j = i + 1; j < cake.size(); j++) {
        if (cake[i].first * cake[i].second > cake[j].first * cake[j].second)
          swap(cake[i], cake[j]);
      }
    for (int i = 0; i < cake.size(); i++) {
      cout << cake[i].first * cake[i].second;
      if (i == cake.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}

Error: 0
out:5 6 7
in:5 {2,4}
Now: 10 20
out:5 4 1
in:{1,4} 4
Now: 10 4 16
out:5 2 2
in:{2,3} 2
Now: 4 16 4 6
out:4 1 1
in:{1,3} 1
Now: 1 3
out:3 1 1
in:{1,2} 1
Now: 1 1 2
out:2 1 1
in:{1,1} 1
Now: 1 1 1 1


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, w, d;
  for (; cin >> n >> w >> d, w;) {
    vector<pair<int, int>> cake;
    cake.push_back(make_pair(w, d));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;

      p--;
      pair<int, int> tmp = cake[p];
      cake.erase(cake.begin() + p);
      int size = s % (tmp.first + tmp.second);
      cerr << "out:" << tmp.first << " " << tmp.second << " " << size << endl;
      if (size < tmp.first) {
        cerr << "in:{" << size << "," << tmp.first - size << "} " << tmp.second
             << "" << endl;
        cake.push_back(make_pair(min(size, tmp.first - size), tmp.second));
        cake.push_back(make_pair(max(size, tmp.first - size), tmp.second));
      } else {
        cerr << "in:" << tmp.first << " {" << (size - tmp.first) << ","
             << tmp.second - (size - tmp.first) << "}" << endl;
        cake.push_back(
            make_pair(tmp.first, min((size - tmp.first),
                                     tmp.second - (size - tmp.first))));
        cake.push_back(
            make_pair(tmp.first, max((size - tmp.first),
                                     tmp.second - (size - tmp.first))));
      }
      cerr << "Now: ";
      for (int i = 0; i < cake.size(); i++) {
        cerr << cake[i].first * cake[i].second;
        if (i == cake.size() - 1)
          cerr << endl;
        else
          cerr << " ";
      }
    }
    sort(cake.begin(), cake.end());
    for (int i = 0; i < cake.size(); i++) {
      cout << cake[i].first * cake[i].second;
      if (i == cake.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}