CF BUDDY
← Problems·

1060B · Maximum Sum of Digits

1100 · greedy

Problem: You are given a positive integer nn.

Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)=1+2+3=6S(123) = 1 + 2 + 3 = 6, S(0)=0S(0) = 0.

Your task is to find two integers a,ba, b, such that 0a,bn0 \leq a, b \leq n, a+b=na + b = n and S(a)+S(b)S(a) + S(b) is the largest possible among all such pairs.

Input Format: The only line of input contains an integer nn (1n1012)(1 \leq n \leq 10^{12}).

Output Format: Print largest S(a)+S(b)S(a) + S(b) among all pairs of integers a,ba, b, such that 0a,bn0 \leq a, b \leq n and a+b=na + b = n.

Note: In the first example, you can choose, for example, a=17a = 17 and b=18b = 18, so that S(17)+S(18)=1+7+1+8=17S(17) + S(18) = 1 + 7 + 1 + 8 = 17. It can be shown that it is impossible to get a larger answer.

In the second test example, you can choose, for example, a=5000000001a = 5000000001 and b=4999999999b = 4999999999, with S(5000000001)+S(4999999999)=91S(5000000001) + S(4999999999) = 91. It can be shown that it is impossible to get a larger answer.

Sample Cases

Case 1

Input

35

Output

17

Case 2

Input

10000000000

Output

91

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback