A-Nosy
Chunxi is very nosy. He takes time to help some students every day. chunxi is very rigid and will not help his later classmates out of fairness.
Now you have N A student needs his help. Although he really wants to help everyone for a day, he has limited energy, so he decided M Come to help them.
Based on the importance of things, chunxi helps different students have different happy values, and the total happy values obtained by chunxi are the product of the happy values obtained every day.
Now N And M And help you get the greatest value of happiness in spring.
Input
The first behavior is an integer. T Indicates the number of data groups.
Data in each group, two integers in the first row N, m . Indicates the number of students who need help, and the number of days. (1 ≤ m ≤ min (n, 10), 1 ≤ n ≤ 20)
Second act N An integer that indicates the happiness value that helps this student obtain. Each happiness value is not greater 5 .
Output
Each group of data outputs a row with an integer representing the maximum happy value.
Sample input and output
| Sample Input |
Sample Output |
15 33 2 1 4 5 |
125 |
Solution: dp [j] [I] indicates the maximum value of the product of the sum of the first j numbers divided into I. In the test case (3 + 2) * (1 + 4) * 5 = 125 triple loop. Dp [j] [I] = max (dp [j] [I], dp [k] [I-1] * (sum [j]-sum [k]); key code:
For (int I = 1; I <= m; I ++) for (int j = n; j >= I; j --) for (int k = I-1; k
Code:
# Include
# Include
Using namespace std; const int maxn = 25; int dp [maxn] [maxn]; int num [maxn], sum [maxn]; int t, n, m; int main () {cin> t; while (t --) {cin> n> m; for (int I = 0; I <= n; I ++) for (int j = 0; j <= m; j ++) dp [I] [j] = 1; sum [0] = 0; for (int I = 1; I <= n; I ++) {cin> num [I]; sum [I] = sum [I-1] + num [I];} for (int I = 1; I <= m; I ++) for (int j = n; j >= I; j --) for (int k = I-1; k
The one-dimensional data written at the beginning is always WA. I don't know why. Incorrect one-dimensional code:
#include
#include
#include using namespace std;int sum[25];int num[25];int dp[25];int t;int n,m;int main(){ cin>>t; while(t--) { sum[0]=0; dp[0]=1; cin>>n>>m; for(int i=1;i<=n;i++) { cin>>num[i]; sum[i]=sum[i-1]+num[i]; dp[i]=1; } for(int i=1;i<=m;i++) { for(int j=n;j>=i;j--) { for(int k=i-1;k