Problem p04045 (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 < (int)(n); i++)
/*
 */

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

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll func2(ll n) {
  ll res = 0;
  while (n % 2 == 0) {
    n /= 2;
    res++;
  }
  return res;
}

ll zyou(ll a, ll b) { // a＾b
  ll res = 1;
  for (int i = 1; i <= b; i++) {
    res *= a;
  }
  return res;
}
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};

typedef pair<int, int> P;
using Graph = vector<vector<Edge>>;

ll calc(ll a, ll b, ll p) { // aのb乗をpで割った余り
  if (b == 0)
    return 1;

  else if (b % 2 == 0) {
    ll d = calc(a, b / 2, p);
    return (d * d) % p;
  } else if (b % 2 == 1) {
    return (a * calc(a, b - 1, p)) % p;
  }
}

bool is_prime(int n) { // nが素数であるかどうか判定 iで割り切れると素数でない
  if (n == 1)
    return false; // 1は例外
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }

  return true;
}

bool palin(string S) { // 回文判定
  int n = S.size();
  bool flag = true;
  if (n % 2 == 0) {
    for (int i = 0; i <= n / 2 - 1; i++) {
      if (S[i] != S[n - i - 1]) {
        flag = false;
        break;
      }
    }
  } else if (n % 2 == 1) {
    for (int i = 0; i <= n / 2 - 1; i++) {
      if (S[i] != S[n - i - 1]) {
        flag = false;
        break;
      }
    }
  }
  return flag;
}
// 使える数字のみからなるN以上の最小数を求める
void dfs(int N, int a, vector<int> &v, vector<int> &use) {
  if (a >= N)
    return;

  int n = use.size();
  rep(i, n) {
    if (a == 0 && use[i] == 0)
      continue;
    if (10 * a + use[i] >= N)
      v.push_back(10 * a + use[i]);
    dfs(N, 10 * a + use[i], v, use);
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> D(N);
  rep(i, K) { cin >> D[i]; }
  set<int> s;
  rep(i, K) { s.insert(D[i]); }
  vector<int> use; // 使える数
  for (int i = 0; i <= 9; i++) {
    if (!s.count(i))
      use.push_back(i);
  }
  /*rep(i,use.size()){
    cout << use[i] << endl;
  }*/
  vector<int> v;
  dfs(N, 0, v, use);
  sort(v.begin(), v.end());
  cout << v[0] << endl;
}

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

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

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll func2(ll n) {
  ll res = 0;
  while (n % 2 == 0) {
    n /= 2;
    res++;
  }
  return res;
}

ll zyou(ll a, ll b) { // a＾b
  ll res = 1;
  for (int i = 1; i <= b; i++) {
    res *= a;
  }
  return res;
}
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};

typedef pair<int, int> P;
using Graph = vector<vector<Edge>>;

ll calc(ll a, ll b, ll p) { // aのb乗をpで割った余り
  if (b == 0)
    return 1;

  else if (b % 2 == 0) {
    ll d = calc(a, b / 2, p);
    return (d * d) % p;
  } else if (b % 2 == 1) {
    return (a * calc(a, b - 1, p)) % p;
  }
}

bool is_prime(int n) { // nが素数であるかどうか判定 iで割り切れると素数でない
  if (n == 1)
    return false; // 1は例外
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }

  return true;
}

bool palin(string S) { // 回文判定
  int n = S.size();
  bool flag = true;
  if (n % 2 == 0) {
    for (int i = 0; i <= n / 2 - 1; i++) {
      if (S[i] != S[n - i - 1]) {
        flag = false;
        break;
      }
    }
  } else if (n % 2 == 1) {
    for (int i = 0; i <= n / 2 - 1; i++) {
      if (S[i] != S[n - i - 1]) {
        flag = false;
        break;
      }
    }
  }
  return flag;
}
// 使える数字のみからなるN以上の最小数を求める
void dfs(int N, int a, vector<int> &v, vector<int> &use) {
  if (a >= N)
    return;

  int n = use.size();
  rep(i, n) {
    if (a == 0 && use[i] == 0)
      continue;
    if (10 * a + use[i] >= N)
      v.push_back(10 * a + use[i]);
    dfs(N, 10 * a + use[i], v, use);
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> D(K);
  rep(i, K) { cin >> D[i]; }
  set<int> s;
  rep(i, K) { s.insert(D[i]); }
  vector<int> use; // 使える数
  for (int i = 0; i <= 9; i++) {
    if (!s.count(i))
      use.push_back(i);
  }
  /*rep(i,use.size()){
    cout << use[i] << endl;
  }*/
  vector<int> v;
  dfs(N, 0, v, use);
  sort(v.begin(), v.end());
  cout << v[0] << endl;
}