Problem p02856 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

// #define DEBUG

void pushAsDigits(vec<int> &v, ll num) {
  if (num == 0) {
    v.push_back(0);
    return;
  }
  // num > 0
  ll tens = 1;
  while (tens <= num) {
    tens *= 10;
  }
  // tens > num
  tens /= 10;
  // tens <= num
  while (tens > 0) {
    v.push_back(num / tens);
    num %= tens;
    tens /= 10;
  }
}

struct Result {
  vector<int> seq;
  ll cnt;
};

Result compress(int dig, ll num) {
  if (num == 0) {
    return Result{vector<int>{}, 0};
  }
  if (num < 2) {
    assert(num == 1);
    return Result{vector<int>{dig}, 0};
  }
  if (num % 2 == 1) {
    auto res = compress(dig, num - 1);
    res.seq.push_back(dig);
    return res;
  }

  if (dig * 2 < 10) {
    auto res = compress(dig * 2, num / 2);
    return Result{res.seq, res.cnt + num / 2};
  } else {
    auto res1 = compress((dig * 2) / 10, num / 2);
    auto res2 = compress((dig * 2) % 10, num / 2);
    res1.seq.insert(res1.seq.end(), res2.seq.begin(), res2.seq.end());
    return Result{res1.seq, res1.cnt + res2.cnt + num / 2};
  }
}

void solve(long long M, std::vector<long long> d, std::vector<long long> c) {
  ll cnt = 0;
  vec<int> people;

  // 各数の出現回数をしらべる
  vector<int> appr(10, 0);
  for (int i = 0; i < M; i++) {
    appr[d[i]] += c[i];
  }

  for (int i = 0; i < 10; i++) {
    if (appr[i] == 0) {
      continue;
    }
    auto res = compress(i, appr[i]);
    cnt += res.cnt;
    people.insert(people.end(), res.seq.begin(), res.seq.end());
  }

  // for(int i=0; i < M; i++){
  //     // for(int j=0; j < c[i]; j++){
  //     //     people.push_back(d[i]);
  //     // }
  //     auto res = compress(d[i], c[i]);
  //     #ifdef DEBUG
  //     printf("*START: hoge\n");
  //     for(auto iter: res.seq){
  //         cout << iter << " ";
  //     }
  //     cout << endl;
  //     cout << res.cnt << endl;
  //     printf("*END: hoge\n");
  //     #endif
  //     cnt += res.cnt;
  //     people.insert(people.end(), res.seq.begin(), res.seq.end());
  // }

  while (people.size() > 1) {
    int N = people.size();
    int next = people[N - 2] + people[N - 1];
    people.erase(people.begin() + (N - 1));
    people.erase(people.begin() + (N - 2));
    if (next < 10) {
      people.push_back(next);
    } else {
      people.push_back(next / 10);
      people.push_back(next % 10);
    }
#ifdef DEBUG
    printf("*START: rem\n");
    cout << people.size() << endl;
    printf("*END: rem\n");
#endif
    cnt++;
  }
  cout << cnt << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long M;
  scanf("%lld", &M);
  std::vector<long long> d(M);
  std::vector<long long> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &d[i]);
    scanf("%lld", &c[i]);
  }
  solve(M, std::move(d), std::move(c));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long
    ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

// #define DEBUG

void pushAsDigits(vec<int> &v, ll num) {
  if (num == 0) {
    v.push_back(0);
    return;
  }
  // num > 0
  ll tens = 1;
  while (tens <= num) {
    tens *= 10;
  }
  // tens > num
  tens /= 10;
  // tens <= num
  while (tens > 0) {
    v.push_back(num / tens);
    num %= tens;
    tens /= 10;
  }
}

struct Result {
  vector<int> seq;
  ll cnt;
};

Result compress(int dig, ll num) {
  if (num == 0) {
    return Result{vector<int>{}, 0};
  }
  if (num < 2) {
    assert(num == 1);
    return Result{vector<int>{dig}, 0};
  }
  if (num % 2 == 1) {
    auto res = compress(dig, num - 1);
    res.seq.push_back(dig);
    return res;
  }

  if (dig * 2 < 10) {
    auto res = compress(dig * 2, num / 2);
    return Result{res.seq, res.cnt + num / 2};
  } else {
    auto res1 = compress((dig * 2) / 10, num / 2);
    auto res2 = compress((dig * 2) % 10, num / 2);
    res1.seq.insert(res1.seq.end(), res2.seq.begin(), res2.seq.end());
    return Result{res1.seq, res1.cnt + res2.cnt + num / 2};
  }
}

void solve(long long M, std::vector<long long> d, std::vector<long long> c) {
  ll cnt = 0;
  vec<int> people;

  // 各数の出現回数をしらべる
  vector<ll> appr(10, 0);
  for (int i = 0; i < M; i++) {
    appr[d[i]] += c[i];
  }

  for (int i = 0; i < 10; i++) {
    if (appr[i] == 0) {
      continue;
    }
    auto res = compress(i, appr[i]);
    cnt += res.cnt;
    people.insert(people.end(), res.seq.begin(), res.seq.end());
  }

  // for(int i=0; i < M; i++){
  //     // for(int j=0; j < c[i]; j++){
  //     //     people.push_back(d[i]);
  //     // }
  //     auto res = compress(d[i], c[i]);
  //     #ifdef DEBUG
  //     printf("*START: hoge\n");
  //     for(auto iter: res.seq){
  //         cout << iter << " ";
  //     }
  //     cout << endl;
  //     cout << res.cnt << endl;
  //     printf("*END: hoge\n");
  //     #endif
  //     cnt += res.cnt;
  //     people.insert(people.end(), res.seq.begin(), res.seq.end());
  // }

  while (people.size() > 1) {
    int N = people.size();
    int next = people[N - 2] + people[N - 1];
    people.erase(people.begin() + (N - 1));
    people.erase(people.begin() + (N - 2));
    if (next < 10) {
      people.push_back(next);
    } else {
      people.push_back(next / 10);
      people.push_back(next % 10);
    }
#ifdef DEBUG
    printf("*START: rem\n");
    cout << people.size() << endl;
    printf("*END: rem\n");
#endif
    cnt++;
  }
  cout << cnt << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long M;
  scanf("%lld", &M);
  std::vector<long long> d(M);
  std::vector<long long> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &d[i]);
    scanf("%lld", &c[i]);
  }
  solve(M, std::move(d), std::move(c));
  return 0;
}