Problem p04039 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define forn(i, o, n) for (int i = o; i < n; i++)
#define int long long
#define endl "\n"
#define mod 1000000007
#define double long double
#define pb push_back
#define mk make_pair
#define swap(x, y) (x ^= y ^= x ^= y)
#define TT                                                                     \
  int ttt;                                                                     \
  cin >> ttt;                                                                  \
  while (ttt--)

using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  if (n < 2)
    return 0;
  if (n < 4)
    return 1;
  if (n % 2 == 0 or n % 3 == 0)
    return 0;
  for (int i = 5; i * i <= n; i += 6)
    if (n % i == 0 or n % (i + 2) == 0)
      return 0;
  return 1;
}
int modexpo(int x, int p) {
  int res = 1;
  x = x % mod;
  while (p) {
    if (p % 2)
      res = res * x;
    p >>= 1;
    x = x * x % mod;
    res %= mod;
  }
  return res;
}

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

int min(int a, int b) {
  if (a < b) {
    return a;
  }
  return b;
}
const int N = 2005;
int n, k;
int32_t main() {
  IOS cin >> n >> k;
  int arr[10];
  memset(arr, 0, sizeof(arr));
  for (int i = 0; i < k; i++) {
    int a;
    cin >> a;
    arr[a]++;
  }
  vector<int> v;
  int temp = n;
  while (temp > 0) {
    v.pb(temp % 10);
    temp /= 10;
  }
  reverse(v.begin(), v.end());
  int flag = 0;
  int mn = 25;
  for (int j = 0; j < 10; j++) {
    if (arr[j] == 0) {
      mn = j;
      break;
    }
  }
  string ans = "";
  for (int i = 0; i < v.size(); i++) {
    if (flag == 1) {
      ans += (char)('0' + mn);
      continue;
    }
    for (int j = v[i]; j < 10; j++) {
      if (arr[j] == 0) {
        ans += (char)('0' + j);
        if (j > v[i]) {
          flag = 1;
        }
        break;
      }
    }
  }
  int x = stoi(ans);
  if (x >= n) {
    cout << ans << endl;
    return 0;
  }
  if (arr[0] == 0) {
    for (int i = 1; i < 10; i++) {
      if (arr[i] == 0) {
        cout << i;
        break;
      }
    }
    for (int i = 0; i < v.size(); i++) {
      cout << 0;
    }
    cout << endl;
  } else {
    for (int i = 1; i < 10; i++) {
      if (arr[i] == 0) {
        for (int j = 0; j < v.size() + 1; j++) {
          cout << i;
        }
        break;
      }
    }
  }
}

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define forn(i, o, n) for (int i = o; i < n; i++)
#define int long long
#define endl "\n"
#define mod 1000000007
#define double long double
#define pb push_back
#define mk make_pair
#define swap(x, y) (x ^= y ^= x ^= y)
#define TT                                                                     \
  int ttt;                                                                     \
  cin >> ttt;                                                                  \
  while (ttt--)

using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  if (n < 2)
    return 0;
  if (n < 4)
    return 1;
  if (n % 2 == 0 or n % 3 == 0)
    return 0;
  for (int i = 5; i * i <= n; i += 6)
    if (n % i == 0 or n % (i + 2) == 0)
      return 0;
  return 1;
}
int modexpo(int x, int p) {
  int res = 1;
  x = x % mod;
  while (p) {
    if (p % 2)
      res = res * x;
    p >>= 1;
    x = x * x % mod;
    res %= mod;
  }
  return res;
}

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

int min(int a, int b) {
  if (a < b) {
    return a;
  }
  return b;
}
const int N = 2005;
int n, k;
int32_t main() {
  IOS cin >> n >> k;
  int arr[10];
  memset(arr, 0, sizeof(arr));
  for (int i = 0; i < k; i++) {
    int a;
    cin >> a;
    arr[a]++;
  }
  vector<int> v;
  int temp = n;
  while (temp > 0) {
    v.pb(temp % 10);
    temp /= 10;
  }
  reverse(v.begin(), v.end());
  int flag = 0;
  int mn = 25;
  for (int j = 0; j < 10; j++) {
    if (arr[j] == 0) {
      mn = j;
      break;
    }
  }
  string ans = "";
  for (int i = 0; i < v.size(); i++) {
    if (flag == 1) {
      ans += (char)('0' + mn);
      continue;
    }
    for (int j = v[i]; j < 10; j++) {
      if (arr[j] == 0) {
        ans += (char)('0' + j);
        if (j > v[i]) {
          flag = 1;
        }
        break;
      }
    }
  }
  if (ans.length() > 0) {
    // cout<<ans<<endl;
    int x = stoi(ans);
    if (x >= n) {
      cout << ans << endl;
      return 0;
    }
  }
  if (arr[0] == 0) {
    for (int i = 1; i < 10; i++) {
      if (arr[i] == 0) {
        cout << i;
        break;
      }
    }
    for (int i = 0; i < v.size(); i++) {
      cout << 0;
    }
    cout << endl;
  } else {
    for (int i = 1; i < 10; i++) {
      if (arr[i] == 0) {
        for (int j = 0; j < v.size() + 1; j++) {
          cout << i;
        }
        break;
      }
    }
  }
}