Skip to main content
Back to problems
Codeforces
Medium
Math
Greedy
Strings
Given Length and Sum of Digits...

Construct the smallest and largest decimal numbers with a fixed number of digits and a given digit sum.

Acceptance 0%
Problem Statement

Given Length and Sum of Digits...

You are given two integers mm and ss.

Your task is to construct:

  • the smallest non-negative integer consisting of exactly mm decimal digits whose digits sum to ss;
  • the largest such integer.

If it is impossible to form any mm-digit number with digit sum ss, report that no answer exists.

A valid number must have exactly mm digits. For the smallest number, the first digit cannot be zero unless m=1m = 1.

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 mm and ss.

  • mm — the required number of digits
  • ss — the required sum of digits

Output Format

Print two numbers separated by a space:

  • the smallest valid mm-digit number
  • the largest valid mm-digit number

If it is impossible, print -1 -1.

Constraints

  • 1m1 \le m
  • 0s0 \le s
  • Digits must be decimal digits from 0 to 9
  • The constructed numbers must have exactly mm digits
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.