Problem p02856 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LARB 1
#define RARB 30000
#define inf 1e18
#define zeros(x) ((x ^ (x - 1)) & x)
#define endl '\n'
#define eps 1e-9
#define mod 1000000007
#define NMAX 300

using namespace std;

typedef long long ll;
typedef long double ld;

ll d, c, tot, sum, n;
vector<pair<ll, ll>> Init;

ll norm(ll &sum) {
  ll nr = 0, steps = 0;
  while (sum) {
    nr += sum % 10;
    steps++;
    sum /= 10;
  }
  ll ans = steps - 1;
  if (nr >= 10)
    ans += norm(nr);
  sum = nr;
  return ans;
}

ll compute(pair<ll, ll> pr, ll &sum);

ll group(pair<ll, ll> pr, ll &sum, ll grsize, ll nxt) {
  if (pr.second % grsize == 0)
    return (grsize - 1) * (pr.second / grsize) + pr.second / grsize +
           compute({nxt, pr.second / grsize}, sum);
  else {
    ll def = 0;
    if (sum == 0) {
      def = pr.second % grsize - 1;
    } else {
      def = pr.second % grsize;
    }

    sum += (pr.second % grsize) * pr.first;
    if (sum > 9)
      def += norm(sum);

    return (grsize - 1) * (pr.second / grsize) + pr.second / grsize + def +
           compute({nxt, pr.second / grsize}, sum);
  }
}

ll compute(pair<ll, ll> pr, ll &sum) {
  if (pr.second == 0)
    return 0;

  ll base = pr.second - (sum == 0);
  if (pr.first == 0)
    return base;

  if (pr.first == 1)
    return group(pr, sum, 10, 1);

  if (pr.first == 2)
    return group(pr, sum, 5, 1);

  if (pr.first == 3)
    return group(pr, sum, 4, 3);

  if (pr.first == 4)
    return group(pr, sum, 3, 3);

  if (pr.first == 5)
    return group(pr, sum, 2, 1);

  if (pr.first == 6)
    return group(pr, sum, 2, 3);

  if (pr.first == 7)
    return group(pr, sum, 2, 5);

  if (pr.first == 8)
    return group(pr, sum, 2, 7);

  if (pr.first == 9)
    return group(pr, sum, 2, 9);

  assert(false);
}

int main() {
  freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> d >> c;
    Init.push_back({d, c});
  }

  ll sum = 0;
  for (auto i : Init) {
    tot += compute(i, sum);
  }
  cout << tot;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LARB 1
#define RARB 30000
#define inf 1e18
#define zeros(x) ((x ^ (x - 1)) & x)
#define endl '\n'
#define eps 1e-9
#define mod 1000000007
#define NMAX 300

using namespace std;

typedef long long ll;
typedef long double ld;

ll d, c, tot, sum, n;
vector<pair<ll, ll>> Init;

ll norm(ll &sum) {
  ll nr = 0, steps = 0;
  while (sum) {
    nr += sum % 10;
    steps++;
    sum /= 10;
  }
  ll ans = steps - 1;
  if (nr >= 10)
    ans += norm(nr);
  sum = nr;
  return ans;
}

ll compute(pair<ll, ll> pr, ll &sum);

ll group(pair<ll, ll> pr, ll &sum, ll grsize, ll nxt) {
  if (pr.second % grsize == 0)
    return (grsize - 1) * (pr.second / grsize) + pr.second / grsize +
           compute({nxt, pr.second / grsize}, sum);
  else {
    ll def = 0;
    if (sum == 0) {
      def = pr.second % grsize - 1;
    } else {
      def = pr.second % grsize;
    }

    sum += (pr.second % grsize) * pr.first;
    if (sum > 9)
      def += norm(sum);

    return (grsize - 1) * (pr.second / grsize) + pr.second / grsize + def +
           compute({nxt, pr.second / grsize}, sum);
  }
}

ll compute(pair<ll, ll> pr, ll &sum) {
  if (pr.second == 0)
    return 0;

  ll base = pr.second - (sum == 0);
  if (pr.first == 0)
    return base;

  if (pr.first == 1)
    return group(pr, sum, 10, 1);

  if (pr.first == 2)
    return group(pr, sum, 5, 1);

  if (pr.first == 3)
    return group(pr, sum, 4, 3);

  if (pr.first == 4)
    return group(pr, sum, 3, 3);

  if (pr.first == 5)
    return group(pr, sum, 2, 1);

  if (pr.first == 6)
    return group(pr, sum, 2, 3);

  if (pr.first == 7)
    return group(pr, sum, 2, 5);

  if (pr.first == 8)
    return group(pr, sum, 2, 7);

  if (pr.first == 9)
    return group(pr, sum, 2, 9);

  assert(false);
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> d >> c;
    Init.push_back({d, c});
  }

  ll sum = 0;
  for (auto i : Init) {
    tot += compute(i, sum);
  }
  cout << tot;
  return 0;
}