cf#513 B. Maximum Sum of Digits

B. Maximum Sum of Digits

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

扫描二维码关注公众号,回复: 3487193 查看本文章

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

Copy

35

output

Copy

17

input

Copy

10000000000

output

Copy

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.

题意:给出一个n,需要a和b,两个数,使得a+b=n且a与b各位上的数加起来和最大

样例会让人误解   其实可以其中一个都又9组成,这样各位数上的和最后就会是最大的。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int cnt(ll a)
{
    int sum=0;
    while(a)
    {
        sum+=a%10;
        a/=10;
    }
    return sum;
}
ll judge(ll a)
{
    ll b=9;
    while(b<=a)
    {
        if(b*10+9>=a)break;
        else b=b*10+9;
    }
    return b;

}
int main()
{

    ll n;
    cin>>n;
    if(n<=10)cout<<n<<endl;
    else
    {
        int ans=0;
        ans+=cnt(judge(n))+cnt(n-judge(n));
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fqfzs/p/9758146.html