Congratulations. You have just gotten a job at Lockheed-Boeing-BAE-Aerospace, a new congolomerate corporation that makes airplanes. They need to simulate the flight of a new design, and will need to multiply a lot of matrices together. Your job is to figure out the right order, so that this is done as quickly as possible. By the way, there are about 30 other people who just got hired, and they have the same task as you -- and if you don't have as good of a solution as the others, you'll be fired by this time next week. Now only if you could remember that dynamic programming trick from the book....

Input will be the sizes of the matrices (in pairs of numbers). There will always be pairs, and the matrix sizes will be correct for multiplying them together. One possible input is:

30 35
35 15
15 5
5 10
10 20
20 25

Your program should read from standard input, and print out the following
15125
(A0 (A1 A2))((A3 A4) A5))

This is the total number of multiplications, followed by the parenthesization of the actual matrices.

Some simple input files: