Initialize dp array with - 1
var dp = Array ( MAX ) . fill ( ) . map ( ( ) => Array ( MAX ) . fill ( 0 ) ) ; for ( var i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; } document . write ( maximizeSum ( a , n , 0 , k , dp ) ) ;