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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

class Tpair {
public:
  int first, second;
  Tpair(int x, int y) {
    first = x;
    second = y;
  }
  bool operator<(const Tpair &a) const {
    return (first) * (second) < (a.first) * (a.second);
  }
};

typedef Tpair P;

int main() {
  int i, j, k, l, m, n;
  int w, d, s, p;

  while (cin >> n >> w >> d) {
    vector<P> cakes;
    cakes.clear();
    if (n == 0 && w == 0 && d == 0)
      break;
    cakes.push_back(P(w, d));
    rep(i, n) {
      cin >> p >> s;
      int ww = cakes[p - 1].first, dd = cakes[p - 1].second;
      cakes.erase(cakes.begin() + p - 1);

      s = s % (2 * ww + 2 * dd);
      vector<P> subcakes;
      subcakes.clear();
      if (s < ww) {
        subcakes.push_back(P(s, dd));
        subcakes.push_back(P(ww - s, dd));
      } else if (s < ww + dd) {
        s = s - ww;
        subcakes.push_back(P(ww, s));
        subcakes.push_back(P(ww, dd - s));
      } else if (s < 2 * ww + dd) {
        s -= ww + dd;
        subcakes.push_back(P(s, dd));
        subcakes.push_back(P(ww - s, dd));
      } else {
        s -= 2 * ww + dd;
        subcakes.push_back(P(ww, s));
        subcakes.push_back(P(ww, dd - s));
      }
      sort(subcakes.begin(), subcakes.end());
      cakes.push_back(subcakes[0]);
      cakes.push_back(subcakes[1]);
    }
    sort(cakes.begin(), cakes.end());
    rep(i, cakes.size() - 1) cout << cakes[i].first * cakes[i].second << " ";
    cout << cakes[i].first * cakes[i].second << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

class Tpair {
public:
  int first, second;
  Tpair(int x, int y) {
    first = x;
    second = y;
  }
  bool operator<(const Tpair &a) const {
    return (first) * (second) < (a.first) * (a.second);
  }
};

typedef Tpair P;

int main() {
  int i, j, k, l, m, n;
  int w, d, s, p;

  while (cin >> n >> w >> d) {
    vector<P> cakes;
    cakes.clear();
    if (n == 0 && w == 0 && d == 0)
      break;
    cakes.push_back(P(w, d));
    rep(i, n) {
      cin >> p >> s;
      int ww = cakes[p - 1].first, dd = cakes[p - 1].second;
      cakes.erase(cakes.begin() + p - 1);

      s = s % (2 * ww + 2 * dd);
      vector<P> subcakes;
      subcakes.clear();
      if (s < ww) {
        subcakes.push_back(P(s, dd));
        subcakes.push_back(P(ww - s, dd));
      } else if (s < ww + dd) {
        s = s - ww;
        subcakes.push_back(P(ww, s));
        subcakes.push_back(P(ww, dd - s));
      } else if (s < 2 * ww + dd) {
        s -= ww + dd;
        subcakes.push_back(P(s, dd));
        subcakes.push_back(P(ww - s, dd));
      } else {
        s -= 2 * ww + dd;
        subcakes.push_back(P(ww, s));
        subcakes.push_back(P(ww, dd - s));
      }
      sort(subcakes.begin(), subcakes.end());
      cakes.push_back(subcakes[1]);
      cakes.push_back(subcakes[0]);
    }
    sort(cakes.begin(), cakes.end());
    rep(i, cakes.size() - 1) cout << cakes[i].first * cakes[i].second << " ";
    cout << cakes[i].first * cakes[i].second << endl;
  }
  return 0;
}