Create a map whose key are the elements of the array and values are lists of the position they appear in (your example would become {0: [0, 2, 4, 6], 8: [3], 4: [1], 32: [5]}). These groups form unrelated problems. One way finding the answer is to see them as maximum flow problems where each index form two vertices.
Each index is connected to all the indexes following it and all the indexes preceding it are connected to it. The weight of the edges is the distance of the elements in the array, i.e. the difference between the two indexes. All the "source" vertices are connected to an infinite source and all the "destination" vertices are connected to a sink.
The sum of the values of the maximum flows for all the problems is the sum you are looking for.  

As an example, let's use the indexes for the item 0:

           +-------- 0 ----------+      0 --------+
           |                     |                |
           |                     |                |
Source ----+-------- 2 -------+  +----&gt; 2 --------+---- Sink
           |                  |  |                |
           |                  |  |                |
           +-------- 4 ----+  +--+----&gt; 4 --------+
           |               |  |  |                |
           |               |  |  |                |
           +-------- 6     +--+--+----&gt; 6 --------+


Weight of arc (u, v) = v - u


Note that the number of edges grows as V^2 because every edge corresponds to a difference you have to do in order to compute the result, which are between each index and all its successors. This makes the complexity of the solution O(n^3) which is even worse than the naive algorithm, it's just a nice way of viewing things from another perspective :)

Equal elements in SubArray
As someone whose still new to agile, I'm not sure I completely understand the relationship or difference between a user story, feature, and epic.

According to this question, a feature is a collection of stories.  One of the answers suggest that a feature is actually an epic.  

So are features and epics considered the same thing, which is basically a collection of related user stories?

Our project manager insists that there's a hierarchical structure:

Epic -> Features -> User stories

... basically all user stories must fall within this structure.  Therefore all user stories must fall under an umbrella feature and all features must fall under an epic.

To me, that sounds awkward.  Can someone please clarify how user stories, features, and epics are related?  Or is there an article that clearly outlines the differences?
