#P9538. Maximize Sum of Digits after Sequential Operations
Maximize Sum of Digits after Sequential Operations
Maximize Sum of Digits after Sequential Operations
You are given a non‐negative integer \(n\). You can perform a sequence of operations on \(n\). The number of operations \(m\) is defined as the number of digits of the current number \(n\) (treating the natural number \(0\) as a one-digit number), and \(m\) may change as \(n\) changes.
For the \(i\)-th operation \( (1\le i\le m)\), you may choose one of the following three actions:
- \(n \gets n+10^{i-1}\).
- \(n \gets n-10^{i-1}\).
- Leave \(n\) unchanged.
The operations are performed sequentially. At the beginning of each operation, recalculate \(m\) as the number of digits of the current \(n\) (with \(0\) considered as one digit), and if the current operation index \(i\) exceeds \(m\), stop the process. Your goal is to maximize the sum of digits of the final number \(n\). The sum of digits is computed in base 10 (for negative numbers, assume the minus sign is not counted, i.e. consider the digits of its absolute value).
Note: Since an operation might change the number of digits of \(n\) (for example, turning \(99\) into \(100\)), the total number of operations available may change during the process.
Example: If \(n=83\), initially \(m=2\) so you perform operations for \(i=1\) and \(i=2\):
For \(i=1\) (weight \(10^{0}=1\)): choosing \(+1\) gives \(83+1=84\).
For \(i=2\) (weight \(10^{1}=10\)): choosing \(+10\) gives \(84+10=94\), whose digit sum is \(9+4=13\).
Your task is to output the maximum possible sum of digits that can be achieved by optimally choosing the operations.
inputFormat
The input consists of a single line containing a non‐negative integer \(n\).
outputFormat
Output a single integer denoting the maximum sum of digits obtainable after performing the operations.
sample
83
13