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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define ld long double

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define int ll
const ll mod = 1e9 + 7;
const ll MAX_NODES = 200001;
const ll MAX_LOG = 50;
const ll maxn = 400000 + 10;
stack<int> st;
bool first = true;
int k;
vl g[10];
int sum = 0;
int lev;
void dfs(int i, int l) {
  st.push(i);

  if (l == lev && k == sum + 1) {
    vl ans;
    while (!st.empty()) {
      ans.push_back(st.top());
      st.pop();
    }
    reverse(ans.begin(), ans.end());
    bool f = 0;
    for (int i = 0; i < ans.size(); i++) {
      if (ans[i] != 0) {
        f = 1;
      }
      if (f) {
        cout << ans[i];
      }
    }
    exit(0);
  }
  if (l == lev) {
    st.pop();
    sum++;
    return;
  }
  int sum = 0;
  for (auto x : g[i]) {
    dfs(x, l + 1);
  }

  st.pop();
  return;
}
int32_t main() {

  IOS cin >> k;
  for (int i = 0; i <= 9; i++) {
    for (int j = max(0ll, i - 1); j <= (min(9ll, i + 1)); j++) {
      g[i].push_back(j);
    }
  }

  for (int i = 1; i <= 70; i++) {
    for (int j = 1; j <= 9; j++) {
      lev = i;
      dfs(j, 1);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define ld long double

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 1
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define int ll
const ll mod = 1e9 + 7;
const ll MAX_NODES = 200001;
const ll MAX_LOG = 50;
const ll maxn = 400000 + 10;
stack<int> st;
bool first = true;
int k;
vl g[10];
int sum = 0;
int lev;
void dfs(int i, int l) {
  st.push(i);

  if (l == lev && k == sum + 1) {
    vl ans;
    while (!st.empty()) {
      ans.push_back(st.top());
      st.pop();
    }
    reverse(ans.begin(), ans.end());
    bool f = 0;
    for (int i = 0; i < ans.size(); i++) {
      if (ans[i] != 0) {
        f = 1;
      }
      if (f) {
        cout << ans[i];
      }
    }
    exit(0);
  }
  if (l == lev) {
    st.pop();
    sum++;
    return;
  }
  int sum = 0;
  for (auto x : g[i]) {
    dfs(x, l + 1);
  }

  st.pop();
  return;
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS cin >> k;
  for (int i = 0; i <= 9; i++) {
    for (int j = max(0ll, i - 1); j <= (min(9ll, i + 1)); j++) {
      g[i].push_back(j);
    }
  }

  for (int i = 1; i <= 70; i++) {
    for (int j = 1; j <= 9; j++) {
      lev = i;
      dfs(j, 1);
    }
  }
}