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.

题意:给出一个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=;
while(a)
{
sum+=a%;
a/=;
}
return sum;
}
ll judge(ll a)
{
ll b=;
while(b<=a)
{
if(b*+>=a)break;
else b=b*+;
}
return b; }
int main()
{ ll n;
cin>>n;
if(n<=)cout<<n<<endl;
else
{
int ans=;
ans+=cnt(judge(n))+cnt(n-judge(n));
cout<<ans<<endl;
}
return ;
}

cf#513 B. Maximum Sum of Digits的更多相关文章

  1. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  2. Maximum Sum of Digits(CodeForces 1060B)

    Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...

  3. CF1060B:Maximum Sum of Digits

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...

  4. Codeforces_B.Maximum Sum of Digits

    http://codeforces.com/contest/1060/problem/B 题意:将n拆为a和b,让a+b=n且S(a)+S(b)最大,求最大的S(a)+S(b). 思路:考虑任意一个数 ...

  5. CF 276C Little Girl and Maximum Sum【贪心+差分】

    C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...

  6. CodeForces 489C Given Length and Sum of Digits... (贪心)

    Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...

  7. Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  8. codeforces#277.5 C. Given Length and Sum of Digits

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  9. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

随机推荐

  1. c#运用this.invoke() 在多线程时对UI进行修改

    什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的,线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈 ...

  2. 原生js 异步请求,responseXML解析

    异步更新原理:用XMLHTTP发送请求得到服务器端应答数据,在不重新载入整个页面的情况下,用js操作Dom最终更新页面1.创建XMLHttp请求协议 function createXMLHttpReq ...

  3. Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

    Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition Kaiming He, Xiangyu Zh ...

  4. Python 学习笔记(八)Python列表(三)

    序列 序列:数学上,序列是被排成一列的对象(或事件):这样,每个元素不是在器他元素之前,就是在其他元素之后.这里元素之间的顺序非常重要.<维基百科> 序列是Python中最基本的数据结构. ...

  5. 并发编程之多线程基础-Thread和Runnable的区别及联系(二)

    上篇文章讲述了创建线程的常用方式 本篇主要分析一下Thread和Runnable两种方式创建线程的区别及联系 联系: ▶Thread类实现了Runable接口. ▶都需要重写里面Run方法. 区别: ...

  6. shell脚本中 [-eq] [-ne] [-gt] [-lt] [ge] [le]

    -eq //等于 -ne //不等于 -gt //大于 (greater ) -lt //小于 (less) -ge //大于等于 -le //小于等于 在linux 中 命令执行状态:0 为真,其他 ...

  7. union的两个子查询是否并行

    需求描述 问题:subquery 1 union subquery2,其中union左右的两个子查询是否并行. 场景:业务中性能敏感的业务,希望能加快速度,如果数据库能两个子查询并行执行,既可以节省时 ...

  8. Percona-Tookit工具包之pt-visual-explain

      Preface       As usual we will check the MySQL executed plan of SQL query by execute "explain ...

  9. ubuntu安装和常用软件推荐

    ubuntu安装和常用软件推荐(个人整理) 2016.08.22 17:29 13811浏览 字号 安装一套双系统,win10打游戏,ubuntu开发,win10放机械,ubuntu放固态,电脑联想i ...

  10. 简单使用PuTTy登录centos虚拟机

    博主刚刚开始学习Linux,想通过写博客的方式加深对linux学习的记忆 使用虚拟机安装精简版的linux后发现窗口字体太小,于是就想着通过PuTTy登录的方式解决 简单安装linux后 将linux ...