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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long

const int N = 100005;
const int MOD = 1e9 + 7;

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
vector<int> v;
set<int> s;

void dfs(int n) {
  if (n > 4e9)
    return;
  s.insert(n);
  int last = n % 10;
  if (last - 1 >= 0)
    dfs(n * 10 + last - 1);

  dfs(n * 10 + last);

  if (last + 1 <= 9)
    dfs(n * 10 + last + 1);
}
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */

  for (int i = 1; i <= 9; i++) {
    dfs(i);
  }

  for (auto &it : s) {
    v.push_back(it);
  }

  int k;
  cin >> k;
  cout << v[k - 1] << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define int long long

const int N = 1005;
const int MOD = 1e9 + 7;

// lower_bound - which does not compare less than val
// upper_bound - which compares greater than val
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl

int fun(int n) {
  string s;
  s = to_string(n);
  int m = s.size();
  int count = 0;
  for (int i = 0; i < m - 1; i++) {
    if (abs(s[i + 1] - s[i]) <= 1) {
      count++;
    }
  }
  if (s.size() == 1)
    return 1;
  else if (m - 1 == count)
    return 1;
  else
    return 0;
}
int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* stuff you should look for
   * int overflow, array bounds
   * special cases (n=1?)
   * do smth instead of nothing and stay organized
   */

  int k;
  cin >> k;
  vector<int> v;
  for (int i = 1; i <= 100000; i++) {
    if (fun(i)) {
      // cout<<i<<endl;
      v.push_back(i);
    }
  }
  // cout<<v.size();
  cout << v[k - 1];
}