Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2).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.

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.

#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;
    while(cin>>n){
        if(n<=10) cout<<n<<endl;
        else {
            int ans=0;
            //cout<<judge(n)<<endl;
            //cout<<judge(n)<<' '<<n-judge(n)<<' '<<cnt(judge(n))<<' '<<cnt(n-judge(n))<<endl;
            ans+=(cnt(judge(n))+cnt(n-judge(n)));
            cout<<ans<<endl;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值