Problem p04045 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  string n;
  int k;
  cin >> n >> k;
  int ns = n.size();
  set<int> notuse;
  rep(i, k) {
    int d;
    cin >> d;
    notuse.insert(d);
  }
  int ans = 0;
  vin use;
  rep(i, 10) if (!notuse.count(i)) use.push_back(i);
  int us = use.size();
  vbo judge(ns); // n[i]が存在→true,存在しない→false
  int tmp =
      1; // n[i]より大きな数字が存在する場所より前にn[i]未満しか存在しない場所が来たらアウト
  rep(i, ns) {
    if (use[us - 1] < (n[i] - '0') && tmp == 1)
      tmp = 0;
    else if (!notuse.count((n[i] - '0')))
      judge[i] = true;
    else {
      judge[i] = false;
      if (tmp == 1)
        tmp = 2;
    }
  }
  if (tmp == 0)
    rep(i, ns + 1) ans = 10 * ans + use[0]; // nより一桁大きい
  rep(i, ns) {
    if (!judge[i]) {
      ans = ans * 10 + (*upper_bound(all(use), n[i] - '0'));
      rep(j, ns - i - 1) ans = ans * 10 + use[0];
      break;
    } else
      ans = ans * 10 + (n[i] - '0');
  }
  cout << ans << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  string n;
  int k;
  cin >> n >> k;
  int ns = n.size();
  set<int> notuse;
  rep(i, k) {
    int d;
    cin >> d;
    notuse.insert(d);
  }
  int ans = 0;
  vin use;
  rep(i, 10) if (!notuse.count(i)) use.push_back(i);
  int us = use.size();
  vbo judge(ns); // n[i]が存在→true,存在しない→false
  int tmp =
      1; // n[i]より大きな数字が存在する場所より前にn[i]未満しか存在しない場所が来たらアウト
  rep(i, ns) {
    if (use[us - 1] < (n[i] - '0') && tmp == 1)
      tmp = 0;
    else if (!notuse.count((n[i] - '0')))
      judge[i] = true;
    else {
      judge[i] = false;
      if (tmp == 1)
        tmp = 2;
    }
  }
  if (tmp == 0) { // nより一桁大きい
    ans = (use[0] > 0 ? use[0] : use[1]);
    rep2(i, 1, ns + 1) ans = 10 * ans + use[0];
  } else {
    rep(i, ns) {
      if (!judge[i]) {
        ans = ans * 10 + (*upper_bound(all(use), n[i] - '0'));
        rep(j, ns - i - 1) ans = ans * 10 + use[0];
        break;
      } else
        ans = ans * 10 + (n[i] - '0');
    }
  }
  cout << ans << endl;
}