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
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT;
}
}
}
}
}
}
 OUT:
 
if(1000>N&&N>=100)
{
for(a=0;a<=10-K;a=a+1)
{
for(b=0;b<=10-K;b=b+1)
{
for(c=1;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT2;
}
}
}
}
}
}
  OUT2:
 
 
if(100>N&&N>=10)
{
for(a=0;a<=10-K;a=a+1)
{
for(b=0;b<=10-K;b=b+1)
{
for(c=0;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT3;
}
}
}
}
}
}
  OUT3:
 
if(10>N&&N>=1)
{
for(a=0;a<=10-K;a=a+1)
{
for(b=0;b<=10-K;b=b+1)
{
for(c=0;c<=10-K;c=c+1)
{
for(d=0;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT4;
}
}
}
}
}
}
}
 OUT4:
  
  

  
  
  
  
if(E[1]==0)
{
 
 
 
 
 
if(N>=1000)
{
for(a=1;a<=10-K;a=a+1)
{
for(b=1;b<=10-K;b=b+1)
{
for(c=1;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT6;
}
}
}
}
}
}
  OUT6:
 
if(1000>N&&N>=100)
{
for(a=1;a<=10-K;a=a+1)
{
for(b=1;b<=10-K;b=b+1)
{
for(c=1;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT7;
}
}
}
}
}
}
 OUT7:
  
if(100>N&&N>=10)
{
for(a=1;a<=10-K;a=a+1)
{
for(b=1;b<=10-K;b=b+1)
{
for(c=1;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT8;
}
}
}
}
}
}
 OUT8:
  
 
if(10>N&&N>=1)
{
for(a=1;a<=10-K;a=a+1)
{
for(b=1;b<=10-K;b=b+1)
{
for(c=1;c<=10-K;c=c+1)
{
for(d=1;d<=10-K;d=d+1)
{
for(e=1;e<=10-K;e=e+1)
{
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N){T=10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e];}
if(10000*E[a]+1000*E[b]+100*E[c]+10*E[d]+E[e]>=N)goto OUT9;
}
}
}
}
}
}
}
 OUT9:
  
  
  
  

  
 
printf("%d\n",T);
 
 
 return 0;
}


Measured memory_bytes: 128