B. Nirvana

Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.

Help Kurt find the maximum possible product of digits among all integers from 11 to nn.

Input

The only input line contains the integer nn (1≤n≤2⋅1091≤n≤2⋅109).

Output

Print the maximum product of digits among all integers from 11 to nn.

Examples

input

Copy

390

output

Copy

216

input

Copy

7

output

Copy

7

input

Copy

1000000000

output

Copy

387420489

Note

In the first example the maximum product is achieved for 389389 (the product of digits is 3⋅8⋅9=2163⋅8⋅9=216).

In the second example the maximum product is achieved for 77 (the product of digits is 77).

In the third example the maximum product is achieved for 999999999999999999 (the product of digits is 99=38742048999=387420489).

题解:从最后一位开始递归,分该位变与不变两种情况(若变,必定变成9,乘上前面的)

为什么只能更改9呢:假设该位为7,如果改小,那么前面的不变,肯定没有该位为7大,如果改大,那么前面的必定减1,该位越大越好,那么必定是9更大了~~

#include <bits/stdc++.h>
using namespace std;
int solve(int n)
{
	if(n<10)
            return max(1,n);//防止在递归的时候出现错误,这里要加上与1比较的情况
	return max((n%10)*solve(n/10),9*solve(n/10-1));//该位不变,乘上前边and该位变成9,前面减一(取max)
}
int main()
{
	int n;
	cin>>n;
	cout<<solve(n)<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/89001671