src2020/ll1867_maxproduct/ll1867_maxproduct.cpp

29 lines
870 B
C++

#include <iostream>
using namespace std;
int main()
{
int n, k, a[41] = { 0 }, f[7][41] = { 0 }, sum = 1, num = 1;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
a[i] = c - '0';
f[0][i] = f[0][i - 1] * 10 + a[i];
}
for (int i = 1; i <= k; i++) {
for (int j = i + 1; j <= n; j++) {
sum = 0, num = 1;
for (int e = j - 1; e >= i; e--) {
sum = sum + a[e + 1] * num;
num *= 10;
if (f[i][j] < f[i - 1][e] * sum) {
f[i][j] = f[i - 1][e] * sum;
}
//cout << i << " " << j << " " << f[i - 1][e] << " " << sum << " " << f[i][j] << endl;
}
if (i == j)
f[i][j] = f[i - 1][j];
}
}
cout << f[k][n];
}