Problem p02856 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  ll M;
  cin >> M;
  vector<ll> D(M);
  vector<ll> C(M);
  rep(i, M) cin >> D[i] >> C[i];
  ll ans = 0;
  vector<ll> cntd(10, 0);
  rep(i, M) {
    if (D[i] == 0) {
      ans += C[i];
    } else {
      cntd[D[i]] += C[i];
    }
  }
  bool flag = true;
  while (flag) {
    vector<ll> cnt_(10, 0);
    rep(i, 10) {
      if (i == 0)
        continue;
      ans += cntd[i] / 2;
    }
    cnt_[2] += cntd[1] / 2;
    cnt_[1] += cntd[1] % 2;
    cnt_[4] += cntd[2] / 2;
    cnt_[2] += cntd[2] % 2;
    cnt_[6] += cntd[3] / 2;
    cnt_[3] += cntd[3] % 2;
    cnt_[8] += cntd[4] / 2;
    cnt_[4] += cntd[4] % 2;
    cnt_[1] += cntd[5] / 2;
    cnt_[0] += cntd[5] / 2;
    cnt_[5] += cntd[5] % 2;
    cnt_[1] += cntd[6] / 2;
    cnt_[2] += cntd[6] / 2;
    cnt_[6] += cntd[6] % 2;
    cnt_[1] += cntd[7] / 2;
    cnt_[4] += cntd[7] / 2;
    cnt_[7] += cntd[7] % 2;
    cnt_[1] += cntd[8] / 2;
    cnt_[6] += cntd[8] / 2;
    cnt_[8] += cntd[8] % 2;
    cnt_[1] += cntd[9] / 2;
    cnt_[8] += cntd[9] / 2;
    cnt_[9] += cntd[9] % 2;
    ans += cnt_[0];
    cnt_[0] = 0;
    ans += 10 * (cnt_[1] / 10);
    cnt_[1] = cnt_[1] / 10 + cnt_[1] % 10;
    cntd = cnt_;
    rep(i, 10) { cerr << cntd[i] << " "; }
    cerr << endl;
    rep(i, 10) {
      if (cntd[i] >= 2)
        break;
      if (i == 9)
        flag = false;
    }
    // break;
  }
  cerr << "a" << endl;
  bool flag2 = true;
  vector<ll> cntt_ = cntd;
  while (flag2) {
    for (int x = 1; x < 10; x++) {
      for (int y = x; y < 10; y++) {
        if (cntt_[x] > 0 && cntt_[y] > 0) {
          if (x == y) {
            if (cntt_[x] >= 2) {
              if (x + y >= 10)
                cntt_[1]++;
              cntt_[(x + y) % 10]++;
              cntt_[x]--;
              cntt_[y]--;
              ans++;
            }
          } else {
            if (x + y >= 10)
              cntt_[1]++;
            cntt_[(x + y) % 10]++;
            cntt_[x]--;
            cntt_[y]--;
            ans++;
            // goto ttt;
          }
        }
      }
    }
    // ttt:
    // ans += cntt_[0]; cntt_[0] = 0;
    // ans += 10*(cntt_[1]/10); cntt_[1] = cntt_[1]/10+cntt_[1]%10;
    ll s = 0;
    rep(i, 10) {
      cerr << cntt_[i] << " ";
      s += cntt_[i];
    }
    cerr << endl;
    if (s <= 1)
      flag2 = false;
    // ans += cnt_[0]; cnt_[0]=0;
    /*
    rep(i,10){
      cntd[i] += cnt_[i];
    }
    */
    /*
    int cnt=0;
    rep(i,10){
      cerr << cntd[i] << " ";
      cnt += cntd[i];
    }
    cerr << endl;
    if(cnt==1 || cnt==0) break;
    */
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  ll M;
  cin >> M;
  vector<ll> D(M);
  vector<ll> C(M);
  rep(i, M) cin >> D[i] >> C[i];
  ll ans = 0;
  vector<ll> cntd(10, 0);
  rep(i, M) {
    if (D[i] == 0) {
      ans += C[i];
    } else {
      cntd[D[i]] += C[i];
    }
  }
  bool flag = true;
  while (flag) {
    vector<ll> cnt_(10, 0);
    rep(i, 10) {
      if (i == 0)
        continue;
      ans += cntd[i] / 2;
    }
    cnt_[2] += cntd[1] / 2;
    cnt_[1] += cntd[1] % 2;
    cnt_[4] += cntd[2] / 2;
    cnt_[2] += cntd[2] % 2;
    cnt_[6] += cntd[3] / 2;
    cnt_[3] += cntd[3] % 2;
    cnt_[8] += cntd[4] / 2;
    cnt_[4] += cntd[4] % 2;
    cnt_[1] += cntd[5] / 2;
    cnt_[0] += cntd[5] / 2;
    cnt_[5] += cntd[5] % 2;
    cnt_[1] += cntd[6] / 2;
    cnt_[2] += cntd[6] / 2;
    cnt_[6] += cntd[6] % 2;
    cnt_[1] += cntd[7] / 2;
    cnt_[4] += cntd[7] / 2;
    cnt_[7] += cntd[7] % 2;
    cnt_[1] += cntd[8] / 2;
    cnt_[6] += cntd[8] / 2;
    cnt_[8] += cntd[8] % 2;
    cnt_[1] += cntd[9] / 2;
    cnt_[8] += cntd[9] / 2;
    cnt_[9] += cntd[9] % 2;
    ans += cnt_[0];
    cnt_[0] = 0;
    ans += 10 * (cnt_[1] / 10);
    cnt_[1] = cnt_[1] / 10 + cnt_[1] % 10;
    cntd = cnt_;
    rep(i, 10) { cerr << cntd[i] << " "; }
    cerr << endl;
    rep(i, 10) {
      if (cntd[i] >= 2)
        break;
      if (i == 9)
        flag = false;
    }
    // break;
  }
  cerr << "a" << endl;
  bool flag2 = true;
  vector<ll> cntt_ = cntd;
  while (flag2) {
    for (int x = 0; x < 10; x++) {
      for (int y = x; y < 10; y++) {
        if (cntt_[x] > 0 && cntt_[y] > 0) {
          if (x == y) {
            if (cntt_[x] >= 2) {
              if (x + y >= 10)
                cntt_[1]++;
              cntt_[(x + y) % 10]++;
              cntt_[x]--;
              cntt_[y]--;
              ans++;
            }
          } else {
            if (x + y >= 10)
              cntt_[1]++;
            cntt_[(x + y) % 10]++;
            cntt_[x]--;
            cntt_[y]--;
            ans++;
            // goto ttt;
          }
        }
      }
    }
    // ttt:
    // ans += cntt_[0]; cntt_[0] = 0;
    // ans += 10*(cntt_[1]/10); cntt_[1] = cntt_[1]/10+cntt_[1]%10;
    ll s = 0;
    rep(i, 10) {
      cerr << cntt_[i] << " ";
      s += cntt_[i];
    }
    cerr << endl;
    if (s <= 1)
      flag2 = false;
    // ans += cnt_[0]; cnt_[0]=0;
    /*
    rep(i,10){
      cntd[i] += cnt_[i];
    }
    */
    /*
    int cnt=0;
    rep(i,10){
      cerr << cntd[i] << " ";
      cnt += cntd[i];
    }
    cerr << endl;
    if(cnt==1 || cnt==0) break;
    */
  }

  cout << ans << endl;
  return 0;
}