输入输出样例
输入样例#1:
4 2
1231
输出样例#1:
62
import java.util.Scanner;
public class chengjizuida {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
String s = sc.next();
s = s.trim();
sc.close();
// String ss = "0123456789";
// String sss=ss.substring(0,1);
// System.out.println(sss);
// System.out.println(Integer.parseInt("123456"));
int[][] num = new int[s.length() + 2][s.length() + 2];
for (int i = 1; i < s.length() + 1; i++) {
for (int j = i + 1; j < s.length() + 2; j++) {
String str = s.substring(i - 1, j - 1);
// str = str.trim();
// System.out.println(str);
int a=0;
try {
a = Integer.valueOf(str);
} catch (Exception e) {
// TODO: handle exception
a = 0;
}
num[i][j-1] = a;
// System.out.println(a);
// System.out.println(num[i][j]);
// System.out.println(str);
}
}
int[][] dp = new int[n + 1][k + 1];
for (int i = 0; i < n+1; i++) {
for (int j = 0; j < k+1; j++) {
dp[i][j]=1;
}
dp[i][0]=num[i][1];
}
for (int j = 1; j <= k; j++)
// 乘号个数
for (int i = 1+j; i <= n; i++)
// 前i个数
for (int l = j; l <= i; l++)
// 最后一个乘号放在第k个数后第k+1个数前
dp[i][j] = Math.max(dp[i][j], dp[l][j-1] * num[l + 1][i]);
System.out.println(dp[n][k]);
}
}