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

#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

void dfs(ll num, vector<ll> &v) {
  if (num > 3234566667) {
    return;
  } else {
    v.push_back(num);
    if (num % 10 == 0) {
      dfs(num * 10, v);
      dfs(num * 10 + 1, v);
    } else if (num % 10 == 9) {
      dfs(num * 10 + 9, v);
      dfs(num * 10 + 8, v);
    } else {
      dfs(num * 10 + num % 10, v);
      dfs(num * 10 + (num % 10) + 1, v);
      dfs(num * 10 + (num % 10) - 1, v);
    }
  }
  return;
}

int main() {
  ll K;
  cin >> K;
  vector<ll> s;
  if (K < 10) {
    PRINT(K);
    return 0;
  } else {
    FOR(i, 1, 10) { dfs(i, s); }
  }
  sort(ALL(s));
  PRINT(s[K - 1]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, n, m) for (ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair<ll, ll> P;
static const ll dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i)
    os << v[i] << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (auto p : m)
    os << "<" << p.first << ", " << p.second << "> ";
  return os;
}

void dfs(ll num, vector<ll> &v) {
  if (num > 3234566667) {
    return;
  } else {
    v.push_back(num);
    if (num % 10 == 0) {
      dfs(num * 10, v);
      dfs(num * 10 + 1, v);
    } else if (num % 10 == 9) {
      dfs(num * 10 + 9, v);
      dfs(num * 10 + 8, v);
    } else {
      dfs(num * 10 + num % 10, v);
      dfs(num * 10 + (num % 10) + 1, v);
      dfs(num * 10 + (num % 10) - 1, v);
    }
  }
  return;
}

int main() {
  ll K;
  cin >> K;
  vector<ll> s;
  if (K < 10) {
    PRINT(K);
  } else {
    FOR(i, 1, 10) { dfs(i, s); }
  }
  sort(ALL(s));
  PRINT(s[K - 1]);
  return 0;
}