<pre>#include&lt;iostream&gt;

#include&lt;algorithm&gt;
#include&lt;vector&gt;
#include&lt;queue&gt;
#include&lt;stack&gt;
#include&lt;list&gt;
#include&lt;map&gt;
#include&lt;set&gt;

#include&lt;stdio.h&gt;
#include&lt;math.h&gt;
#include&lt;ctype.h&gt;
#include&lt;string.h&gt;

using namespace std;

#define FOR(i,a,b) for(int i=a;i&lt;=b;i++)
#define PB push_back
#define PII pair&lt;int, int&gt;
#define fi first
#define se second
#define MP make_pair

#define SIZE(s) ((int)(s).size())

int cf(int num)
{
	int s = 0;
	while(num &gt; 0)
	{
		s+= num % 10;
		num = num / 10;
	}
	return s;
}

int next(int a)
{
	int ok = cf(a) - 1;
	while(a &gt; 0)
	{
		a--;
		if (cf(a) == ok) return a;
	}
	return 0;
}

int main()
{
	int a;
	while(scanf(&quot;%d&quot;, &amp;a) == 1)
	{
		printf(&quot;%d\n&quot;, next(a));
	}
	return 0;
}

</pre>
