The formula is largest
Problem Description
Title destination time very simple, given N digital, do not change their relative positions, in the middle of filling with K for multiplication number and NK-1 plus sign, (plus whatever parentheses) the end result is zoomed as possible. Because the multiplication and plus co ⼀ is the N-1, so there are exactly ⼀ symbols between every two adjacent numbers.
For example: N = 5, K = 2, five numbers are 1 , 2 , 3 , 4 , 5 ,
Addition can be: 1 * 2 * (3 + 4 + 5) = 241 * (3 + 2) * (4 + 5) = 45 (1 + 2 * 3) * (4 + 5) = 45 ......
START input format
Entering text total ⼆ ⾏ member, the first frame ⾏ two integers separated by spaces represents N and K , where ( 2 <= N <= 15, 0 <= K <= N-. 1).
Second shot ⾏ to N th digital Use separated by a space (each number in the 0 to . 9 between).
Output Format
Output files only ⼀ ⾏ Contains either integers, it indicates that the requested result is largest
Sample lose START
5 2
1 2 3 4 5
Sample Output
120
Sample Description
(1+2+3)*4*5=120
Analysis: Using dynamic planning solutions. Provided SUM [i] is the sum of the first number of i, the sum from j to k is the sum [k] -sum [j-1].
Set DP [i] [j] represents the number of i in outcome before the j-th from the largest number of multiplication. To know the dp [i] [j], you can try before ⾯ ⼀ second shot number from
⾯ until the last number of ⼀ added sequentially multiplication sign, the result is largest conservation ⾄ dp [i] [j] in.
It can be obtained for the state transition process ⽅: dp [i] [j] = max (dp [i] [j], dp [l-1] [j-1] * ([i] sum -sum [l-1 ])); Insert after l is multiplied by two numbers
Coordinate ⼀ digits.
#include <iostream>
using namespace std;
#define max(a, b) a > b ? a : b;
long long int dp[16][16];
int sum[16];
int main() {
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
int temp;
cin >> temp;
sum[i] = sum[i-1] + temp;
}
for(int i = 1; i <= n; i++) {
dp[i][0] = sum[i];
}
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= i-1 && j <= k; j++) {
for(int l = 2; l <= n; l++) {
dp[i][j] = max(dp[i][j], dp[l-1][j-1] * (sum[i] -sum[l-1]));
}
}
}
cout << dp[n][k];
return 0;
}