Title Description
On Mars planet, each Mars person carries a string of energy necklaces. There are n energy beads on the necklace. The energy bead is a bead with a head mark and a tail marker, which corresponds to a positive integer. Also, for two adjacent beads, the tail mark of the previous bead must be equal to the head mark of the latter bead. Because this is the only way that the two beads can be aggregated into a bead and release the energy that can be absorbed by the suction cup, by means of the suction cup, which is an organ of the Mars Human Energy absorption. If the head of the previous energy bead is marked M, the tail mark is R, the head of the latter energy bead is labeled R, and the tail mark is n, then the energy released by the aggregation is M*r*n (Mars Unit), the newly generated bead has a head labeled M, and the tail mark is n.
When needed, the Mars man uses a suction cup to clamp the two adjacent beads, and the energy is obtained by polymerization until only one bead is left on the necklace. Obviously, the total energy from different aggregation sequences is different, please design an aggregation order so that the total energy released by a string of necklaces is the largest.
For example: The head mark and tail mark of the n=4,4 beads are (2,3) (3,5) (5,10) (10,2). We use a tick to indicate the aggregation of two beads, (j⊕k) represents the energy released by the aggregation of the j,k two beads. Then the energy released by the 4th and 12 beads is:
(4⊕1) =10*2*3=60.
This string necklace can be obtained by an aggregation order of the optimal values, releasing the total energy of
((4⊕1) ⊕2) ⊕3) =10*2*3+10*3*5+10*5*10=710.
Input/output format
Input format:
The first line of input is a positive integer N (4≤n≤100) that represents the number of beads on the necklace. The second line is n spaces separated by a positive integer, all the numbers are not more than 1000. The number of I is the head mark (1≤i≤n) of the first bead, when i<n< span>, the tail mark of the first bead shall be equal to the head mark of the i+1 bead. The tail mark of the nth bead should be equal to the head mark of the 1th bead.
As for the order of the beads, you can be sure: put the necklace on the table, do not cross, arbitrarily specify the first bead, and then clockwise to determine the order of the other beads.
Output format:
The output is only one row, which is a positive integer E (e≤2.1*10^9), which is the total energy released for an optimal aggregation order.
Input and Output Sample input example # #:
42 3 5 10
Sample # # of output:
710
Description
NOIP 2006 Raising the first question of the group
Interval DP
1#include <cstdio>2#include <iostream>3 using namespacestd;4 inta[20000]={0};5 intt[20000]={0};6 intf[ the][ the]={0};7 intMain () {8 intn,i,j;9Cin>>N;Ten for(i=1; i<=n;i++){ Onescanf"%d",&a[i]); Aa[n+i]=A[i]; - } - for(i=1; i<=2*n-1; i++) t[i]=a[i+1]; thet[2*n]=a[1]; - for(intTime=1; time<=n-1; time++) - for(i=1; i<=2*n-time;i++)//starting point - { +j=i+Time ; - intmax=0; + for(intk=i;k<=j-1; k++){ A intpower=f[i][k]+f[k+1][j]+a[i]*t[k]*T[j]; at if(Max<power) max=Power; - } -f[i][j]=Max; - } - intmax=0; - for(i=1; i<=n;i++)if(max<f[i][i+n-1]) max=f[i][i+n-1]; inprintf"%d", max); - return 0; to}
[NOIP2006] Improved group Rokua P1063 Energy Necklace