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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> P;

#define MAX_N 101

int n, w, d;
int p[MAX_N], s[MAX_N];

PII cake[MAX_N];

int main() {

  while (true) {
    cin >> n >> w >> d;
    if (n == 0 && w == 0 && d == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> p[i] >> s[i];
    }

    int M = 1;

    cake[1].first = w;
    cake[1].second = d;

    for (int num = 0; num < n; num++) {
      // cout << endl << num << " " << p[num] << endl;
      M++;
      int ww = cake[p[num]].first;
      int dd = cake[p[num]].second;
      int ss = s[num];
      for (int i = p[num] + 1; i < M; i++) {
        cake[i - 1].first = cake[i].first;
        cake[i - 1].second = cake[i].second;
      }
      // cout << ww << " " << dd << endl;
      ss = ss % ((ww + dd) * 2);

      if (ss - ww < 0) {
        int ds = ww - ss;
        cake[M - 1].first = min(ds, ss);
        cake[M - 1].second = dd;
        cake[M].first = max(ds, ss);
        cake[M].second = dd;
      } else if (ss - ww - dd < 0) {
        ss -= ww;
        int ds = dd - ss;
        cake[M - 1].second = min(ds, ss);
        cake[M - 1].first = ww;
        cake[M].second = max(ds, ss);
        cake[M].first = ww;
      } else if (ss - 2 * ww - dd < 0) {
        ss -= (ww + dd);
        int ds = ww - ss;
        cake[M - 1].first = min(ds, ss);
        cake[M - 1].second = dd;
        cake[M].first = max(ds, ss);
        cake[M].second = dd;
      } else if (ss - ww - dd - ww - dd < 0) {
        ss -= (ww + dd + ww);
        int ds = dd - ss;
        cake[M - 1].second = min(ds, ss);
        cake[M - 1].first = ww;
        cake[M].second = max(ds, ss);
        cake[M].first = ww;
      }
    }

    int ans[MAX_N + 1];
    for (int i = 1; i <= M; i++) {
      ans[i - 1] = cake[i].first * cake[i].second;
    }

    sort(ans, ans + M);

    for (int i = 0; i < M; i++) {
      cout << ans[i];
      if (i < n + 1 - 1)
        cout << " ";
    }

    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> P;

#define MAX_N 101

int n, w, d;
int p[MAX_N], s[MAX_N];

PII cake[MAX_N];

int main() {

  while (true) {
    cin >> n >> w >> d;
    if (w == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> p[i] >> s[i];
    }

    int M = 1;

    cake[1].first = w;
    cake[1].second = d;

    for (int num = 0; num < n; num++) {
      M++;
      int ww = cake[p[num]].first;
      int dd = cake[p[num]].second;

      int ss = s[num];
      ss %= ((ww + dd) * 2);
      if (ss - ww < 0) {
        int ds = ww - ss;
        cake[p[num]].first = min(ds, ss);
        cake[p[num]].second = dd;
        cake[M].first = max(ds, ss);
        cake[M].second = dd;
      } else if (ss - ww - dd < 0) {
        ss -= ww;
        int ds = dd - ss;
        cake[p[num]].second = min(ds, ss);
        cake[p[num]].first = ww;
        cake[M].second = max(ds, ss);
        cake[M].first = ww;
      } else if (ss - 2 * ww - dd < 0) {
        ss -= (ww + dd);
        int ds = ww - ss;
        cake[p[num]].first = min(ds, ss);
        cake[p[num]].second = dd;
        cake[M].first = max(ds, ss);
        cake[M].second = dd;
      } else {
        ss -= (ww + dd + ww);
        int ds = dd - ss;
        cake[p[num]].second = min(ds, ss);
        cake[p[num]].first = ww;
        cake[M].second = max(ds, ss);
        cake[M].first = ww;
      }
    }

    vector<int> ans;
    for (int i = 1; i <= n + 1; i++) {
      ans.push_back(cake[i].first * cake[i].second);
    }

    sort(ans.begin(), ans.end());

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

    cout << endl;
  }

  return 0;
}