1361. The Smallest Number With That Digit Sum
Print the smallest number with exactly d digits whose digits add up to s,
or none if no such number exists. The first digit may not be 0 unless d is 1.
d = 2, s = 9 -> 18
d = 3, s = 20 -> 299
d = 2, s = 19 -> none
To be smallest, the leading digits must be as small as possible, which means the TRAILING digits must be as large as possible. So fill from the right with 9s:
for each position from the last to the second:
take = min(9, remaining - 1) leave at least 1 for the first digit
place take, subtract it
place whatever remains in the first position
The - 1 in that line is the whole problem. The first digit cannot be 0, so at
least 1 must be kept back for it. Without that, d = 2, s = 9 produces 09,
which is not a two-digit number.
Existence is decided before any of this: the sum must be at least 1 and at most
9d. With d = 1 the rule loosens, because a single digit may be 0 in some
formulations but here s is at least 1, so it is simply s itself.
Constraints - `1 ≤ d ≤ 100` - `0 ≤ s ≤ 1000`
Input
One line containing two integers d and s.
Output
Print the smallest d-digit number whose digits sum to s, or none if there is none.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.