luogu#P9538. 「AWOI Round 2 A」最大和

「AWOI Round 2 A」最大和

Problem Description

You are given an integer nn. You need to perform mm operations on this number. Here, mm is the number of digits of the current number to be operated on (by default, the natural number 00 is a one-digit number), and it may change as nn changes.

For the ii-th operation (1im)(1\leqslant i \leqslant m), you can choose one of the following three options:

  1. nn+10i1n\gets n+10^{i-1}.
  2. nn10i1n\gets n-10^{i-1}.
  3. nn remains unchanged.

You need to maximize the sum of the digits of the number after all operations.

Input Format

This problem contains multiple sets of testdata in one test file.

The first line contains a positive integer TT, meaning there are TT test cases.

The next TT lines each contain an integer nn.

Output Format

Output TT lines. Each line contains one integer, meaning the maximum possible sum of digits of nn after the operations for that test case.

4
33
2023
10
0
8
18
9
1

Hint

Sample Explanation.

  • For 3333, choose operation 1 each time, and you get the maximum value 4+4=84+4=8.
  • For 20232023, choose operation 1 for the first, second, and fourth operations, and choose operation 2 for the third operation, getting 2+9+3+4=182+9+3+4 = 18.
  • For 1010, choose operation 2 to get 99. At this time, mm becomes 11. Since you have already performed one operation, you do not perform any more operations.
  • For 00, just choose operation 1.

Constraints.

For 30%30\% of the testdata, 1T1041 \leqslant T \leqslant10^4, 0n1040 \leqslant n \leqslant 10^4.

For 100%100\% of the testdata, 1T1051 \leqslant T \leqslant 10^5, 0n1090 \leqslant n \leqslant 10^9.

Staff.

Idea\text{Idea} Data\text{Data} Check\text{Check} Solution\text{Solution}
S__X y_kx_b S__X

Translated by ChatGPT 5