#K46602. Smallest M-Digit Number with a Given Digit Sum

    ID: 28013 Type: Default 1000ms 256MiB

Smallest M-Digit Number with a Given Digit Sum

Smallest M-Digit Number with a Given Digit Sum

Given two integers N and M, determine the smallest M-digit number such that the sum of its digits is exactly N. If no such number exists, output -1.

In other words, you are required to construct the smallest number with exactly M digits (without leading zeros) whose digits add up to N. Use a greedy approach to assign digits from right to left and adjust if needed to avoid a leading zero.

Formally, find a number X = d1d2...dM (each di is a digit with d1 ≠ 0) such that
\( d_1 + d_2 + \cdots + d_M = N \) and X is the smallest possible number satisfying this condition.

inputFormat

The input consists of a single line containing two space-separated integers N and M, where:

  • N is the required sum of digits.
  • M is the number of digits the number should have.

outputFormat

Output a single integer representing the smallest M-digit number whose digits sum up to N. If no such number exists, output -1.

## sample
9 2
18