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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

ifstream ifs("in.txt");

void inp(vector<pair<char, int>> &a) {
  while (1) {
    char c;
    cin >> c;
    if (c == '$')
      break;
    int x;
    cin >> x;
    a.push_back(make_pair(c, x));
  }
}

int main() {
  vector<pair<char, int>> a, b, c, res, ans;
  inp(a);
  inp(b);
  inp(c);
  REP(i, (int)a.size() - (int)b.size() + 1) {
    bool f = true;
    REP(j, b.size()) {
      if (a[i + j].first != b[j].first) {
        f = false;
        break;
      }
      if (j == 0 || j == b.size() - 1) {
        if (a[i + j].second < b[j].second) {
          f = false;
          break;
        }
      } else {
        if (a[i + j].second != b[j].second) {
          f = false;
          break;
        }
      }
    }
    if (f) {
      REP(j, i) res.push_back(a[j]);
      if (b.size() > 1) {
        a[i].second -= b[0].second;
        res.push_back(a[i]);
      }
      REP(j, c.size()) res.push_back(c[j]);
      a[i + b.size() - 1].second -= b[b.size() - 1].second;
      res.push_back(a[i + b.size() - 1]);
      FOR(j, i + b.size(), a.size() - 1) res.push_back(a[j]);
      break;
    }
  }
  if (res.empty()) {
    REP(i, a.size()) { printf("%c %d ", a[i].first, a[i].second); }
    printf("$\\n");
  } else {
    REP(i, res.size()) {
      if (!ans.empty() && ans[ans.size() - 1].first == res[i].first) {
        ans[ans.size() - 1].second += res[i].second;
      } else if (res[i].second > 0) {
        ans.push_back(res[i]);
      }
    }
    REP(i, ans.size()) { printf("%c %d ", ans[i].first, ans[i].second); }
    printf("$\\n");
  }
  return 0;
}

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


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

void inp(vector<pair<char, int>> &a) {
  while (1) {
    char c;
    cin >> c;
    if (c == '$')
      break;
    int x;
    cin >> x;
    a.push_back(make_pair(c, x));
  }
}

int main() {
  vector<pair<char, int>> a, b, c, res, ans;
  inp(a);
  inp(b);
  inp(c);
  REP(i, a.size() - b.size() + 1) {
    bool f = true;
    REP(j, b.size()) {
      if (a[i + j].first != b[j].first) {
        f = false;
        break;
      }
      if (j == 0 || j == b.size() - 1) {
        if (a[i + j].second < b[j].second) {
          f = false;
          break;
        }
      } else {
        if (a[i + j].second != b[j].second) {
          f = false;
          break;
        }
      }
    }
    if (f) {
      REP(j, i) res.push_back(a[j]);
      a[i].second -= b[0].second;
      res.push_back(a[i]);
      REP(j, c.size()) res.push_back(c[j]);
      if (b.size() > 1) {
        a[i + b.size() - 1].second -= b[b.size() - 1].second;
        res.push_back(a[i + b.size() - 1]);
      }
      FOR(j, i + b.size(), a.size() - 1) res.push_back(a[j]);
    }
  }
  if (res.empty()) {
    REP(i, a.size()) { printf("%c %d ", a[i].first, a[i].second); }
    printf("$\\n");
  } else {
    REP(i, res.size()) {
      if (!ans.empty() && ans[ans.size() - 1].first == res[i].first) {
        ans[ans.size() - 1].second += res[i].second;
      } else if (res[i].second > 0) {
        ans.push_back(res[i]);
      }
    }
    REP(i, ans.size()) { printf("%c %d ", ans[i].first, ans[i].second); }
    printf("$\\n");
  }
  return 0;
}