#C4964. Maximum Digit Sum in a Range

    ID: 48560 Type: Default 1000ms 256MiB

Maximum Digit Sum in a Range

Maximum Digit Sum in a Range

Given two integers \(L\) and \(R\) where \(L \le R\), your task is to find the number within the interval \([L, R]\) that has the maximum sum of digits. The digit sum of a number is defined as the sum of all its digits. For example, the digit sum of \(123\) is \(1+2+3=6\).

If more than one number has the same maximum digit sum, return the smallest number among them.

inputFormat

The input is read from standard input and consists of a single line containing two space-separated integers (L) and (R) ((1 \le L \le R \le 10^6)).

outputFormat

Output the number from the range ([L, R]) that has the maximum sum of its digits. If there are multiple numbers with the same maximum digit sum, output the smallest one.## sample

10 20
19