[Clear thinking] energy necklace

Source: Internet
Author: User

[Description]

On the Mars planet, every mars person carries a pair of energy necklaces with him. There are n energy beads on the necklace. An energy beads are beads with header and tail tags, which correspond to a positive integer. In addition, for the adjacent two beads, the tail mark of the previous Bead must be equal to the head mark of the next bead. Because only in this way, the two beads can be aggregated into one bead through the suction cup (which is an organ that Mars humans absorb energy, at the same time, it releases the energy that can be absorbed by the suction cup. If the head of the previous energy bead is marked as m, the tail is marked as R, the head of the next energy bead is marked as R, and the tail is marked as N, then, the energy released after aggregation is M * r * n (MARS Unit). The head of the newly generated beads is marked as m, and the tail is marked as N.
When needed, the Mars user uses a suction disc to hold the adjacent two beads and aggregate them to obtain energy until there is only one bead left on the necklace. Obviously, the total energy produced by different aggregation sequences is different. Please design an aggregation sequence to maximize the total energy released by a chain of necklaces.
For example, set the head and tail labels of N = beads ). We use the mark "yellow" to indicate the aggregation operation of the two beads. (J yellow K) indicates the energy released after the aggregation of the J and K beads. The energy released after the aggregation of 4th and 1 beads is:
(4 rows 1) = 10*2*3 = 60.
The total energy released by this chain of necklaces is
(4 rows 1) rows 2) rows 3) = 10*2*3 + 10*3*5 + 10*5*10 = 710.

[Input format]

The first line of the input file is a positive integer N (4 ≤ n ≤ 100), indicating the number of beads on the necklace. The second row is n positive integers separated by spaces. The number of All integers cannot exceed 1000. The number of I is the head mark (1 ≤ I ≤ n) of the I beads. When I <n <span>, the tail mark of beads I should be equal to the head mark of beads I + 1. The tail mark of N beads should be equal to the head mark of 1st beads.
As for the order of beads, you can determine the order of the beads by placing the necklace on the desktop. Do not cross the beads, specify the First beads at will, and then determine the order of other beads clockwise.

[Output format]

The output file contains only one row, which is a positive integer e (e ≤ 2.1*10 ^ 9) and the total energy released for an optimal aggregation order.

[Example input]

4

2 3 5 10

[Sample output]

710

[Analysis]

Typical interval dynamic planning.

First, break the ring into a chain, and then the process of moving back.

F [I] [J] = max {f [I] [k] + F [k + 1] [J] + left [I] * right [k] * right [j]}, I <= k <j.

Easy to understand.

 

# Include <stdio. h> # define maxn 300 long f [maxn] [maxn], left [maxn], right [maxn]; int N, ans; void dp (int I, Int J) {If (f [I] [J]) | (I = J) return; For (int K = I; k <j; ++ K) {dp (I, K); DP (k + 1, J ); if (F [I] [J] <F [I] [k] + F [k + 1] [J] + left [I] * right [k] * right [j]) f [I] [J] = f [I] [k] + F [k + 1] [J] + left [I] * right [k] * right [J] ;}} int main () {scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) {scanf ("% d ", & left [I]); left [I + N] = left [I];} For (INT I = 1; I <2 * n; ++ I) right [I] = left [I + 1]; right [2 * n] = left [1]; for (INT I = 1; I <= N; ++ I) {Int J = I + n-1; DP (I, j); If (F [I] [J]> ans) ans = f [I] [J];} printf ("% d \ n", ANS); Return 0 ;}

Related Article

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.