Construct the smallest and largest decimal numbers with a fixed number of digits and a given digit sum.
Given Length and Sum of Digits...
You are given two integers and .
Your task is to construct:
- the smallest non-negative integer consisting of exactly decimal digits whose digits sum to ;
- the largest such integer.
If it is impossible to form any -digit number with digit sum , report that no answer exists.
A valid number must have exactly digits. For the smallest number, the first digit cannot be zero unless .
This is a classic constructive problem: you need to place digits carefully so that the total sum is correct while keeping one result as small as possible and the other as large as possible.
Input Format
The input contains two integers and .
- — the required number of digits
- — the required sum of digits
Output Format
Print two numbers separated by a space:
- the smallest valid -digit number
- the largest valid -digit number
If it is impossible, print -1 -1.
Constraints
- Digits must be decimal digits from 0 to 9
- The constructed numbers must have exactly digits
Example 1
Input
2 15
Output
69 96
Explanation
For two digits with sum 15, the smallest valid number is 69 and the largest is 96.
Example 2
Input
1 0
Output
0 0
Explanation
A single digit number may be zero.
Show 2 more examples
Example 3
Input
2 1
Output
10 10
Explanation
The only valid two-digit number with digit sum 1 is 10.
Example 4
Input
3 28
Output
-1 -1
Explanation
The maximum possible digit sum for three digits is 27, so this is impossible.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.