prithivMLmods/Coder-Stat huggingface dataset:
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll n;
ll ans;
ll c = 1;

bool f(ll num, ll digit) {
  if (digit == (ll)log10(num) + 1) {
    if (n == c) {
      ans = num;
      return true;
    } else {
      c++;
      return false;
    }
  }

  int rnum = num % 10;
  for (int i = max(rnum - 1, 0); i <= min(rnum + 1, 9); i++) {
    if (f(num * 10 + i, digit))
      return true;
  }
}

int main() {
  cin >> n;

  ll digit = 1;
  while (true) {
    bool res = false;
    for (int i = 1; i <= 9; i++) {
      res = f(i, digit);
      if (res)
        break;
    }
    if (res)
      break;
    digit++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll n;
ll ans;
ll c = 1;

bool f(ll num, ll digit) {
  if (digit == (ll)log10(num) + 1) {
    if (n == c) {
      ans = num;
      return true;
    } else {
      c++;
      return false;
    }
  }

  int rnum = num % 10;
  for (int i = max(rnum - 1, 0); i <= min(rnum + 1, 9); i++) {
    if (f(num * 10 + i, digit))
      return true;
  }
  return false;
}

int main() {
  cin >> n;

  ll digit = 1;
  while (true) {
    bool res = false;
    for (int i = 1; i <= 9; i++) {
      res = f(i, digit);
      if (res)
        break;
    }
    if (res)
      break;
    digit++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define fill0(n) setfill('0') << right << setw(n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define LONGMAX 1e18
#define INTMAX 1000000000
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const long double EPS = 0.0000000001;

vector<ll> bfs(int n) {
  queue<ll> q;
  vector<ll> ret;
  int count = 0;
  for (int i = 1; i < 10; i++) {
    q.push(i);
    ret.push_back(i);
    count++;
  }
  while (1) {
    ll now = q.front();
    q.pop();
    // string now = to_string(top);
    // char c = now[now.size()-1];
    for (int i = -1; i < 2; i++) {
      ll check = now % 10 + i;
      if (check == -1 or check == 10)
        continue;
      q.push(now * 10 + check);
      ret.push_back(now * 10 + check);
      count++;
      if (count == n)
        return ret;
    }
  }
}
int main() {
  int k;
  cin >> k;
  vector<ll> v = bfs(k);
  // // for(int i = 0; i < v.size(); i++)cout << v[i] << " ";
  // cout << endl;
  cout << v[k - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define fill0(n) setfill('0') << right << setw(n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define LONGMAX 1e18
#define INTMAX 1000000000
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const long double EPS = 0.0000000001;

vector<ll> bfs(int n) {
  queue<ll> q;
  vector<ll> ret;
  int count = 0;
  for (int i = 1; i < 10; i++) {
    q.push(i);
    ret.push_back(i);
    count++;
    if (count == n)
      return ret;
  }
  while (1) {
    ll now = q.front();
    q.pop();
    // string now = to_string(top);
    // char c = now[now.size()-1];
    for (int i = -1; i < 2; i++) {
      ll check = now % 10 + i;
      if (check == -1 or check == 10)
        continue;
      q.push(now * 10 + check);
      ret.push_back(now * 10 + check);
      count++;
      if (count == n)
        return ret;
    }
  }
}
int main() {
  int k;
  cin >> k;
  vector<ll> v = bfs(k);
  // // for(int i = 0; i < v.size(); i++)cout << v[i] << " ";
  // cout << endl;
  cout << v[k - 1] << endl;
}
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int k;
  cin >> k;
  vector<ll> a;
  for (int i = 1; i <= 9; ++i)
    a.push_back(i);
  while (1) {
    if (k <= a.size()) {
      cout << a[k - 1] << endl;
    } else {
      k -= a.size();
      vector<ll> old;
      swap(old, a);
      for (ll x : old) {
        for (int i = -1; i <= 1; ++i) {
          int d = x % 10 + i;
          if (d < 0 || d > 9) {
            continue;
          }
          ll nx = x * 10 + d;
          a.push_back(nx);
        }
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int k;
  cin >> k;
  vector<ll> a;
  for (int i = 1; i <= 9; ++i)
    a.push_back(i);
  while (1) {
    if (k <= a.size()) {
      cout << a[k - 1] << endl;
      return 0;
    } else {
      k -= a.size();
      vector<ll> old;
      swap(old, a);
      for (ll x : old) {
        for (int i = -1; i <= 1; ++i) {
          int d = x % 10 + i;
          if (d < 0 || d > 9) {
            continue;
          }
          ll nx = x * 10 + d;
          a.push_back(nx);
        }
      }
    }
  }
}
Problem p02720 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

k = int(readline())
q = [(i + 1) for i in range(9)]
if k < 10:
    print(k)
cnt = 9
while q:
    x = q.pop(0)
    y = int(str(x)[-1])
    if y != 0:
        cnt += 1
        if cnt == k:
            print(10 * x + y - 1)
            exit()
        q.append(10 * x + y - 1)
    cnt += 1
    if cnt == k:
        print(10 * x + y)
        exit()
    q.append(10 * x + y)
    if y != 9:
        cnt += 1
        if cnt == k:
            print(10 * x + y + 1)
            exit()
        q.append(10 * x + y + 1)


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

k = int(readline())
q = [(i + 1) for i in range(9)]
if k < 10:
    print(k)
    exit()
cnt = 9
while q:
    x = q.pop(0)
    y = int(str(x)[-1])
    if y != 0:
        cnt += 1
        if cnt == k:
            print(10 * x + y - 1)
            exit()
        q.append(10 * x + y - 1)
    cnt += 1
    if cnt == k:
        print(10 * x + y)
        exit()
    q.append(10 * x + y)
    if y != 9:
        cnt += 1
        if cnt == k:
            print(10 * x + y + 1)
            exit()
        q.append(10 * x + y + 1)
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll fr;
  ll to;
  ll d;
};

///////////////////////////

ll d(ll n) {
  if (n < 10) {
    return 1;
  }
  return d(n / 10) + 1;
}

ll keta(ll n, ll k) {
  if (k == 1) {
    return n % 10;
  } else {
    return keta(n / 10, k - 1);
  }
}

ll p(ll n) {
  if (n == 0) {
    return 1;
  }
  return p(n / 10) * 10;
}

int main() {
  ll X;
  cin >> X;

  ll ans = 1;
  ll c = 0;

  while (c < X) {
    ll b = 1;
    for (ll i = d(ans) - 1; i >= 1; i--) {
      if (abs(keta(ans, i) - keta(ans, i + 1)) > 1) {
        b = 0;
        ans += p(i - 1);
        break;
      }
    }

    // cout << ans << c << b << endl;

    if (b == 1) {
      c++;
      if (c == X) {
        break;
      }
      ans++;
    }
  }

  cout << ans;

  system("PAUSE");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll fr;
  ll to;
  ll d;
};

///////////////////////////

ll d(ll n) {
  if (n < 10) {
    return 1;
  }
  return d(n / 10) + 1;
}

ll keta(ll n, ll k) {
  if (k == 1) {
    return n % 10;
  } else {
    return keta(n / 10, k - 1);
  }
}

ll p(ll n) {
  if (n == 0) {
    return 1;
  }
  return p(n - 1) * 10;
}

int main() {
  ll X;
  cin >> X;

  ll ans = 1;
  ll c = 0;

  while (c < X) {
    ll b = 1;
    for (ll i = d(ans) - 1; i >= 1; i--) {
      if (abs(keta(ans, i) - keta(ans, i + 1)) > 1) {
        b = 0;
        ans += p(i - 1);
        break;
      }
    }

    // cout << ans << c << b << endl;

    if (b == 1) {
      c++;
      if (c == X) {
        break;
      }
      ans++;
    }
  }

  cout << ans;

  system("PAUSE");
}
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int k;
  cin >> k;
  vector<ll> a;

  for (int i = 1; i <= 9; ++i)
    a.push_back(i);
  while (1) {
    if (k <= a.size()) {
      cout << a[k - 1] << endl;
    }
    k -= a.size();
    vector<ll> old;
    swap(old, a);
    for (ll x : old) {
      for (int i = -1; i <= 1; ++i) {
        int d = x % 10 + i;
        if (d < 0 || d > 9)
          continue;
        ll nx = x * 10 + d;
        a.push_back(nx);
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int k;
  cin >> k;
  vector<ll> a;

  for (int i = 1; i <= 9; ++i)
    a.push_back(i);
  while (1) {
    if (k <= a.size()) {
      cout << a[k - 1] << endl;
      return 0;
    }
    k -= a.size();
    vector<ll> old;
    swap(old, a);
    for (ll x : old) {
      for (int i = -1; i <= 1; ++i) {
        int d = x % 10 + i;
        if (d < 0 || d > 9)
          continue;
        ll nx = x * 10 + d;
        a.push_back(nx);
      }
    }
  }
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
bool lun(string n) {
  int len = n.length();
  for (int i = 1; i < len; i++) {
    if (abs(n[i] - n[i - 1]) > 1)
      return false;
  }
  return true;
}
string nxt(string n) {
  int len = n.length();
  for (int i = len - 2; i >= 0; i--) {
    if (n[i] + 1 != n[i + 1] && n[i + 1] != '9') {
      n[i + 1] += 1;
      for (int j = i + 2; j < len; j++) {
        if (n[j - 1] == '0')
          n[j] = '0';
        else
          n[j] = n[j - 1] - 1;
      }
      return n;
    }
  }
  if (n[0] == '9')
    n = '1' + string(len, '0');
  else {
    n[0]++;
    for (int i = 1; i < len; i++) {
      if (n[i - 1] == '0')
        n[i] = '0';
      else
        n[i] = n[i - 1] - 1;
    }
  }
  return n;
}
int main(void) {
  int k;
  string n = "10";
  cin >> k;
  if (k <= 10)
    return k;
  for (int i = 10; i < k; i++) {
    // cout << n << endl;
    n = nxt(n);
  }
  cout << n << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
bool lun(string n) {
  int len = n.length();
  for (int i = 1; i < len; i++) {
    if (abs(n[i] - n[i - 1]) > 1)
      return false;
  }
  return true;
}
string nxt(string n) {
  int len = n.length();
  for (int i = len - 2; i >= 0; i--) {
    if (n[i] + 1 != n[i + 1] && n[i + 1] != '9') {
      n[i + 1] += 1;
      for (int j = i + 2; j < len; j++) {
        if (n[j - 1] == '0')
          n[j] = '0';
        else
          n[j] = n[j - 1] - 1;
      }
      return n;
    }
  }
  if (n[0] == '9')
    n = '1' + string(len, '0');
  else {
    n[0]++;
    for (int i = 1; i < len; i++) {
      if (n[i - 1] == '0')
        n[i] = '0';
      else
        n[i] = n[i - 1] - 1;
    }
  }
  return n;
}
int main(void) {
  int k;
  string n = "10";
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 0;
  }
  for (int i = 10; i < k; i++) {
    // cout << n << endl;
    n = nxt(n);
  }
  cout << n << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

bool lun(string n) {
  int len = n.length();
  for (int i = 1; i < len; i++) {
    if (abs(n[i] - n[i - 1]) > 1)
      return false;
  }
  return true;
}

string nxt(string n) {
  int len = n.length();
  for (int i = len - 2; i >= 0; i--) {
    if (n[i] + 1 != n[i + 1] && n[i + 1] != '9') {
      n[i + 1] += 1;
      for (int j = i + 2; j < len; j++) {
        if (n[j - 1] == '0')
          n[j] = '0';
        else
          n[j] = n[j - 1] - 1;
      }
      return n;
    }
  }
  if (n[0] == '9')
    n = '1' + string(len, '0');
  else {
    n[0]++;
    for (int i = 1; i < len; i++) {
      if (n[i - 1] == '0')
        n[i] = '0';
      else
        n[i] = n[i - 1] - 1;
    }
  }
  return n;
}

int main(void) {
  int k;
  string n = "10";
  cin >> k;
  if (k <= 10)
    return k;
  for (int i = 11; i <= k; i++) {
    // cout << n << endl;
    n = nxt(n);
  }
  cout << n << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

bool lun(string n) {
  int len = n.length();
  for (int i = 1; i < len; i++) {
    if (abs(n[i] - n[i - 1]) > 1)
      return false;
  }
  return true;
}

string nxt(string n) {
  int len = n.length();
  for (int i = len - 2; i >= 0; i--) {
    if (n[i] + 1 != n[i + 1] && n[i + 1] != '9') {
      n[i + 1] += 1;
      for (int j = i + 2; j < len; j++) {
        if (n[j - 1] == '0')
          n[j] = '0';
        else
          n[j] = n[j - 1] - 1;
      }
      return n;
    }
  }
  if (n[0] == '9')
    n = '1' + string(len, '0');
  else {
    n[0]++;
    for (int i = 1; i < len; i++) {
      if (n[i - 1] == '0')
        n[i] = '0';
      else
        n[i] = n[i - 1] - 1;
    }
  }
  return n;
}

int main(void) {
  int k;
  string n = "10";
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 0;
  }
  for (int i = 10; i < k; i++) {
    // cout << n << endl;
    n = nxt(n);
  }
  cout << n << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define in(s) cin >> s
#define out(s) cout << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rept(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define MOD 1000000007

#define IMIN INT_MIN;
#define IMAX INT_MAX;

bool checklun(ll val) {
  ll prev = val % 10;
  while (val >= 10) {
    val /= 10;
    if (abs(val % 10 - prev) > 1)
      return false;
    prev = val % 10;
  }
  return true;
}

ll nextlun(ll val) {
  if (checklun(val + 1))
    return val + 1;
  for (int i = 1; i < 10; i++) {
    val /= 10;
    val++;
    ll tmp = val % 10;
    if (checklun(val)) {
      rep(j, i) {
        val *= 10;
        tmp = max(tmp - 1, 0LL);
        val += tmp;
      }
      return val;
    } else
      val--;
  }
  return val;
}

int main() {
  ll k;
  in(k);
  vector<ll> dp(k + 1, 0);
  rep(i, 10) dp[i] = i;
  rept(i, 10, 100001) {
    // out(i << ' ' << nextlun(dp[i-1]));
    dp[i] = nextlun(dp[i - 1]);
  }
  // rept(i, 1, k) cout << dp[i] << ' '; cout << endl;
  out(dp[k]);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define in(s) cin >> s
#define out(s) cout << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rept(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define MOD 1000000007

#define IMIN INT_MIN;
#define IMAX INT_MAX;

bool checklun(ll val) {
  ll prev = val % 10;
  while (val >= 10) {
    val /= 10;
    if (abs(val % 10 - prev) > 1)
      return false;
    prev = val % 10;
  }
  return true;
}

ll nextlun(ll val) {
  if (checklun(val + 1))
    return val + 1;
  for (int i = 1; i < 10; i++) {
    val /= 10;
    val++;
    ll tmp = val % 10;
    if (checklun(val)) {
      rep(j, i) {
        val *= 10;
        tmp = max(tmp - 1, 0LL);
        val += tmp;
      }
      return val;
    } else
      val--;
  }
  return val;
}

int main() {
  ll k;
  in(k);
  vector<ll> dp(100001, 0);
  rep(i, 10) dp[i] = i;
  rept(i, 10, 100001) {
    // out(i << ' ' << nextlun(dp[i-1]));
    dp[i] = nextlun(dp[i - 1]);
  }
  // rept(i, 1, k) cout << dp[i] << ' '; cout << endl;
  out(dp[k]);

  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (ll i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  ll k;
  cin >> k;
  queue<ll> q;
  repA(i, 1, 9) q.push(i);
  if (k <= 9) {
    return k;
  } else {
    k -= 9;
    while (k) {
      ll a = q.front();
      q.pop();
      if (a % 10 != 0) {
        ll var = a * 10 + (a % 10 - 1);
        k--;
        if (k == 0) {
          cout << var << endl;
          return 0;
        }
        q.push(var);
      }
      q.push(a * 10 + (a % 10));
      k--;
      if (k == 0) {
        cout << a * 10 + (a % 10) << endl;
        return 0;
      }
      if (a % 10 != 9) {
        ll var = a * 10 + (a % 10 + 1);
        k--;
        if (k == 0) {
          cout << var << endl;
          return 0;
        }
        q.push(var);
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (ll i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  ll k;
  cin >> k;
  queue<ll> q;
  repA(i, 1, 9) q.push(i);
  if (k <= 9) {
    cout << k << endl;
  } else {
    k -= 9;
    while (k) {
      ll a = q.front();
      q.pop();
      if (a % 10 != 0) {
        ll var = a * 10 + (a % 10 - 1);
        k--;
        if (k == 0) {
          cout << var << endl;
          return 0;
        }
        q.push(var);
      }
      q.push(a * 10 + (a % 10));
      k--;
      if (k == 0) {
        cout << a * 10 + (a % 10) << endl;
        return 0;
      }
      if (a % 10 != 9) {
        ll var = a * 10 + (a % 10 + 1);
        k--;
        if (k == 0) {
          cout << var << endl;
          return 0;
        }
        q.push(var);
      }
    }
  }
}
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \
  cin >> n;                                                                    \
  for (int i = 1; i <= n; i++)                                                 \
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

bool isl(int x) {
  int p = x % 10;
  while (x) {
    int c = x % 10;
    if (abs(c - p) > 1) {
      return false;
    }
    p = c;
    x /= 10;
  }
  return true;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int k;
  cin >> k;
  int x = 0;
  while (k) {
    x++;
    k -= isl(x);
  }
  cout << x << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \
  cin >> n;                                                                    \
  for (int i = 1; i <= n; i++)                                                 \
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\n"
#define int long long
typedef long long ll;
using namespace std;

bool isl(int x) {
  int p = x % 10;
  while (x) {
    int c = x % 10;
    if (abs(c - p) > 1) {
      return false;
    }
    p = c;
    x /= 10;
  }
  return true;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int k;
  cin >> k;
  int x = 0;
  while (k) {
    x++;
    int p = 1, tp = 10;
    while (x / tp) {
      int u = x / p % 10, d = x / tp % 10;
      if (u == d + 2) {
        x += 8 * p;
        x -= x % p;
      } else if (u < d - 1) {
        x += (d - 1 - u) * p;
        x -= x % p;
      }
      p *= 10, tp *= 10;
    }
    k -= isl(x);
  }
  cout << x << endl;
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
/**
 *   Author:    @glaucoacassioc
 *   Created on 11.09.2020, 15:32:16
 **/
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
vector<vii> AdjList;

vi pai, color, dist, ts;
ll vals[1000002];
ll maxnr = 400000000;
set<ll> s;
void bkt(ll x) {
  if (s.find(x) != s.end())
    return;
  if (x == 0)
    for (int i = 1; i <= 9; i++)
      bkt(i);
  else {
    int lst = x % 10;
    if (lst > 0)
      bkt(x * 10 + (lst - 1));
    bkt(x * 10 + lst);
    if (lst < 9)
      bkt(x * 10 + (lst + 1));
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k;

  cin >> k;
  bkt(0);
  while (k) {
    s.erase(s.begin());
    --k;
  }
  cout << *s.begin() << endl;
  return 0;
}

Fixed submission:
/**
 *   Author:    @glaucoacassioc
 *   Created on 11.09.2020, 15:32:16
 **/
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
vector<vii> AdjList;

vi pai, color, dist, ts;
ll vals[1000002];
ll maxnr = 400000000;
set<ll> s;
void bkt(ll x) {
  if (s.find(x) != s.end())
    return;

  s.insert(x);
  if (x >= maxnr)
    return;

  if (x == 0)
    for (int i = 1; i <= 9; i++)
      bkt(i);
  else {
    int lst = x % 10;
    if (lst > 0)
      bkt(x * 10 + (lst - 1));
    bkt(x * 10 + lst);
    if (lst < 9)
      bkt(x * 10 + (lst + 1));
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k;

  cin >> k;
  bkt(0);
  while (k) {
    s.erase(s.begin());
    --k;
  }
  cout << *s.begin() << endl;
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int k;
  cin >> k;
  vvi dp(k, vi(10));
  vi sum(k + 1);
  rep(i, 10) { dp[1][i] = 1; }
  sum[1] = 9;
  int num = 1;
  for (num = 2; num <= k; num++) {
    sum[num] = sum[num - 1];
    rep(d, 10) {
      dp[num][d] = dp[num - 1][d];
      if (d) {
        dp[num][d] += dp[num - 1][d - 1];
      }
      if (d < 9) {
        dp[num][d] += dp[num - 1][d + 1];
      }
      if (d)
        sum[num] += dp[num][d];
    }
    if (sum[num] >= k)
      break;
  }
  string ret = "";
  int cnt = sum[num - 1];
  int prev = 0;
  for (int d = 1; d <= 9; d++) {
    if (cnt + dp[num][d] >= k) {
      prev = d;
      ret += '0' + d;
      break;
    } else
      cnt += dp[num][d];
  }
  num--;
  while (num) {
    for (int d = prev - 1; d <= prev + 1; d++) {
      if (d >= 0 && d <= 9) {
        if (cnt + dp[num][d] >= k) {
          prev = d;
          ret += '0' + d;
          break;
        } else {
          cnt += dp[num][d];
        }
      }
    }
    num--;
  }
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

ll mod = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int k;
  cin >> k;
  if (k <= 9) {
    cout << k << endl;
    return 0;
  }
  vvi dp(k, vi(10));
  vi sum(k + 1);
  rep(i, 10) { dp[1][i] = 1; }
  sum[1] = 9;
  int num = 1;
  for (num = 2; num <= k; num++) {
    sum[num] = sum[num - 1];
    rep(d, 10) {
      dp[num][d] = dp[num - 1][d];
      if (d) {
        dp[num][d] += dp[num - 1][d - 1];
      }
      if (d < 9) {
        dp[num][d] += dp[num - 1][d + 1];
      }
      if (d)
        sum[num] += dp[num][d];
    }
    if (sum[num] >= k)
      break;
  }
  string ret = "";
  int cnt = sum[num - 1];
  int prev = 0;
  for (int d = 1; d <= 9; d++) {
    if (cnt + dp[num][d] >= k) {
      prev = d;
      ret += '0' + d;
      break;
    } else
      cnt += dp[num][d];
  }
  num--;
  while (num) {
    for (int d = prev - 1; d <= prev + 1; d++) {
      if (d >= 0 && d <= 9) {
        if (cnt + dp[num][d] >= k) {
          prev = d;
          ret += '0' + d;
          break;
        } else {
          cnt += dp[num][d];
        }
      }
    }
    num--;
  }
  cout << ret << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
ll k;
set<string> st;
int main() {
  io();
  cin >> k;
  queue<string> q;
  rep(i, 1, 9) q.push(to_string(i));
  vector<ll> vec;
  while (st.size() <= 2e5) {
    string top = q.front();
    q.pop();
    st.insert(top);
    if (top.back() != '9' && top.back() != '0') {
      char ch = top.back();
      --ch;
      q.push(top + ch);
      q.push(top + top.back());
      ch += 2;
      q.push(top + ch);
    } else if (top.back() == '0') {
      char ch = top.back();
      ++ch;
      q.push(top + top.back());
      q.push(top + ch);
    } else {
      char ch = top.back();
      --ch;
      q.push(top + ch);
      q.push(top + top.back());
    }
  }
  for (auto i : st) {
    // cout << i << '\n';
    // ll num = stoi(i);
    vec.emplace_back((ll)stoi(i));
  }
  sort(vec.begin(), vec.end());
  // for (auto i : vec) cout << i << '\n';
  cout << vec[k - 1];
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
ll k;
set<string> st;
int main() {
  io();
  cin >> k;
  queue<string> q;
  rep(i, 1, 9) q.push(to_string(i));
  vector<ll> vec;
  while (st.size() <= 2e5) {
    string top = q.front();
    q.pop();
    st.insert(top);
    if (top.back() != '9' && top.back() != '0') {
      char ch = top.back();
      --ch;
      q.push(top + ch);
      q.push(top + top.back());
      ch += 2;
      q.push(top + ch);
    } else if (top.back() == '0') {
      char ch = top.back();
      ++ch;
      q.push(top + top.back());
      q.push(top + ch);
    } else {
      char ch = top.back();
      --ch;
      q.push(top + ch);
      q.push(top + top.back());
    }
  }
  for (auto i : st) {
    // cout << i << '\n';
    // ll num = stoi(i);
    vec.emplace_back((ll)stoll(i));
  }
  sort(vec.begin(), vec.end());
  // for (auto i : vec) cout << i << '\n';
  cout << vec[k - 1];
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  queue<ll> numbers;
  for (ll i = 1; i <= 9; i++)
    numbers.push(i);

  ll k;
  cin >> k;
  while (k > 1) {
    // cout<<numbers.front()<<endl;
    ll curr = numbers.front();
    numbers.pop();

    if (curr % 10 != 0)
      numbers.push((curr * 10 + ((curr % 10) - 1)));
    numbers.push((curr * 10 + ((curr % 10))));
    if (curr % 10 != 9)
      numbers.push((curr * 10 + ((curr % 10) + 1)));
    k--;
  }
  cout << numbers.front() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  queue<ll> numbers;
  for (ll i = 1; i <= 9; i++)
    numbers.push(i);

  ll k;
  cin >> k;
  while (k > 1) {
    // cout<<numbers.front()<<endl;
    ll curr = numbers.front();
    numbers.pop();

    if (curr % 10 != 0)
      numbers.push((curr * 10 + ((curr % 10) - 1)));
    numbers.push((curr * 10 + ((curr % 10))));
    if (curr % 10 != 9)
      numbers.push((curr * 10 + ((curr % 10) + 1)));
    k--;
  }
  cout << numbers.front() << endl;
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

int main() {
  int K;
  cin >> K;

  vector<ll> lun(K + 3);

  rep1(i, 10) { lun[i] = i; }

  int i = 1, j = 10;
  while (j <= K) {
    int r = lun[i] % 10;
    ll x10 = lun[i] * 10;
    if (r != 0)
      lun[j++] = x10 + r - 1;
    lun[j++] = x10 + r;
    if (r != 9)
      lun[j++] = x10 + r + 1;
    i++;
  }

  cout << lun[K] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

int main() {
  int K;
  cin >> K;

  vector<ll> lun(max(11, K + 3));

  rep1(i, 10) { lun[i] = i; }

  int i = 1, j = 10;
  while (j <= K) {
    int r = lun[i] % 10;
    ll x10 = lun[i] * 10;
    if (r != 0)
      lun[j++] = x10 + r - 1;
    lun[j++] = x10 + r;
    if (r != 9)
      lun[j++] = x10 + r + 1;
    i++;
  }

  cout << lun[K] << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int k, i = -1;
vector<long long> vec = {};
void dfs(long long x, int a) {
  vec.push_back(x);
  if (x > 10000000)
    return;
  if (x == 0) {
    for (int j = 1; j < 10; j++) {
      dfs(j, j);
    }
  } else {
    if (a != 0 && a != 9) {
      dfs(10 * x + a - 1, a - 1);
      dfs(10 * x + a, a);
      dfs(10 * x + a + 1, a + 1);
    }
    if (a == 0) {
      dfs(10 * x + 0, 0);
      dfs(10 * x + 1, 1);
    }
    if (a == 9) {
      dfs(10 * x + 8, 8);
      dfs(10 * x + 9, 9);
    }
  }
}

int main() {
  cin >> k;
  dfs(0, 0);
  sort(vec.begin(), vec.end());
  cout << vec[k] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int k, i = -1;
vector<long long> vec = {};
void dfs(long long x, int a) {
  vec.push_back(x);
  if (x > 1000000000)
    return;
  if (x == 0) {
    for (int j = 1; j < 10; j++) {
      dfs(j, j);
    }
  } else {
    if (a != 0 && a != 9) {
      dfs(10 * x + a - 1, a - 1);
      dfs(10 * x + a, a);
      dfs(10 * x + a + 1, a + 1);
    }
    if (a == 0) {
      dfs(10 * x + 0, 0);
      dfs(10 * x + 1, 1);
    }
    if (a == 9) {
      dfs(10 * x + 8, 8);
      dfs(10 * x + 9, 9);
    }
  }
}

int main() {
  cin >> k;
  dfs(0, 0);
  sort(vec.begin(), vec.end());
  cout << vec[k] << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
#define bay_bay return 0;
#define fori(n) for (int i = 0; i < n; ++i)
#define for1(n) for (int i = 1; i <= n; i += 3)
#define all(v) ((v).begin()), ((v).end())
#define sz(v) v.size()
#define endl "\n"
constexpr auto EPS = 1e-9;
#define ff fflush(stdout);
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#else
#endif
}
ll m;
ll a[1000005];
int pos = 0;
void solve(ll x) {
  if (x > 1e10)
    return;
  pos++;
  a[pos] = x;
  int last = x % 10;
  if (last == 0) {
    solve(x * 10 + 0);
    solve(x * 10 + 1);
  } else if (last == 9) {
    solve(x * 10 + 8);
    solve(x * 10 + 9);
  } else {
    solve(x * 10 + last - 1);
    solve(x * 10 + last);
    solve(x * 10 + last + 1);
  }
}
int main() {
  GOAT();
  cin >> m;
  for (ll i = 1; i <= 9; i++)
    solve(i);
  sort(a, a + pos + 1);
  cout << a[m] << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
#define bay_bay return 0;
#define fori(n) for (int i = 0; i < n; ++i)
#define for1(n) for (int i = 1; i <= n; i += 3)
#define all(v) ((v).begin()), ((v).end())
#define sz(v) v.size()
#define endl "\n"
constexpr auto EPS = 1e-9;
#define ff fflush(stdout);
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#else
#endif
}
ll m;
ll a[1000005];
int pos = 0;
void solve(ll x) {
  if (x > 1e10)
    return;
  pos++;
  a[pos] = x;
  int last = x % 10;
  if (last == 0) {
    solve(x * 10 + 0);
    solve(x * 10 + 1);
  } else if (last == 9) {
    solve(x * 10 + 8);
    solve(x * 10 + 9);
  } else {
    solve(x * 10 + last - 1);
    solve(x * 10 + last);
    solve(x * 10 + last + 1);
  }
}
int main() {
  // GOAT();
  cin >> m;
  for (ll i = 1; i <= 9; i++)
    solve(i);
  sort(a, a + pos + 1);
  cout << a[m] << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int K;
  cin >> K;
  queue<ll> que;
  for (int i = 1; i < 10; i++)
    que.push(i);
  for (int i = 0; i < K - 1; i++) {
    ll s = que.front();
    que.pop();
    for (int j = -1; j <= 1; j++) {
      ll add = (s % 10) + j;
      if (add <= 0 && add >= 9)
        que.push(s * 10 + add);
    }
  }
  cout << que.front() << endl;
  return 0;
}

// K番目に小さいものを求めよという問題でpriority_queueが使える問題は多い．まず，priority_queueに1から9をpushする．以下の操作をK回繰り返す．
// キューの先頭を取り出す．(sとする)
// sの一の位がdであるとき
// d > 0ならキューに10v + (d - 1)をpush
// キューに10v + dをpush
// d < 9ならキューに10v + (d + 1)をpush
// キューにpushされる値は，時系列にそって単調増加であるためpriority_queueではなくqueueでかまわない
// これらの操作はBFSと見なすことが出来る．


Fixed submission:
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int K;
  cin >> K;
  queue<ll> que;
  for (int i = 1; i < 10; i++)
    que.push(i);
  for (int i = 0; i < K - 1; i++) {
    ll s = que.front();
    que.pop();
    for (int j = -1; j <= 1; j++) {
      ll add = (s % 10) + j;
      if (add >= 0 && add <= 9)
        que.push(s * 10 + add);
    }
  }
  cout << que.front() << endl;
  return 0;
}

// K番目に小さいものを求めよという問題でpriority_queueが使える問題は多い．まず，priority_queueに1から9をpushする．以下の操作をK回繰り返す．
// キューの先頭を取り出す．(sとする)
// sの一の位がdであるとき
// d > 0ならキューに10v + (d - 1)をpush
// キューに10v + dをpush
// d < 9ならキューに10v + (d + 1)をpush
// キューにpushされる値は，時系列にそって単調増加であるためpriority_queueではなくqueueでかまわない
// これらの操作はBFSと見なすことが出来る．
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long arr[100001];
  int ind1, ind2;
  ind1 = 0;
  ind2 = 1;
  for (int i = 1; i < 10; i++) {
    arr[i] = i;
    ind1++;
  }
  int cur = ind1;
  for (int j = 0; j < 8; j++) {
    for (int i = ind2; i <= ind1; i++) {
      int a = arr[i] % 10;
      if (a != 0) {
        arr[++cur] = arr[i] * 10 + a - 1;
      }
      arr[++cur] = arr[i] * 10 + a;
      if (a != 9) {
        arr[++cur] = arr[i] * 10 + a + 1;
      }
    }
    ind2 = ind1 + 1;
    ind1 = cur;
  }
  for (int i = ind2; i <= ind1 && cur < 100001; i++) {
    int a = arr[i] % 10;
    if (a != 0) {
      arr[++cur] = arr[i] * 10 + a - 1;
    }
    arr[++cur] = arr[i] * 10 + a;
    if (a != 9) {
      arr[++cur] = arr[i] * 10 + a + 1;
    }
  }
  // for(int i=1;i<20;i++)cout<<arr[i]<<endl;
  // cout<<n<<endl;
  cout << arr[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long arr[100001];
  int ind1, ind2;
  ind1 = 0;
  ind2 = 1;
  for (int i = 1; i < 10; i++) {
    arr[i] = i;
    ind1++;
  }
  int cur = ind1;
  for (int j = 0; j < 8; j++) {
    for (int i = ind2; i <= ind1; i++) {
      int a = arr[i] % 10;
      if (a != 0) {
        arr[++cur] = arr[i] * 10 + a - 1;
      }
      arr[++cur] = arr[i] * 10 + a;
      if (a != 9) {
        arr[++cur] = arr[i] * 10 + a + 1;
      }
    }
    ind2 = ind1 + 1;
    ind1 = cur;
  }
  for (int i = ind2; i <= ind1 && cur < 100000; i++) {
    int a = arr[i] % 10;
    if (a != 0) {
      arr[++cur] = arr[i] * 10 + a - 1;
    }
    arr[++cur] = arr[i] * 10 + a;
    if (a != 9) {
      arr[++cur] = arr[i] * 10 + a + 1;
    }
  }
  // for(int i=1;i<20;i++)cout<<arr[i]<<endl;
  // cout<<n<<endl;
  cout << arr[n] << endl;
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long K;
  cin >> K;
  deque<long long> d;

  if (K <= 10)
    return K;

  // init
  for (int i = 1; i <= 9; ++i) {
    d.emplace_back(i);
  }

  long long cnt = 0;

  while (true) {
    cnt++;
    if (cnt == K)
      break;
    long long f = d.front();
    d.pop_front();
    long long new_num = 10 * f + (f % 10);

    if (new_num % 10 != 0) {
      d.emplace_back(new_num - 1);
    }
    d.emplace_back(new_num);
    if (new_num % 10 != 9) {
      d.emplace_back(new_num + 1);
    }
  }

  cout << d.front() << endl;
}

Fixed submission:
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long K;
  cin >> K;
  deque<long long> d;

  // init
  for (int i = 1; i <= 9; ++i) {
    d.emplace_back(i);
  }

  long long cnt = 0;

  while (true) {
    cnt++;
    if (cnt == K)
      break;
    long long f = d.front();
    d.pop_front();
    long long new_num = 10 * f + (f % 10);

    if (new_num % 10 != 0) {
      d.emplace_back(new_num - 1);
    }
    d.emplace_back(new_num);
    if (new_num % 10 != 9) {
      d.emplace_back(new_num + 1);
    }
  }

  cout << d.front() << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<long long> v;
void dfs(ll x, ll num, ll st, ll cc) {
  if (v.size() > 100050)
    return;
  if (num == 0)
    num += x;
  else {
    num *= 10;
    num += x;
  }
  if (cc > st)
    return;
  if (cc == st) {
    v.push_back(num);
    return;
  }
  if (x - 1 >= 0)
    dfs(x - 1, num, st, cc + 1);
  if (x + 1 <= 9)
    dfs(x + 1, num, st, cc + 1);
  dfs(x, num, st, cc + 1);
  return;
}

int main() {

  ll k;
  cin >> k;
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 9; j++) {
      dfs(j, 0, i, 1);
    }
  }
  sort(v.begin(), v.end());
  cout << v[k - 1] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<long long> v;
void dfs(ll x, ll num, ll st, ll cc) {
  if (v.size() > 100050)
    return;
  if (num == 0)
    num += x;
  else {
    num *= 10;
    num += x;
  }
  if (cc > st)
    return;
  if (cc == st) {
    v.push_back(num);
    return;
  }
  if (x - 1 >= 0)
    dfs(x - 1, num, st, cc + 1);
  if (x + 1 <= 9)
    dfs(x + 1, num, st, cc + 1);
  dfs(x, num, st, cc + 1);
  return;
}

int main() {

  ll k;
  cin >> k;
  for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 9; j++) {
      dfs(j, 0, i, 1);
    }
  }
  sort(v.begin(), v.end());
  cout << v[k - 1] << "\n";
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

vector<ll> lun;

void dfs(int d, ll now) {
  lun.push_back(now);

  if (d == 10)
    return;

  for (int i = -1; i <= 1; i++) {
    int add = now % 10 + i;
    if (add >= 0 && add <= 9)
      dfs(d + 1, now * 10 + add);
  }
}

int main() {
  int k;
  cin >> k;
  vector<ll> lun;
  rep(i, 9) dfs(1, i + 1);
  sort(all(lun));
  dunk(lun[k - 1]);
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(a) (a).begin(), (a).end()
typedef pair<int, int> P;
typedef long long ll;

vector<ll> lun;

void dfs(int d, ll now) {
  lun.push_back(now);

  if (d == 10)
    return;

  for (int i = -1; i <= 1; i++) {
    int add = now % 10 + i;
    if (add >= 0 && add <= 9)
      dfs(d + 1, now * 10 + add);
  }
}

int main() {
  int k;
  cin >> k;
  rep(i, 9) dfs(1, i + 1);
  sort(all(lun));
  dunk(lun[k - 1]);
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const int INF = 100;
typedef pair<int, int> P;
const int ld = 10;

int main() {
  int n;
  cin >> n;
  int a[ld];
  fill(a, a + ld, 0);
  // cout<<n<<endl;
  int i = 0;
  while (i < n) {
    // cout<<n<<" "<<endl;
    int jj = -1;
    for (int j = ld - 1; j >= 0; j--) {
      if (a[j] != 9) {
        a[j]++;
        jj = j;
        break;
      }
    }
    if (jj == -1)
      return 0;
    for (int j = jj + 1; j < n; j++) {
      a[j] = max(0, a[j - 1] - (j - jj));
    }
    bool ng = false;
    for (int j = 1, st = 0; j < ld; j++) {
      if (a[j - 1] != 0)
        st = 1;
      if (st && abs(a[j - 1] - a[j]) > 1) {
        ng = true;
        break;
      }
    }
    if (!ng)
      i++;
  }
  for (int i = 0, st = 0; i < ld; i++) {
    if (a[i] != 0)
      st++;
    if (st)
      cout << a[i];
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const int INF = 100;
typedef pair<int, int> P;
const int ld = 10;

int main() {
  int n;
  cin >> n;
  int a[ld];
  fill(a, a + ld, 0);
  // cout<<n<<endl;
  int i = 0;
  while (i < n) {
    // cout<<n<<" "<<endl;
    int jj = -1;
    for (int j = ld - 1; j >= 0; j--) {
      if (a[j] != 9) {
        a[j]++;
        jj = j;
        break;
      }
    }
    if (jj == -1)
      return 0;
    for (int j = jj + 1; j < ld; j++) {
      a[j] = max(0, a[j - 1] - (j - jj));
    }
    bool ng = false;
    for (int j = 1, st = 0; j < ld; j++) {
      if (a[j - 1] != 0)
        st = 1;
      if (st && abs(a[j - 1] - a[j]) > 1) {
        ng = true;
        break;
      }
    }
    if (!ng)
      i++;
  }
  for (int i = 0, st = 0; i < ld; i++) {
    if (a[i] != 0)
      st++;
    if (st)
      cout << a[i];
  }
  cout << endl;
}