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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s;
int x;
vector<pair<string, int>> a, b, c;
vector<pair<string, int>> shift(vector<pair<string, int>> v) {
  vector<pair<string, int>> w;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].second > 0)
      w.push_back(v[i]);
  }
  v = w;
  vector<pair<string, int>> ret;
  int sum = v[0].second;
  for (int i = 1; i <= v.size(); i++) {
    if (i == v.size() || v[i - 1].first != v[i].first) {
      ret.push_back(make_pair(v[i - 1].first, sum));
      sum = 0;
    }
    if (i != v.size())
      sum += v[i].second;
  }
  return ret;
}
int main() {
  while (cin >> s, s != "$")
    cin >> x, a.push_back(make_pair(s, x));
  while (cin >> s, s != "$")
    cin >> x, b.push_back(make_pair(s, x));
  while (cin >> s, s != "$")
    cin >> x, c.push_back(make_pair(s, x));
  for (int i = 0; i <= (int)(a.size() - b.size()); i++) {
    bool ok = true;
    for (int j = 0; j < b.size(); j++) {
      if (a[i + j].first != b[j].first)
        ok = false;
    }
    for (int j = 2; j < b.size(); j++) {
      if (a[i + j - 1].second != b[j - 1].second)
        ok = false;
    }
    if (a[i].second < b[0].second)
      ok = false;
    if (a[i + b.size() - 1].second < b[b.size() - 1].second)
      ok = false;
    if (ok) {
      vector<pair<string, int>> d(a.begin(), a.begin() + i);
      if (b.size() != 1)
        d.push_back(make_pair(a[i].first, a[i].second - b[0].second));
      d.insert(d.end(), c.begin(), c.end());
      d.push_back(
          make_pair(a[i + b.size() - 1].first,
                    a[i + b.size() - 1].second - b[b.size() - 1].second));
      d.insert(d.end(), a.begin() + i + b.size(), a.end());
      a = shift(d);
      break;
    }
  }
  for (int i = 0; i < a.size(); i++)
    cout << a[i].first << ' ' << a[i].second << ' ';
  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 <string>
#include <vector>
using namespace std;
string s;
int x;
vector<pair<string, int>> a, b, c;
vector<pair<string, int>> shift(vector<pair<string, int>> v) {
  vector<pair<string, int>> ret;
  int p = 0, sum = v[0].second;
  for (int i = 1; i <= v.size(); i++) {
    if (i == v.size() || v[i - 1].first != v[i].first) {
      if (sum > 0)
        ret.push_back(make_pair(v[i - 1].first, sum));
      sum = 0;
    }
    if (i != v.size())
      sum += v[i].second;
  }
  return ret;
}
int main() {
  while (cin >> s, s != "$")
    cin >> x, a.push_back(make_pair(s, x));
  while (cin >> s, s != "$")
    cin >> x, b.push_back(make_pair(s, x));
  while (cin >> s, s != "$")
    cin >> x, c.push_back(make_pair(s, x));
  for (int i = 0; i <= a.size() - b.size(); i++) {
    bool ok = true;
    for (int j = 0; j < b.size(); j++) {
      if (a[i + j].first != b[j].first)
        ok = false;
    }
    for (int j = 2; j < b.size(); j++) {
      if (a[i + j - 1].second != b[j - 1].second)
        ok = false;
    }
    if (a[i].second < b[0].second)
      ok = false;
    if (a[i + b.size() - 1].second < b[b.size() - 1].second)
      ok = false;
    if (ok) {
      vector<pair<string, int>> d(a.begin(), a.begin() + i);
      d.push_back(make_pair(a[i].first, a[i].second - b[0].second));
      d.insert(d.end(), c.begin(), c.end());
      d.push_back(
          make_pair(a[i + b.size() - 1].first,
                    a[i + b.size() - 1].second - b[b.size() - 1].second));
      d.insert(d.end(), a.begin() + i + b.size(), a.end());
      a = shift(d);
      break;
    }
  }
  for (int i = 0; i < a.size(); i++)
    cout << a[i].first << ' ' << a[i].second << ' ';
  cout << "$" << endl;
  return 0;
}