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 (in the unit of Mars). 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.