F - Maximum Sum of Digits

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 0≤a,b≤n0≤a,b≤n, a+b=na+b=n and S(a)+S(b)S(a)+S(b) is the largest possible among all such pairs.

Input

The only line of input contains an integer nn (1≤n≤1012)(1≤n≤1012).

Output

Print largest S(a)+S(b)S(a)+S(b) among all pairs of integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n and a+b=na+b=n.

Examples

Input

35

Output

17

Input

10000000000

Output

91

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.

思路:如果想明白了,其实很简单。最大的个位数是9,那么对于一个整an=a+b,不妨把最高位a单独拉出来,然后减1,将1加到b上,这样a出现了最多个数的9,整体也就出现了多个9.

例如n=123;a=100,b=23;    a-1=99;b+1=24;

sum=9+9+2+4=24;

知道了思路,代码就很好写了。

提示:我用的是long long int 读取的n,也可以使用字符串。当用字符串的时候,char n[12];while(~scanf("%s",n))

代码如下;

#include <stdio.h>
#include <string.h>
typedef long long int ll;
int main()
{
ll a,b,sum,n;
ll i;
//得到n的最大位数 
while(scanf("%lld",&n)!=EOF)
{
sum=
0;
for(i=1000000000000;i>=1;i=i/10)
{

if(n/i!=0)
{
a=(n/i)*i
-1;
b=n%i+
1;
for(;i>=1;i=i/10)
{
sum+=a/i+b/i;
a=a%i;
b=b%i;
}

break;

}
}

printf("%d\n",sum);
}
}


 

猜你喜欢

转载自blog.csdn.net/weixin_43442778/article/details/83110280