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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second

#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 3200
const ll MOD = 1e9 + 7;
ll dp[3000][3000];
ll func(int pos, int lastdig) {
  if (lastdig < 0 || lastdig >= 10)
    return 0;
  if (pos == 0)
    return 1;
  if (dp[pos][lastdig] != -1)
    return dp[pos][lastdig];
  ll res = 0;
  res += func(pos - 1, lastdig - 1);
  res += func(pos - 1, lastdig);
  res += func(pos - 1, lastdig + 1);
  return dp[pos][lastdig] = res;
}

ll kth;
void print(int pos, int lastdig) {
  if (lastdig == 10)
    assert(false);
  cout << lastdig;
  if (pos == 0) {
    cout << endl;
    exit(0);
    return;
  }
  if (lastdig > 0 && kth <= func(pos - 1, lastdig - 1)) {
    print(pos - 1, lastdig - 1);
    return;
  }
  kth -= func(pos - 1, lastdig - 1);
  if (kth <= func(pos - 1, lastdig)) {
    print(pos - 1, lastdig);
    return;
  }
  kth -= func(pos - 1, lastdig);
  print(pos - 1, lastdig + 1);
}
int main() {
  memset(dp, -1, sizeof dp);

  cin >> kth;
  forr(digs, 1, 100) {
    forr(lastdig, 1, 10) {
      // cout<<"resta es "<<kth-func(digs-1,lastdig)<<endl;
      // cout<<func(digs-1,lastdig)<<"--";
      if (kth <= func(digs - 1, lastdig)) {
        print(digs - 1, lastdig);
        return 0;
      }
      // cout<<func(digs-1,lastdig)<<","<<kth<<"  ";
      kth -= func(digs - 1, lastdig);
    }
    // cout<<kth<<" "<<digs<<endl;
  }
}
// PLUS ULTRA!


Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define f first
#define s second
#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 32000
const ll MOD = 1e9 + 7;
ll dp[tam][11];
int f(int pos, int lastdig) {
  if (pos == 0)
    return 1;
  if (lastdig < 0 || lastdig >= 10)
    return 0;
  if (dp[pos][lastdig] != -1)
    return dp[pos][lastdig];
  ll res = 0;
  res += f(pos - 1, lastdig);
  if (lastdig != 9)
    res += f(pos - 1, lastdig + 1);
  if (lastdig != 0)
    res += f(pos - 1, lastdig - 1);
  dp[pos][lastdig] = res;
}

int kth;
void print(int pos, int lastdig) {
  if (lastdig == 10)
    assert(false);
  assert(lastdig >= 0);
  cout << lastdig;
  if (pos == 0) {
    return;
  }
  if (lastdig != 0) {
    if (kth <= f(pos - 1, lastdig - 1)) {
      print(pos - 1, lastdig - 1);
      return;
    } else {
      kth -= f(pos - 1, lastdig - 1);
    }
  }
  if (kth <= f(pos - 1, lastdig)) {
    print(pos - 1, lastdig);
    return;
  }
  kth -= f(pos - 1, lastdig);
  print(pos - 1, lastdig + 1);
}
int main() {
  memset(dp, -1, sizeof dp);

  cin >> kth;
  forr(digs, 1, 100) {
    forr(lastdig, 1, 10) {
      if (kth <= f(digs - 1, lastdig)) {
        print(digs - 1, lastdig);
        return 0;
      } else
        kth -= f(digs - 1, lastdig);
    }
    // cout<<kth<<" "<<digs<<endl;
  }
}
// PLUS ULTRA!