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. MvvmLight框架使用入门(二)

    上一篇我们简单对MvvmLight做了介绍.罗列了三个DLL中,各个命名空间下主要类的定义及大致作用.因为只是范范的概论,对于从未接触过MvvmLight的萌新来说,根本就是在晃点他们.不过万事开头难 ...

  2. await使用中的阻塞和并发(二)

    本文继续上篇未完成的讨论,通过将Lambda还原成最普通的代码段,来解释上篇提出的疑问.并更正上篇中一些不太正确的写法.最后会给出无需等待Async方法返回值时,对Async方法使用await的建议, ...

  3. 调用阿里云API 的demo示例(java/python)

    Java 示例 // 创建DefaultAcsClient实例并初始化 DefaultProfile profile = DefaultProfile.getProfile(vo.getAliRegi ...

  4. 线上日志集中化可视化管理:ELK

    本文来自网易云社区 作者:王贝 为什么推荐ELK: 当线上服务器出了问题,我们要做的最重要的事情是什么?当需要实时监控跟踪服务器的健康情况,我们又要拿什么去分析?大家一定会说,去看日志,去分析日志.是 ...

  5. maven指定本地的文件包

    maven指定本地的文件包 案例: <!-- CKFinder begin --> <dependency> <groupId>net.coobird</gr ...

  6. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)

    今天运行Redis时发生错误,错误信息如下: org.springframework.dao.InvalidDataAccessApiUsageException: MISCONF Redis is ...

  7. MQ 消息队列的比较

    目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...

  8. P3622 [APIO2007]动物园

    题目链接 题意分析 这是一道状压\(DP\)的题 一个人只可以欣赏到\(5\)只动物 显然可以状压 我们用\(dp[i][j]\)表示当前\([i,i+4]\)中这\(5\)只动物的状态\(j\) 在 ...

  9. 2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I

    1117 AlvinZH's Fight with DDLs I 思路 简单题,动态规划. 本题与期末练习赛B题很相似,而且更为简单些.简化问题:在数字序列上取数,不能取相邻的数. DP数组定义,dp ...

  10. Python3 操作系统与路径 模块(os / os.path / pathlib)

    #!/usr/bin/env python # coding=utf-8 __author__ = 'Luzhuo' __date__ = '2017/5/7' import os def os_de ...