Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fin                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ii pair<int, int>
#define F first
#define S second
#define pb push_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fore(i, a, b) for (int i = a; i < b; i += 1)
#define forr(i, a) for (int i = a; i >= 0; i--)
#define fori(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define w(t) while (t--)
#define sz(s) int(s.size())
#define cls(a, car) memset(a, car, sizeof(a))
#define what_is(x) cerr << #x << " is " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 1 * 1e5 + 5;
const ll mod = 1e9 + 7;
const int INF = INT_MAX;
const ll INFCAD = ll(INT_MAX) * 2 + 1;
const double E = 1e-9;
// fflush(stdout)
// cout << flush
// a + b = a ^ b + 2 * (a & b)
unordered_set<ll> s;
vector<ll> ans;
void build() {
  queue<ll> q;
  q.push(1LL);
  q.push(2LL);
  q.push(3LL);
  q.push(4LL);
  q.push(5LL);
  q.push(6LL);
  q.push(7LL);
  q.push(8LL);
  q.push(9LL);

  while (sz(q)) {
    ll a = q.front();
    s.insert(a);
    q.pop();
    if (sz(s) <= 100000)
      for (int i = 0; i <= 9; i++) {
        if (abs(a % 10 - i) <= 1 && a * 10 + i <= 3234566667) {
          ll nw = a * 10 + i;
          s.insert(nw);
          q.push(nw);
        }
      }
  }
}
int main() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
  fin;
  build();
  fori(i, s) ans.pb(*i);
  sort(all(ans));
  int k, q;
  // cin >> q;
  // w(q){
  cin >> k;
  cout << ans[k - 1] << '\n';
  //}
  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fin                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ii pair<int, int>
#define F first
#define S second
#define pb push_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fore(i, a, b) for (int i = a; i < b; i += 1)
#define forr(i, a) for (int i = a; i >= 0; i--)
#define fori(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define w(t) while (t--)
#define sz(s) int(s.size())
#define cls(a, car) memset(a, car, sizeof(a))
#define what_is(x) cerr << #x << " is " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 1 * 1e5 + 5;
const ll mod = 1e9 + 7;
const int INF = INT_MAX;
const ll INFCAD = ll(INT_MAX) * 2 + 1;
const double E = 1e-9;
// fflush(stdout)
// cout << flush
// a + b = a ^ b + 2 * (a & b)
unordered_set<ll> s;
vector<ll> ans;
void build() {
  queue<ll> q;
  q.push(1LL);
  q.push(2LL);
  q.push(3LL);
  q.push(4LL);
  q.push(5LL);
  q.push(6LL);
  q.push(7LL);
  q.push(8LL);
  q.push(9LL);

  while (sz(q)) {
    ll a = q.front();
    s.insert(a);
    q.pop();
    if (sz(s) <= 100000)
      for (int i = 0; i <= 9; i++) {
        if (abs(a % 10 - i) <= 1 && a * 10 + i <= 3234566667) {
          ll nw = a * 10 + i;
          s.insert(nw);
          q.push(nw);
        }
      }
  }
}
int main() {
  // freopen("in","r",stdin);
  // freopen("out","w",stdout);
  fin;
  build();
  fori(i, s) ans.pb(*i);
  sort(all(ans));
  int k, q;
  // cin >> q;
  // w(q){
  cin >> k;
  cout << ans[k - 1] << '\n';
  //}
  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}