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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
typedef pair<char, ll> pci;
vector<pci> a, b, c;

int main() {
  char let;
  ll n;
  while (cin >> let && let != '$') {
    cin >> n;
    a.emplace_back(let, n);
  }
  while (cin >> let && let != '$') {
    cin >> n;
    b.emplace_back(let, n);
  }
  while (cin >> let && let != '$') {
    cin >> n;
    c.emplace_back(let, n);
  }
  int sza = a.size(), szb = b.size(), szc = c.size();
  vector<pci> res;

  REP(i, sza - szb + 1) {
    if (a[i].first == b[0].first && a[i].second >= b[0].second) {
      bool f = true;
      int j;
      for (j = 1; j < szb; j++) {
        if (j == szb - 1) {
          if (b[j].first != a[i + j].first || b[j].second > a[i + j].second)
            f = false;
        } else {
          if (b[j].first != a[i + j].first || b[j].second != a[i + j].second)
            f = false;
        }
      }
      if (f) {
        if (szb <= 1) {
          REP(k, i) res.emplace_back(a[k]);
          REP(k, c.size()) res.emplace_back(c[k]);
          if (a[i].second > b[0].second)
            res.emplace_back(a[i].first, a[i].second - b[0].second);
          FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
          break;
        } else {
          REP(k, i) res.emplace_back(a[k]);
          if (a[i].second > b[0].second)
            res.emplace_back(a[i].first, a[i].second - b[0].second);
          REP(k, szc) res.emplace_back(c[k]);
          if (a[i + szb - 1].second > b.back().second)
            res.emplace_back(b.back().first,
                             a[i + b.size() - 1].second - b.back().second);
          FOR(k, i + szb, sza) res.emplace_back(a[k]);
          break;
        }
      }
    }
  }
  vector<pci> ans;
  if (res.size() == 0)
    ans = a;
  else {
    ans.emplace_back(res[0]);
    FOR(i, 1, res.size()) {
      if (res[i].first == ans.back().first)
        ans.back().second += res[i].second;
      else
        ans.emplace_back(res[i]);
    }
  }
  REP(i, ans.size()) cout << ans[i].first << " " << ans[i].second << " ";
  cout << "$\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
typedef pair<char, int> pci;

int main() {
  vector<pci> a, b, c;
  char let;
  int n;
  while (cin >> let && let != '$') {
    cin >> n;
    a.emplace_back(let, n);
  }
  while (cin >> let && let != '$') {
    cin >> n;
    b.emplace_back(let, n);
  }
  while (cin >> let && let != '$') {
    cin >> n;
    c.emplace_back(let, n);
  }
  vector<pci> res;
  REP(i, a.size() - b.size() + 1) {
    if (a[i].first == b[0].first && a[i].second >= b[0].second) {
      bool f = true;
      int j;
      for (j = 1; j < b.size(); j++) {
        if (j == b.size() - 1) {
          if (b[j].first != a[i + j].first || b[j].second >= a[i + j].second)
            f = false;
        } else {
          if (b[j].first != a[i + j].first || b[j].second != a[i + j].second)
            f = false;
        }
      }
      if (f) {

        REP(k, i) res.emplace_back(a[k]);
        if (a[i].second > b[i].second)
          res.emplace_back(a[i].first, a[i].second - b[i].second);
        REP(k, c.size()) res.emplace_back(c[k]);
        if (a[i + b.size() - 1].second > b.back().second)
          res.emplace_back(b.back().first,
                           a[i + b.size() - 1].second - b.back().second);
        FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
        i = INF;
      }
    }
  }
  vector<pci> ans;
  if (res.size() == 0)
    ans = a;
  else {
    ans.emplace_back(res[0]);
    FOR(i, 1, res.size()) {
      if (res[i].first == ans.back().first)
        ans.back().second += res[i].second;
      else
        ans.emplace_back(res[i]);
    }
  }
  REP(i, ans.size())
      cout << ans[i].first << " " << ans[i].second
           << (i == ans.size() - 1 ? " $\\n" : " ");
  return 0;
}