Interval DP-- energy necklace

E. energy necklace

Memory limit: 128 MiB time limit: 1000 ms standard output
Question Types: traditional evaluation methods: text comparison

Title Description

On the planet Mars, Mars every person be worn with a bunch of energy necklace. There are N pieces of beads on the necklace energy. Energy beads have a head and tail mark labeled beads, these marks correspond to a positive integer. And, for two adjacent beads, the beads before the end of a marker must be equal to the first mark after a bead. Only in this way, the suction cup (sucker is an organ Mars human energy absorption) effect, the two beads to polymerize into a bead, while the release of energy may be absorbed by the chuck. If a former head energy labeled beads m, postamble is r, the energy of a head of beads labeled r, tail marks is n, then after polymerization energy released (Mars units), beads newly generated head marked m, tail marks is n.

When needed, Mars people with a sucker clamping two adjacent beads, obtained by polymerizing energy, until the left until the next bead necklace. Obviously, the total energy of a different order of polymerization get is different, you design a polymerization order to make a necklace out of a maximum total energy released.

For example: Let N = 4,4 beads labeled head and tail were labeled (2,3) (3,5) (5,10) (10,2). We use the symbol ⊕ two beads polymerization operation, (j⊕k) represents the energy of j, k two beads after polymerization released. After the polymerization energy released by the first two beads is 4,1:

(4⊕1)=1023=60。

This necklace can be a total energy of the order of polymerization released optimal value for the

((4⊕1)⊕2)⊕3)=1023+1035+10510=710。

Input Format

The first line of the input file energy.in is a positive integer N (4≤N≤100), represents the number of beads on the necklace. The second line is separated by a space of N positive integers, all numbers not more than 1,000. The i-th tag for the first bead of the i-th (1≤i≤N), when i <N <span>, marking the end of the bead should be equal to i i + 1-head mark of beads. N-labeled beads should be equal to the head end of the first bead is labeled. As for the order of the beads, so you can determine: the necklace on the table, do not appear cross, beads optionally specified first, then determine the order in a clockwise direction other beads.

Output Format

Output file energy.out only one line, it is a positive integer E (E≤2.1 * 109), an optimum total energy of the order of polymerization released.

Sample

Sample input

4
2 3 5 10

Sample Output

710

 

 

 

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<stack>
 5 using namespace std;
 6 int n,nuse,dod,dp[201][201]/*i--j最优解*/,wei[201]/*从1至i*/,maxi,tou[201],maxmax;
 7 int main()
 8 {
 9     cin>>n;
10     nuse=2*n-1;
11     for( Int I = . 1 ; I <= n-; I ++ )
 12 is      {
 13 is          CIN >> TOU [I];
 14          Wei [I- . 1 ] = TOU [I];
 15      }
 16      Wei [ 0 ] = 0 ;
 . 17      Wei [ n-] = TOU [ . 1 ];
 18 is      for ( int I = n-+ . 1 ; I <= 2 * n-; I ++ )
 . 19      {
 20 is          TOU [I] = TOU [I- n-];
 21 is          Wei [I] = Wei [I - n-];
 22 is      }
23     for(int len=1;len<n;len++)
24     {
25         for(int i=1;i<=2*n-len;i++)
26         {
27             int j=len+i;
28             for(int k=i;k<j;k++)
29             {
30                 dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]+tou[i]*wei[k]*wei[j]);
31             }
32         }
33     }
34     for(int i=1;i<=n;i++)
35     {
36         if(dp[i][i+n-1]>maxi)
37         {
38             maxi=dp[i][i+n-1];
39         }
40     }
41     cout<<maxi; 
42 }

 

Guess you like

Origin www.cnblogs.com/lihaolin/p/11276775.html