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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define sc second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 10000000007;
int main() {
  ll k;
  cin >> k;
  ll dp[15][10];
  ll vk[16];
  rep(i, 10) dp[0][i] = 1;

  Rep(i, 1, 15) {
    rep(j, 10) {
      if (j == 0) {
        dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
      } else if (j == 9) {
        dp[i][9] = dp[i - 1][8] + dp[i - 1][9];
      } else {
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1];
      }
    }
  }
  ll s[15];
  rep(i, 15) {
    s[i] = 0;
    Rep(j, 1, 10) s[i] += dp[i][j];
  }

  ll cnt = 0;
  ll c = 0;
  rep(i, 15) {
    c = i;
    if (cnt + s[i] > k)
      break;
    cnt += s[i];
  }
  if (c == 0) {
    cout << k << endl;
  } else {
    ll cans = c;
    Rep(i, 1, 10) {
      vk[c] = i;
      if (cnt + dp[c][i] >= k)
        break;
      cnt = cnt + dp[c][i];
    }

    while (c > 1) {
      if (vk[c] == 0) {
        if (cnt + dp[c - 1][0] >= k) {
          vk[c - 1] = 0;
        } else {
          vk[c - 1] = 1;
          cnt = cnt + dp[c - 1][0];
        }
      } else if (vk[c] == 9) {
        if (cnt + dp[c - 1][8] >= k) {
          vk[c - 1] = 8;
        } else {
          vk[c - 1] = 9;
          cnt = cnt + dp[c - 1][8];
        }
      } else {
        if (cnt + dp[c - 1][vk[c] - 1] >= k) {
          vk[c - 1] = vk[c] - 1;
        } else if (cnt + dp[c - 1][vk[c] - 1] + dp[c - 1][vk[c]] >= k) {
          vk[c - 1] = vk[c];
          cnt = cnt + dp[c - 1][vk[c] - 1];
        } else {
          vk[c - 1] = vk[c] + 1;
          cnt = cnt + dp[c - 1][vk[c] - 1] + dp[c - 1][vk[c]];
        }
      }
      c--;
    }
    c--;
    k = k - cnt;
    if (vk[c + 1] == 0) {
      if (k == 1) {
        vk[c] = 0;
      } else {
        vk[c] = 1;
      }
    }

    else if (vk[c + 1] == 9) {
      if (k == 1) {
        vk[c] = 8;
      } else {
        vk[c] = 9;
      }

    } else {
      if (k == 1) {
        vk[c] = vk[c + 1] - 1;
      } else if (k == 2) {
        vk[c] = vk[c + 1];
      } else {
        vk[c] = vk[c + 1] + 1;
      }
    }
    rep(i, cans + 1) { cout << vk[cans - i]; }
  }
  cout << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define sc second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 10000000007;
int main() {
  int k;
  cin >> k;
  int dp[6][10];
  int vk[7];
  rep(i, 10) dp[0][i] = 1;

  Rep(i, 1, 6) {
    rep(j, 10) {
      if (j == 0) {
        dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
      } else if (j == 9) {
        dp[i][9] = dp[i - 1][8] + dp[i - 1][9];
      } else {
        dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1];
      }
    }
  }
  int s[7];
  rep(i, 6) {
    s[i] = 0;
    Rep(j, 1, 10) s[i] += dp[i][j];
  }

  int cnt = 0;
  int c = 0;
  rep(i, 6) {
    c = i;
    if (cnt + s[i] > k)
      break;
    cnt += s[i];
  }
  if (c == 0)
    cout << k << endl;
  else {
    int cans = c;
    Rep(i, 1, 10) {
      vk[c] = i;
      if (cnt + dp[c][i] >= k)
        break;
      cnt = cnt + dp[c][i];
    }
    c--;
    while (c > 0) {
      if (vk[c] == 0) {
        if (cnt + dp[c - 1][0] >= k) {
          vk[c - 1] = 0;
        } else {
          vk[c - 1] = 1;
          cnt = cnt + dp[c - 1][0];
        }
      } else if (vk[c] == 9) {
        if (cnt + dp[c - 1][8] >= k) {
          vk[c - 1] = 8;
        } else {
          vk[c - 1] = 9;
          cnt = cnt + dp[c - 1][8];
        }
      } else {
        if (cnt + dp[c - 1][vk[c] - 1] >= k) {
          vk[c - 1] = vk[c] - 1;
        } else if (cnt + dp[c - 1][vk[c] - 1] + dp[c - 1][vk[c]] >= k) {
          vk[c - 1] = vk[c];
          cnt = cnt + dp[c - 1][vk[c] - 1];
        } else {
          vk[c - 1] = vk[c] + 1;
          cnt = cnt + dp[c - 1][vk[c] - 1] + dp[c - 1][vk[c]];
        }
      }
      c--;
    }
    k = k - cnt;
    if (vk[c + 1] == 0) {
      if (k == 1) {
        vk[c] = 0;
      } else {
        vk[c] = 1;
      }
    }

    else if (vk[c + 1] == 9) {
      if (k == 1) {
        vk[c] = 8;
      } else {
        vk[c] = 9;
      }

    } else {
      if (k == 1) {
        vk[c] = vk[c + 1] - 1;
      } else if (k == 2) {
        vk[c] = vk[c + 1];
      } else {
        vk[c] = vk[c + 1] + 1;
      }
    }
    rep(i, cans + 1) { cout << vk[cans - i]; }

    cout << endl;
  }
}