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

#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
using P = pair<char, int>;

int main() {
  vector<P> A, B, C;
  char c;
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    A.emplace_back(c, l);
  }
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    B.emplace_back(c, l);
  }
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    C.emplace_back(c, l);
  }
  vector<P> ans;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].first == B.front().first && A[i].second >= B.front().second) {
      bool flag = true;
      if (i + B.size() <= A.size()) {
        for (int d = 1; d < B.size(); d++) {
          if (A[i + d].first != B[d].first) {
            flag = false;
            break;
          }
          if (d != B.size() - 1) {
            if (A[i + d].second != B[d].second) {
              flag = false;
              break;
            }
          } else {
            if (A[i + d].second < B[d].second) {
              flag = false;
              break;
            }
          }
        }
      } else {
        continue;
      }
      if (flag) {
        // cout<<i<<endl;
        if (B.size() == 1) {
          for (int j = 0; j < i; j++)
            ans.push_back(A[j]);
          for (auto p : C)
            ans.push_back(p);
          if (A[i].second > B[0].second) {
            ans.emplace_back(B[0].first, A[i].second - B[0].second);
          }
          for (int j = i + 1; j < A.size(); j++)
            ans.push_back(A[j]);
        } else {
          for (int j = 0; j < i; j++)
            ans.push_back(A[j]);
          if (A[i].second > B[0].second) {
            ans.emplace_back(B[0].first, A[i].second - B[0].second);
          }
          for (auto p : C)
            ans.push_back(p);
          if (A[i + B.size() - 1].second > B.back().second) {
            ans.emplace_back(B.back().first,
                             A[i + B.size() - 1].second - B.back().second);
          }
          for (int j = i + B.size(); j < A.size(); j++)
            ans.push_back(A[j]);
        }

        for (int j = ans.size() - 2; j >= 0; j--) {
          if (ans[j].first == ans[j + 1].first) {
            ans[j].second += ans[j + 1].second;
            ans[j + 1].second = 0;
          }
        }
        for (auto p : ans) {
          char c;
          int l;
          tie(c, l) = p;
          if (l)
            cout << c << ' ' << l << ' ';
        }
        cout << '$' << endl;
        /*
        {
            bool b=true;
            for(int i=0;i<A.size();i++){
                if(A[i]!=B[i]){
                    cout<<i<<' '<<A[i].first<<' '<<A[i].second<<'
        '<<B[i].first<<' '<<B[i].second<<endl;
                }
            }
            cout<<b<<endl;
        }*/

        // cout<<A.size()<<endl;
        // cout<<B.size()<<endl;
        // cout<<C.size()<<endl;
        // cout<<ans.size()<<endl;

        return 0;
      }
    }
  }
  for (auto p : A) {
    char c;
    int l;
    tie(c, l) = p;
    cout << c << ' ' << l << ' ';
  }
  cout << '$' << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
using P = pair<char, int>;

int main() {
  vector<P> A, B, C;
  char c;
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    A.emplace_back(c, l);
  }
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    B.emplace_back(c, l);
  }
  while (cin >> c, c != '$') {
    int l;
    cin >> l;
    C.emplace_back(c, l);
  }
  vector<P> ans;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].first == B.front().first && A[i].second >= B.front().second) {
      bool flag = true;
      if (i + B.size() <= A.size()) {
        for (int d = 1; d < B.size(); d++) {
          if (A[i + d].first != B[d].first) {
            flag = false;
            break;
          }
          if (d == B.size() - 2) {
            if (A[i + d].second != B[d].second) {
              flag = false;
              break;
            }
          } else {
            if (A[i + d].second < B[d].second) {
              flag = false;
              break;
            }
          }
        }
      } else {
        continue;
      }
      if (flag) {
        for (int j = 0; j < i; j++) {
          ans.push_back(A[j]);
        }
        if (A[i].second != B[0].second) {
          ans.emplace_back(A[i].first, A[i].second - B[0].second);
        }
        for (int j = 0; j < C.size(); j++) {
          if (j == 0 && ans.back().first == C.front().first) {
            ans.back().second += C.front().second;
          } else {
            ans.push_back(C[j]);
          }
        }

        if (A[i + B.size() - 1].second != B.back().second) {
          if (ans.back().first == B.back().first) {
            ans.back().second += A[i + B.size() - 1].second - B.back().second;
          } else {
            ans.emplace_back(B.back().first,
                             A[i + B.size() - 1].second - B.back().second);
          }
        }
        for (int j = i + B.size(); j < A.size(); j++) {
          ans.push_back(A[j]);
        }
        for (auto p : ans) {
          char c;
          int l;
          tie(c, l) = p;
          cout << c << ' ' << l << ' ';
        }
        cout << '$' << endl;
        return 0;
      }
    }
  }
  for (auto p : A) {
    char c;
    int l;
    tie(c, l) = p;
    cout << c << ' ' << l << ' ';
  }
  cout << '$' << endl;
  return 0;
}