A Dynamic Programming algorithm for partitioning data arrays