Function to generate the cost using Prefix Sum Array technique
public static int [ ] preprocess ( int [ ] a , int n ) { int p [ ] = new int [ n ] ; p [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i ] = p [ i - 1 ] + a [ i ] ; return p ; }