DP in haskell (Immutable array is not very efficient)