For a set S={1,2,3}, I perform all possible contiguous partitioning on the set to get {{1},{2},{3}},{{1,2},{3}},{{1},{2,3}},{{1,2,3}}. Lets call each partition a piece. I need to efficiently calculate sum of the following term over all the partitioning.
(sum of all numbers in the piece)*(length of the piece)
Example: If set is A={1,3,6} then the pieces are:
B = {{1},{3},{6}} and value = (1)*1 +(3)*1 +(6)*1 = 10
B = {{1,3},{6}} and value = (1+3)*2+(6)*1 = 14
B = {{1},{3,6}} and value = (1)*1 +(3+6)*2 = 19
B = {{1,3,6}} and value = (1+3+6)*3 = 30
Total sum of values = 10+14+19+30 = 73.
I am not able to think of any better way other than simulating the required procedure. I need only the total sum and no intermediate values or partitioning. How can this be done efficiently?
P.S> This is not a homework question. I am a programming enthusiast curious to learn the theorems or algorithms related to above concept. I think this is more mathematical, so have asked here and not on stackoverflow.