【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83099737

题干:

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

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,找两个数a,b,使得在满足a+b=n的前提下,a和b的各位数的和最大。

解题报告:

     这题有点小坑啊,但是这么想啊,肯定是9越多越好,所以先找出最多的9999之类的,剩下的再随便挑。(因为不难想到,肯定没有比这个的和  更大的了)所以就这么构造就行,反正是SJ题。

   给一个错误的想法,up=1,然后每次都*10+9,找到最大的满足的,这样就大错特错了。对于123这个样例,输出为15,但正确答案为24(99+24)。所以啊要先凑9,因为可以证明其他的任何最大组合,都可以由这个a来借给b。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main() 
{
	ll n,a,b;
	cin>>n;
	ll up=1;
	while(n>=up) {
		up=up*10;
	}
	up/=10;
	up--;
//	cout<<up<<endl;
	a=up;
	b=n-up;
	ll sum=0;
	while(a) {
		sum+=a%10;
		a/=10;
	}
	while(b) {
		sum+=b%10;
		b/=10;
	}
	printf("%d\n",sum);
	return 0;
}

其实写成更赏心悦目,思路清晰。

while(1)
		{
			temp1*=10;
			if(temp1*10>=n)
			break;
		}
		temp1--;

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83099737