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

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = (1LL << 60);
constexpr ll MOD = (1e9 + 7);
// constexpr ll MOD = (998244353);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
void dump() { cerr << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

int main() {

  int k;
  cin >> k;

  auto next_l = [](ll x) -> ll {
    if (x < 10)
      return x + 1;
    string s = to_string(x);
    int n = s.size();
    reverse(s.begin(), s.end());
    rep(i, n - 1) {
      if (s[i] != '9' && abs(s[i] + 1 - s[i + 1]) <= 1) {
        s[i]++;
        for (int j = i - 1; j >= 0; --j) {
          s[j] = max(char(s[j + 1] - 1), '0');
        }
        reverse(s.begin(), s.end());
        return stoll(s);
      }
    }
    reverse(s.begin(), s.end());
    if (s[0] != '9') {
      s[0]++;
      for (int i = 1; i < n; ++i) {
        s[i] = max(char(s[i - 1] - 1), '0');
      }
    } else {
      s = '1' + s;
      rep(i, n) s[i + 1] = '0';
    }
    return stoll(s);
  };
  ll ans = 1;
  k--;
  while (k--) {
    dump(ans);
    ans = next_l(ans);
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = (1LL << 60);
constexpr ll MOD = (1e9 + 7);
// constexpr ll MOD = (998244353);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
void dump() { cerr << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

int main() {

  int k;
  cin >> k;

  auto next_l = [](ll x) -> ll {
    if (x < 10)
      return x + 1;
    string s = to_string(x);
    int n = s.size();
    reverse(s.begin(), s.end());
    rep(i, n - 1) {
      if (s[i] != '9' && abs(s[i] + 1 - s[i + 1]) <= 1) {
        s[i]++;
        reverse(s.begin(), s.end());
        return stoll(s);
      }
    }
    reverse(s.begin(), s.end());
    if (s[0] != '9') {
      s[0]++;
      for (int i = 1; i < n; ++i) {
        s[i] = max(char(s[i - 1] - 1), '0');
      }
    } else {
      s = '1' + s;
      rep(i, n) s[i + 1] = '0';
    }
    return stoll(s);
  };
  ll ans = 1;
  while (k--) {
    dump(ans);
    ans = next_l(ans);
  }
}