Language: C++

#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
vector<int > leftRange;

vector<int > GetNumberInDigits(int n)
{
	vector<int > res;
	while (n)
	{
		int d = n % 10;
		res.push_back(d);

		n /= 10;
	}
	reverse(res.begin(), res.end());
	return res;
}

bool IsNumberPresentInArray(int n, vector<int > digits)
{
	for (int i = 0; i < (int)digits.size(); i++)
	{
		if (n == digits[i])
		{
			return true;
		}

	}
	return false;
}

vector<int> FindFirstInvalidIndex(vector<int > number, vector<int > invalidDigits)
{	
	vector<int > res;
	for (int i = 0; i < (int)number.size(); i++)
	{
		if (IsNumberPresentInArray(number[i], invalidDigits))
		{
			res.push_back(i);
		}
	}

	return res;
}

int ConvertArrayToNumber(vector<int > number)
{
	int res = 0;
	for (int i = 0; i < (int)number.size(); i++)
	{
		res = res * 10 + number[i];
	}
	return res;
}

int FindNextDigit(int n, vector<int > invalidDigits)
{
	while (true)
	{
		if (n > 9) n = 0;
		bool isValid = true;
		for (int i = 0; i < (int)invalidDigits.size(); i++)
		{
			if (invalidDigits[i] == n)
			{
				n++;
				isValid = false;
				break;
			}

		}

		if (isValid) return n;
	}
}

int FindNextMinDigit(int n, vector<int > invalidDigits)
{
	for (int j = 0; j <= 9; j++)
	{
		if (!IsNumberPresentInArray(j, invalidDigits))
		{
			return j;
		}
	}
}

void ProcessRightRange(vector<int > &number, int index, vector<int > invalidDigits)
{
	for (int i = index; i < (int)number.size(); i++)
	{
		number[i] = FindNextMinDigit(number[i], invalidDigits);
	}
}

void ProcessLeftRange(vector<int > number, int index, vector<int > invalidDigits, int nextSum)
{
	if (index < 0)
	{
		leftRange.push_back(nextSum);
		return;
	}

	number[index] += nextSum;
	int nextDigit = FindNextDigit(number[index], invalidDigits);
	leftRange.push_back(nextDigit);

	if (nextDigit < number[index])
	{
		ProcessLeftRange(number, index - 1, invalidDigits, 1);
	}
	else
	{
		ProcessLeftRange(number, index - 1, invalidDigits, 0);
	}
}

int GetNextLargerNumber(vector<int > number, vector<int > invalidDigits)
{	
	int res = INT_MAX;
	vector<int > indexes = FindFirstInvalidIndex(number, invalidDigits);
	if (indexes.size() == 0)
	{
		return ConvertArrayToNumber(number);
	}

	for (int i = 0; i < indexes.size(); i++)
	{
		ProcessRightRange(number, indexes[i] + 1, invalidDigits);

		ProcessLeftRange(number, indexes[i], invalidDigits, 0);
		reverse(leftRange.begin(), leftRange.end());

		for (int j = indexes[i] + 1; j < number.size(); j++)
		{
			leftRange.push_back(number[j]);
		}

		res = fminl(res, ConvertArrayToNumber(leftRange));
		leftRange.clear();
	}

	return res;
}

int main()
{	
	/*freopen("input.txt", "r", stdin);*/
	freopen("output.txt", "w", stdout);
	
	int n, k;
	cin >> n >> k;

	vector<int > a(k);
	for (auto &it : a)
		cin >> it;

	vector<int > numberDigits = GetNumberInDigits(n);

	int res = GetNextLargerNumber(numberDigits, a);
	cout << res << endl;

	return 0;
}


Compiler output:
a.cc: In function 'std::vector<int> GetNumberInDigits(int)':
a.cc:18:9: error: 'reverse' was not declared in this scope
   18 |         reverse(res.begin(), res.end());
      |         ^~~~~~~
a.cc: In function 'int GetNextLargerNumber(std::vector<int>, std::vector<int>)':
a.cc:123:19: error: 'INT_MAX' was not declared in this scope
  123 |         int res = INT_MAX;
      |                   ^~~~~~~
a.cc:4:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    3 | #include <math.h>
  +++ |+#include <climits>
    4 | 
a.cc:135:17: error: 'reverse' was not declared in this scope
  135 |                 reverse(leftRange.begin(), leftRange.end());
      |                 ^~~~~~~
a.cc: In function 'int FindNextMinDigit(int, std::vector<int>)':
a.cc:89:1: warning: control reaches end of non-void function [-Wreturn-type]
   89 | }
      | ^