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

#define ll long long
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
ll pi = acos(-1);
ll z = 998244353;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i)); }
ll right(ll i) { return ((2 * i) + 1); }
void swap(ll &a, ll &b) {
  ll c = a;
  a = b;
  b = c;
  return;
}
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)

void solve() {
  ll n;
  cin >> n;
  ll dp[11][10];
  fo(i, 11) {
    fo(j, 10) { dp[i][j] = 0; }
  }
  fo(j, 10) { dp[1][j] = 1; }
  for (ll i = 2; i < 11; i++) {
    fo(j, 10) {
      if (j - 1 >= 0)
        dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] += dp[i - 1][j];
      if (j + 1 < 10)
        dp[i][j] += dp[i - 1][j + 1];
    }
  }

  /* fo(i , 11)
   {
       fo(j , 10)
           cout << dp[i][j] << ' ';
       cout << endl ;
   }*/
  ll sum = 0, dig = 0;
  vector<ll> ans;
  /*for(ll i = 1 ; i < 11 ; i++)
  {
      for(ll j = 0 ; j <= 9 ; j++)
      {
          cout << dp[i][j] << ' ';
      }
      cout << endl ;
  }
  cout << endl ;*/
  for (ll i = 1; i < 11; i++) {
    for (ll j = 1; j < 10; j++) {
      if (sum + dp[i][j] >= n) {
        n -= sum;
        dig = i - 1;
        ans.pub(j);
        break;
      } else
        sum += dp[i][j];
    }
    if (ans.size() == 1)
      break;
  }
  /*cout << "here " << endl ;
 cout << dig << endl;
 cout << n << endl ;
  //exit(0) ;*/
  while (dig > 0) {
    ll ssum = 0;
    ll last_dig = ans[ans.size() - 1];
    // cout << last_dig << ' ' << ssum << endl ;
    for (ll i = last_dig - 1; i <= last_dig + 1; i++) {
      if (i < 0 || i > 9)
        continue;
      if (ssum + dp[dig][i] >= n) {
        n -= ssum;
        dig--;
        ans.pub(i);
        break;
      } else
        ssum += dp[dig][i];
      // cout << "sum = " << ssum << endl ;
    }
  }
  fo(i, ans.size()) { cout << ans[i]; }
  cout << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /* #ifndef ONLINE_JUDGE
   freopen("inputf.txt" , "r" , stdin) ;
   freopen("outputf.txt" , "w" , stdout) ;
   freopen("error.txt" , "w" , stderr) ;
   #endif*/

  ll t;
  // cin >> t ;
  t = 1;
  while (t--) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";

  return 0;
}

Error: WA

Fixed submission:

#define ll long long
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define fo(i, n) for (ll i = 0; i < n; i++)
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
ll pi = acos(-1);
ll z = 998244353;
ll inf = 100000000000000000;
ll p1 = 37;
ll p2 = 53;
ll mod1 = 202976689;
ll mod2 = 203034253;
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, p);
  if (b % 2 == 0)
    return ((c * c) % p);
  else
    return ((((c * c) % p) * a) % p);
}
ll inverse(ll a, ll n) { return power(a, n - 2, n); }
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}
ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
ll left(ll i) { return ((2 * i)); }
ll right(ll i) { return ((2 * i) + 1); }
void swap(ll &a, ll &b) {
  ll c = a;
  a = b;
  b = c;
  return;
}
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)

void solve() {
  ll n;
  cin >> n;
  ll dp[11][10];
  fo(i, 11) {
    fo(j, 10) { dp[i][j] = 0; }
  }
  fo(j, 10) { dp[1][j] = 1; }
  for (ll i = 2; i < 11; i++) {
    fo(j, 10) {
      if (j - 1 >= 0)
        dp[i][j] += dp[i - 1][j - 1];
      dp[i][j] += dp[i - 1][j];
      if (j + 1 < 10)
        dp[i][j] += dp[i - 1][j + 1];
    }
  }

  /* fo(i , 11)
   {
       fo(j , 10)
           cout << dp[i][j] << ' ';
       cout << endl ;
   }*/
  ll sum = 0, dig = 0;
  vector<ll> ans;
  /*for(ll i = 1 ; i < 11 ; i++)
  {
      for(ll j = 0 ; j <= 9 ; j++)
      {
          cout << dp[i][j] << ' ';
      }
      cout << endl ;
  }
  cout << endl ;*/
  for (ll i = 1; i < 11; i++) {
    for (ll j = 1; j < 10; j++) {
      if (sum + dp[i][j] >= n) {
        n -= sum;
        dig = i - 1;
        ans.pub(j);
        break;
      } else
        sum += dp[i][j];
    }
    if (ans.size() == 1)
      break;
  }
  /*cout << "here " << endl ;
 cout << dig << endl;
 cout << n << endl ;
  //exit(0) ;*/
  while (dig > 0) {
    ll ssum = 0;
    ll last_dig = ans[ans.size() - 1];
    // cout << last_dig << ' ' << ssum << endl ;
    for (ll i = last_dig - 1; i <= last_dig + 1; i++) {
      if (i < 0 || i > 9)
        continue;
      if (ssum + dp[dig][i] >= n) {
        n -= ssum;
        dig--;
        ans.pub(i);
        break;
      } else
        ssum += dp[dig][i];
      // cout << "sum = " << ssum << endl ;
    }
  }
  fo(i, ans.size()) { cout << ans[i]; }
  cout << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("inputf.txt", "r", stdin);
  freopen("outputf.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  ll t;
  // cin >> t ;
  t = 1;
  while (t--) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";

  return 0;
}