Cdoj 1131 Male God's Gift range DP

Source: Internet
Author: User

The gift of the god of men

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://acm.uestc.edu.cn/#/problem/show/1131

Descriptionlweb Seniors are recognized as training team in the male god. One day he was going to give a gift to Meimei's school sister.

Lweb Seniors can be magical, yo. In order to prepare a gift, the male God to process n copies of material. Each time only the adjacent materials can be processed.

When the male god processing two magic value for a A, a, a material, the male God consumes a*b strength, at the same time in this place to synthesize the magic value (a+b) 0 of the material.

In order to save energy, the man finished his gift. You want to find the smart you help him calculate the minimum amount of energy he is going to spend.

Input

The first line is an integer t, which indicates the number of gifts to be prepared by the male God. The next T-Group data has two rows of data, and the first line has an integer n, which represents the number of materials for this gift (1<=n<=100). The next line has n integers a (0<=a<100), which represents the magic value of the part I material for this gift.

Output

Each set of data is output, indicating the minimum amount of physical energy a man will need to make this gift.

Sample Input

2
2
18 19
3
40 60 20

Sample Output

342
2400

HINT

For example 2:

The first processing materials 40 and 60, to obtain 0 of the material, consumption of 40∗60 physical strength, a total consumption of 2400 physical strength;

Reprocessing materials 0 and 20, get 20 of the material, consumes 0∗20 physical strength, total consumption of 2400 physical strength.

Test instructions

Exercises

Interval DP, similar to the problem of a stone merge, is good every time the merged points are enumerated.

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];Const intinf=0x7fffffff;//нчоч╢с/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10;    while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;}  while(x>0) ch[++num]=x%Ten, x/=Ten;  while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************ll dp[101][101];ll a[101];ll sum[101];intMain () {intt=read ();  while(t--) {memset (DP,0,sizeof(DP)); memset (SUM,0,sizeof(sum)); intn=read ();  for(intI=1; i<=n;i++) {A[i]=read (); Sum[i]=a[i]+sum[i-1]; }         for(intR=2; r<=n;r++)        {              for(inti=r-1; i>=1; i--)            {                 for(intj=i;j<=r;j++) {ll kiss= ((sum[j]-sum[i-1])% -) * ((SUM[R]-SUM[J))% -); if(dp[i][r]==0) Dp[i][r]=dp[i][j]+dp[j+1][r]+Kiss; ElseDp[i][r]=min (dp[i][r],dp[i][j]+dp[j+1][r]+kiss); } }} cout<<dp[1][n]<<Endl; }}

Cdoj 1131 Male God's gift interval DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.