Problem p03789 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const ll inf = 1LL << 60;
const double eps = 1e-9;

vector<int> N;

vector<int> to_vi(ll n) {
  vector<int> v;
  while (n > 0) {
    v.pb(n % 10);
    n /= 10;
  }
  return v;
}
vector<int> operator+(vector<int> a, vector<int> b) {
  if (SZ(a) > SZ(b))
    swap(a, b);
  while (SZ(a) < SZ(b))
    a.pb(0);
  vector<int> c;
  int r = 0;
  rep(i, 0, SZ(a)) {
    c.push_back(a[i] + b[i] + r);
    r = 0;
    if (c.back() > 9)
      r = 1, c.back() -= 10;
  }
  if (r)
    c.pb(1);
  return c;
}
vector<int> operator*(vector<int> a, int k) {
  vector<int> b;
  ll r = 0;
  rep(i, 0, SZ(a)) {
    r += k * a[i];
    b.pb(r % 10);
    r /= 10;
  }
  while (r > 0)
    b.pb(r % 10), r /= 10;
  return b;
}
ostream &operator<<(ostream &o, const vector<int> &v) {
  per(i, 0, SZ(v)) o << v[i];
}

bool check(int k) {
  vector<int> M = N * 9;
  M = M + to_vi(k * 9);
  int sum = 0;
  for (auto i : M)
    sum += i;
  return (sum / 9 <= k);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  per(i, 0, SZ(s)) N.pb(s[i] - '0');
  int lo = 0, hi = 1e8, res;
  while (lo <= hi) {
    int md = (lo + hi) >> 1;
    if (check(md))
      res = md, hi = md - 1;
    else
      lo = md + 1;
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const ll inf = 1LL << 60;
const double eps = 1e-9;

vector<int> N;

vector<int> to_vi(ll n) {
  vector<int> v;
  while (n > 0) {
    v.pb(n % 10);
    n /= 10;
  }
  return v;
}
vector<int> operator+(vector<int> a, vector<int> b) {
  if (SZ(a) > SZ(b))
    swap(a, b);
  while (SZ(a) < SZ(b))
    a.pb(0);
  vector<int> c;
  int r = 0;
  rep(i, 0, SZ(a)) {
    c.push_back(a[i] + b[i] + r);
    r = 0;
    if (c.back() > 9)
      r = 1, c.back() -= 10;
  }
  if (r)
    c.pb(1);
  return c;
}
vector<int> operator*(vector<int> a, int k) {
  vector<int> b;
  ll r = 0;
  rep(i, 0, SZ(a)) {
    r += k * a[i];
    b.pb(r % 10);
    r /= 10;
  }
  while (r > 0)
    b.pb(r % 10), r /= 10;
  return b;
}
ostream &operator<<(ostream &o, const vector<int> &v) {
  per(i, 0, SZ(v)) o << v[i];
}

bool check(int k) {
  vector<int> M = N * 9;
  M = M + to_vi(k * 9);
  int sum = 0;
  for (auto i : M)
    sum += i;
  return (sum / 9 <= k);
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("test.txt", "r", stdin);
  // #endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  per(i, 0, SZ(s)) N.pb(s[i] - '0');
  int lo = 0, hi = 1e8, res;
  while (lo <= hi) {
    int md = (lo + hi) >> 1;
    if (check(md))
      res = md, hi = md - 1;
    else
      lo = md + 1;
  }
  cout << res << endl;
}