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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vs(n);
    for (int i = 0; i < n; i++)
      cin >> vs[i];
    vector<pair<int, int>> sp;
    vector<pair<int, char>> sta;
    if (n == 1) {
      cout << vs[0] << endl;
    } else {
      for (int i = 0; i < n; i++) {
        string str = vs[i];
        if (!sta.empty() && (str.size() != (sta[sta.size() - 1].F + 1))) {
          if (sta[sta.size() - 1].S == '+') {
            int sum = 0;
            while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
              int num = sp[sp.size() - 1].F;
              sp.pop_back();
              sum += num;
            }
            // cout<<sum<<endl;
            sp.pb(mp(sum, sta[sta.size() - 1].F));
            sta.pop_back();
          } else if (sta[sta.size() - 1].S == '*') {
            int sum = 1;
            while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
              int num = sp[sp.size() - 1].F;
              sp.pop_back();
              sum *= num;
            }
            // cout<<sum<<endl;
            sp.pb(mp(sum, sta[sta.size() - 1].F));
            sta.pop_back();
          }
          i--;
        } else if (str[str.size() - 1] == '+') {
          sta.pb(mp(str.size(), '+'));
        } else if (str[str.size() - 1] == '*') {
          sta.pb(mp(str.size(), '*'));
        } else {
          if ((sta.empty()) || (str.size() == (sta[sta.size() - 1].F + 1))) {
            char ccc = str[str.size() - 1];
            int nnn = ccc - '0';
            sp.pb(mp(nnn, str.size()));
          }
        }
      }
      while (sta.size() != 1) {
        if (sta[sta.size() - 1].S == '+') {
          int sum = 0;
          while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
            int num = sp[sp.size() - 1].F;
            sp.pop_back();
            sum += num;
          }
          sp.pb(mp(sum, sta[sta.size() - 1].F));
          sta.pop_back();
        } else if (sta[sta.size() - 1].S == '*') {
          int sum = 1;
          while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
            int num = sp[sp.size() - 1].F;
            sp.pop_back();
            sum *= num;
          }
          sp.pb(mp(sum, sta[sta.size() - 1].F));
          sta.pop_back();
        }
      }
      int ans = 0;
      if (sta[0].S == '+') {
        for (int i = 0; i < sp.size(); i++) {
          ans += sp[i].F;
          // cout<<sp[i].F<<endl;
        }
      } else {
        ans = 1;
        for (int i = 0; i < sp.size(); i++) {
          ans *= sp[i].F;
          // cout<<sp[i].F<<endl;
        }
      }
      // cout<<sta.size()<<" "<<sp.size()<<endl;
      cout << ans << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> vs(n);
    for (int i = 0; i < n; i++)
      cin >> vs[i];
    vector<pair<int, int>> sp;
    vector<pair<int, char>> sta;
    for (int i = 0; i < n; i++) {
      string str = vs[i];
      if (!sta.empty() && (str.size() != (sta[sta.size() - 1].F + 1))) {
        if (sta[sta.size() - 1].S == '+') {
          int sum = 0;
          while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
            int num = sp[sp.size() - 1].F;
            sp.pop_back();
            sum += num;
          }
          // cout<<sum<<endl;
          sp.pb(mp(sum, sta[sta.size() - 1].F));
          sta.pop_back();
        } else if (sta[sta.size() - 1].S == '*') {
          int sum = 1;
          while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
            int num = sp[sp.size() - 1].F;
            sp.pop_back();
            sum *= num;
          }
          // cout<<sum<<endl;
          sp.pb(mp(sum, sta[sta.size() - 1].F));
          sta.pop_back();
        }
        i--;
      } else if (str[str.size() - 1] == '+') {
        sta.pb(mp(str.size(), '+'));
      } else if (str[str.size() - 1] == '*') {
        sta.pb(mp(str.size(), '*'));
      } else {
        if ((sta.empty()) || (str.size() == (sta[sta.size() - 1].F + 1))) {
          char ccc = str[str.size() - 1];
          int nnn = ccc - '0';
          sp.pb(mp(nnn, str.size()));
        }
      }
    }
    while (sta.size() != 1) {
      if (sta[sta.size() - 1].S == '+') {
        int sum = 0;
        while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
          int num = sp[sp.size() - 1].F;
          sp.pop_back();
          sum += num;
        }
        sp.pb(mp(sum, sta[sta.size() - 1].F));
        sta.pop_back();
      } else if (sta[sta.size() - 1].S == '*') {
        int sum = 1;
        while (sp[sp.size() - 1].S == sta[sta.size() - 1].F + 1) {
          int num = sp[sp.size() - 1].F;
          sp.pop_back();
          sum *= num;
        }
        sp.pb(mp(sum, sta[sta.size() - 1].F));
        sta.pop_back();
      }
    }
    int ans = 0;
    if (sta[0].S == '+') {
      for (int i = 0; i < sp.size(); i++) {
        ans += sp[i].F;
        // cout<<sp[i].F<<endl;
      }
    } else {
      ans = 1;
      for (int i = 0; i < sp.size(); i++) {
        ans *= sp[i].F;
        // cout<<sp[i].F<<endl;
      }
    }
    // cout<<sta.size()<<" "<<sp.size()<<endl;
    cout << ans << endl;
  }
  return 0;
}