Problem H: Heaps
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 48 Solved: 9[][][]Description
Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.
Zuosige knows there is a typical problem called Merging Stones. In the problem, you have N heaps of stones and you are going to merging them into one heap. The only restriction is that you can only merging adjacent heaps and the cost of a merging operation is the total number of stones in the two heaps merged. Finally, you are asked to answer the minimum cost to accomplish the merging.
However, Zuosige think this problem is too simple, so he changes it. In his problem, the cost of a merging is a polynomial function of the total number of stones in those two heaps and you are asked to answer the minimum cost.
Input
The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines.In the first line, there are an integer N (1<=N<=1000).In the second line, there are N integers. The i-th integer si (1<=si<=40) indicating the number of stones in the i-th heap.In the third line, there are an integer m (1<=m<=4).In the forth line, there are m+1 integers a0, … , am. The polynomial function is P(x)= (a0+a1*x+a2*x2+…+am*xm). (1<=ai<=5)Output
For each test case, output an integer indicating the answer.
Sample Input
153 1 8 9 9 22 1 2
Sample Output
2840
HINT
转载请注明出处:
题目链接:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include