Maximum Sum of Digits

  You are given a positive integer n.

  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.

解题思路:
  给出一个数字n,将他拆分为2个数字,使拆分的两个数字每一位相加的和最大,输出相加后的和。

  个人感觉不用管下面提示。我们本着贪心的思想,希望获得尽可能多的9,就是将35,拆分为9与26,将10000000000,拆分为9999999999与1,既将原始数字拆分为比其第一位的全由9组成的数字与另一个补偿数字,补偿数字为原始数字减去拆分的全9数字。之后将拆分的两个数字所有位都相加便可以得到答案。

 #include<iostream>
#include<sstream>
#include<string>
#include<cstdio>
//CodeForces不支持万能头文件bits/stdc++.h
using namespace std;
typedef long long ll;
string n;
ll power(int a, int b){ //快速幂
ll ans = ;
while(b){
if(b & ){
ans = ans * a;
}
a = a * a;
b >>= ;
}
return ans;
}
int main()
{
ll a, b;
while(cin >> n)
{
int suma=;
int sumb=;
a = power(, n.size() - );
//若想拆分出小于且9最多的数字,只需要找到n的位数n.size() - 1
//之后便可以用10的n.size() - 1次幂找到与n位数相等数字中最小数字
//减一便可以得到我们所要拆分的数字
istringstream cinn(n);
cinn >> b;
//先用istringstream读取n中的值输入到整形b中
//b - a就是补偿的数字。
a--;
b = b - a;
while(a) //将a的每一位加起来
{
suma += a % ;
a/=;
}
while(b)
{
sumb += b % ; //将b的每一位加起来
b/=;
}
cout << suma + sumb << endl; //所有位数加和
}
return ;
}

CodeForces 1060 B Maximum Sum of Digits的更多相关文章

  1. 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 sta ...

  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. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  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. Ubuntu 16.04 无人值守自动更新

    https://help.ubuntu.com/lts/serverguide/automatic-updates.html 设置说明 APT::Periodic::Update-Package-Li ...

  2. vmware之VMware Remote Console (VMRC) SDK(三)

    前两节我们介绍了vmrc sdk的基本用法.在前面的demo中,有一个关键的问题是,我们现在所作的工作都是基于局域网的,作为应用层面上,主机不会直接暴露给用户,而是通过一系列的web service服 ...

  3. UWP开发入门(五)——自定义Panel

    各位好,终于讲到自定义Panel了.当系统自带的几个Panel比如Gird,StackPanel,RelativePanel不能满足我们的特定要求时(其实不常见啦),自定义Panel就显得非常必要,而 ...

  4. JavaScript多个音频audio标签,点击其中一个播放时,其他的停止播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. InnoDB: The innodb_system data file 'ibdata1' must be writable错误

    重新安装percona5.7过程中,启动mysql服务总是报如下的错误 --10T02::.781070Z [ERROR] InnoDB: The innodb_system data file 'i ...

  6. Weblogic wls-wsat组件反序列化漏洞(CVE-2017-10271)

    CVE编号: CVE-2017-10271 漏洞描述: Weblogic wls-wsat组件反序列化漏洞 利用脚本: https://github.com/hanc00l/weblogic_wls_ ...

  7. 蹭你wifi后 我竟然干了这样的事

    前言:故事发生在前两天,我们去参观工业园区内一家电商公司. 去参观他们公司的时候,我说要用下无线网,他们技术说密码就是他们的网站域名,我一脸懵逼表示我不知道域名,然后对方接过我手机给我连上了他们wif ...

  8. 动态代理方案性能对比 (CGLIB,ASSIT,JDK)

    动态代理工具比较成熟的产品有: JDK自带的,ASM,CGLIB(基于ASM包装),JAVAASSIST, 使用的版本分别为: JDK-1.6.0_18-b07, ASM-3.3, CGLIB-2.2 ...

  9. gradle 常用命令参数

    参考文档 https://docs.gradle.org/current/userguide/userguide gradle -q 任务名(task ) 执行指定某一个task gradle -q ...

  10. vue二级路由跳转后外部引入js失效问题解决方案

    vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...