Problem p01087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using i64 = long long int;
using ui64 = unsigned long long int;

////////

using pic = pair<int, char>;
#define mp make_pair
using psic = pair<stack<int>, char>;

pic parse(string &s) { return make_pair(s.length() - 1, s[s.length() - 1]); }

void calc(deque<psic> &q, int lv) {
  while (q.size() > lv) {
    psic p = q.back();

    q.pop_back();
    // cerr << q.size() << " " << p.second << endl;
    if (p.second == '+') {
      int ret = 0;
      while (!p.first.empty()) {
        ret += p.first.top();
        p.first.pop();
      }
      q.back().first.push(ret);
    } else if (p.second == '*') {
      int ret = 1;
      while (!p.first.empty()) {
        ret *= p.first.top();
        p.first.pop();
      }
      q.back().first.push(ret);
    } else
      assert(false);
  }
}

int solve(deque<psic> &q) {
  calc(q, 1);
  psic p = q[0];
  if (p.second == '+') {
    int ret = 0;
    while (!p.first.empty()) {
      ret += p.first.top();
      p.first.pop();
    }
    return ret;
  } else if (p.second == '*') {
    int ret = 1;
    while (!p.first.empty()) {
      ret *= p.first.top();
      p.first.pop();
    }
    return ret;
  } else
    assert(false);
}

int main() {
  int n;
  while (cin >> n, n) {
    int curlv = 0;
    deque<psic> q;
    q.push_back(mp(stack<int>(), '+'));

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;

      pic ps = parse(s);
      cerr << ps.first << " " << ps.second << endl;
      if (ps.first < curlv) {
        calc(q, ps.first + 1);
        curlv = ps.first;
        if (ps.second == '+' || ps.second == '*') {
          psic ne;
          ne.second = ps.second;
          q.push_back(ne);
          curlv++;
        } else {
          q.back().first.push(((int)(ps.second - '0')));
        }
      } else if (ps.first == curlv) {
        if (ps.second == '+' || ps.second == '*') {
          psic ne;
          ne.second = ps.second;
          q.push_back(ne);
          curlv++;
        } else {
          q.back().first.push(((int)(ps.second - '0')));
        }
      } else
        assert(false);
    }
    cout << solve(q) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using i64 = long long int;
using ui64 = unsigned long long int;

////////

using pic = pair<int, char>;
#define mp make_pair
using psic = pair<stack<int>, char>;

pic parse(string &s) { return make_pair(s.length() - 1, s[s.length() - 1]); }

void calc(deque<psic> &q, int lv) {
  while (q.size() > lv) {
    psic p = q.back();

    q.pop_back();
    // cerr << q.size() << " " << p.second << endl;
    if (p.second == '+') {
      int ret = 0;
      while (!p.first.empty()) {
        ret += p.first.top();
        p.first.pop();
      }
      q.back().first.push(ret);
    } else if (p.second == '*') {
      int ret = 1;
      while (!p.first.empty()) {
        ret *= p.first.top();
        p.first.pop();
      }
      q.back().first.push(ret);
    } else
      assert(false);
  }
}

int solve(deque<psic> &q) {
  calc(q, 1);
  psic p = q[0];
  if (p.second == '+') {
    int ret = 0;
    while (!p.first.empty()) {
      ret += p.first.top();
      p.first.pop();
    }
    return ret;
  } else if (p.second == '*') {
    int ret = 1;
    while (!p.first.empty()) {
      ret *= p.first.top();
      p.first.pop();
    }
    return ret;
  } else
    assert(false);
}

int main() {
  int n;
  while (cin >> n, n) {
    int curlv = 0;
    deque<psic> q;
    q.push_back(mp(stack<int>(), '+'));

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;

      pic ps = parse(s);
      // cerr << ps.first << " " << ps.second << endl;
      if (ps.first < curlv) {
        calc(q, ps.first + 1);
        curlv = ps.first;
        if (ps.second == '+' || ps.second == '*') {
          psic ne;
          ne.second = ps.second;
          q.push_back(ne);
          curlv++;
        } else {
          q.back().first.push(((int)(ps.second - '0')));
        }
      } else if (ps.first == curlv) {
        if (ps.second == '+' || ps.second == '*') {
          psic ne;
          ne.second = ps.second;
          q.push_back(ne);
          curlv++;
        } else {
          q.back().first.push(((int)(ps.second - '0')));
        }
      } else
        assert(false);
    }
    cout << solve(q) << endl;
  }
}