Problem p04039 (C++)

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

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

int n, K;
set<int> mySet;
vector<int> v;

vector<int> getDigits(int x) {
  vector<int> v;
  while (x) {
    v.pb(x % 10);
    x /= 10;
  }
  reverse(v.begin(), v.end());
  return v;
}

void faCelMaiMic() {
  // cout << "in el";
  for (int i = 1; i < 10; ++i) {
    if (mySet.find(i) == mySet.end()) {
      cout << i;
      break;
    }
  }
  for (int k = 0; k < v.size(); ++k) {
    for (int i = 0; i < 10; ++i) {
      if (mySet.find(i) == mySet.end()) {
        cout << i;
        break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif

  cin >> n >> K;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    mySet.insert(x);
  }

  v = getDigits(n);
  vector<int> nextDigit(v.size());
  for (int i = 0; i < v.size(); ++i) {
    int currDigit = v[i];
    nextDigit[i] = -1;
    for (int j = currDigit; j < 10; ++j) {
      if (mySet.find(j) == mySet.end()) {
        nextDigit[i] = j;
        break;
      }
    }
  }

  int firstPos = -1;
  for (int i = 0; i < v.size(); ++i) {
    if (nextDigit[i] == -1) {
      firstPos = i;
      break;
    }
  }

  if (firstPos == 0) {
    // cout << "aic\n";
    faCelMaiMic();
    return 0;
  }
  bool ok = false;
  for (int i = 0; i < firstPos; ++i) {
    if (nextDigit[i] > v[i]) {
      ok = true;
    }
  }
  if (ok == false && firstPos > -1) {
    bool flagMare = false;
    for (int i = firstPos - 1; i >= 0; --i) {
      bool flag = false;
      for (int j = nextDigit[i] + 1; j < 10; ++j) {
        if (mySet.find(j) == mySet.end()) {
          nextDigit[i] = j;
          flag = true;
          break;
        }
      }
      if (flag) {
        flagMare = true;
        break;
      }
    }
    if (!flagMare) {
      // cout << "aicicic\n";
      faCelMaiMic();
      return 0;
    }
  }
  ok = false;
  for (int i = 0; i < v.size(); ++i) {
    if (ok) {
      for (int i = 0; i < 10; ++i) {
        if (mySet.find(i) == mySet.end()) {
          cout << i;
          break;
        }
      }
      continue;
    }
    if (nextDigit[i] > v[i]) {
      ok = true;
    }
    cout << nextDigit[i];
  }

  return 0;
}

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

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

int n, K;
set<int> mySet;
vector<int> v;

vector<int> getDigits(int x) {
  vector<int> v;
  while (x) {
    v.pb(x % 10);
    x /= 10;
  }
  reverse(v.begin(), v.end());
  return v;
}

void faCelMaiMic() {
  // cout << "in el";
  for (int i = 1; i < 10; ++i) {
    if (mySet.find(i) == mySet.end()) {
      cout << i;
      break;
    }
  }
  for (int k = 0; k < v.size(); ++k) {
    for (int i = 0; i < 10; ++i) {
      if (mySet.find(i) == mySet.end()) {
        cout << i;
        break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  // #ifndef ONLINE_JUDGE
  // freopen("test.in", "r", stdin);
  // //freopen("test.out", "w", stdout);
  // #endif

  cin >> n >> K;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    mySet.insert(x);
  }

  v = getDigits(n);
  vector<int> nextDigit(v.size());
  for (int i = 0; i < v.size(); ++i) {
    int currDigit = v[i];
    nextDigit[i] = -1;
    for (int j = currDigit; j < 10; ++j) {
      if (mySet.find(j) == mySet.end()) {
        nextDigit[i] = j;
        break;
      }
    }
  }

  int firstPos = -1;
  for (int i = 0; i < v.size(); ++i) {
    if (nextDigit[i] == -1) {
      firstPos = i;
      break;
    }
  }

  if (firstPos == 0) {
    // cout << "aic\n";
    faCelMaiMic();
    return 0;
  }
  bool ok = false;
  for (int i = 0; i < firstPos; ++i) {
    if (nextDigit[i] > v[i]) {
      ok = true;
    }
  }
  if (ok == false && firstPos > -1) {
    bool flagMare = false;
    for (int i = firstPos - 1; i >= 0; --i) {
      bool flag = false;
      for (int j = nextDigit[i] + 1; j < 10; ++j) {
        if (mySet.find(j) == mySet.end()) {
          nextDigit[i] = j;
          flag = true;
          break;
        }
      }
      if (flag) {
        flagMare = true;
        break;
      }
    }
    if (!flagMare) {
      // cout << "aicicic\n";
      faCelMaiMic();
      return 0;
    }
  }
  ok = false;
  for (int i = 0; i < v.size(); ++i) {
    if (ok) {
      for (int i = 0; i < 10; ++i) {
        if (mySet.find(i) == mySet.end()) {
          cout << i;
          break;
        }
      }
      continue;
    }
    if (nextDigit[i] > v[i]) {
      ok = true;
    }
    cout << nextDigit[i];
  }

  return 0;
}