Problem : Isotope
Scientist recently found a new element X, which can have different isotopes upto an atomic value of 199. Speciality of element X is that when two atoms fuse they produce energy multiple of their atomic value and forms an atom with atomic value of their multiple modulus 199. For Eg: If atom1 with value 56 and atom2 with value 61 fuse. They produce energy of 3416 KJ (56 * 61) Resulting atom will have atomic value (56*61) mod 199 = 33 Scientists created a nuclear reactor to create energy by this method. Everyday they get several atoms of X from supplier in a particular sequence. Since this element highly radioactive they cant risk by changing its sequence. So each atom can fuse only with another atom nearby. Nevertheless scientists can choose order of fusion thereby maximizing total energy produced. Now, for given sequence of atomic values, output maximum energy that can be produced.
Input Format Input starts with a number specifying number of inputs(n) followed by n space separated integers specifying atomic values of n atoms.
Sample Input/ Output Input: 3 56 61 2
Output: 6954
I believe there must exist a method better than simply brute forcing, maybe dynamic programming. Can anyone give an idea how to proceed for the correct solution?