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

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void rec(int digits, vector<ll> &nums, string &n) {
  if (SZ(nums) > 100009)
    return;
  if (digits == 0) {
    if (SZ(n) > 0) {
      nums.pb(stoll(n));
    }
  } else {
    for (int i = 0; i < 10; i++) {
      if (SZ(n) == 0) {
        if (i > 0) {
          n.pb('0' + i);
        }
        rec(digits - 1, nums, n);
        n.pop_back();
      } else if (abs(*(prev(n.end())) - ('0' + i)) <= 1) {
        n.pb('0' + i);
        rec(digits - 1, nums, n);
        n.pop_back();
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int K;
  cin >> K;
  string n = "";
  vector<ll> nums;
  rec(10, nums, n);
  cout << nums[K - 1] << endl;
}

Error: -11

Fixed submission:

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

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int _n = n, i = 0; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void rec(int digits, vector<ll> &nums, string &n) {
  if (SZ(nums) > 100009)
    return;
  if (digits == 0) {
    if (SZ(n) > 0) {
      nums.pb(stoll(n));
    }
  } else {
    for (int i = 0; i < 10; i++) {
      if (SZ(n) == 0) {
        if (i > 0) {
          n.pb('0' + i);
        }
        rec(digits - 1, nums, n);
        n.pop_back();
      } else if (abs(*(prev(n.end())) - ('0' + i)) <= 1) {
        n.pb('0' + i);
        rec(digits - 1, nums, n);
        n.pop_back();
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int K;
  cin >> K;
  string n = "";
  vector<ll> nums;
  rec(10, nums, n);
  cout << nums[K - 1] << endl;
}