Define the dp table globally
var dp = new Array ( 505 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 505 ) . fill ( - 1 ) ; } var choose = new Array ( 505 ) ; for ( var i = 0 ; i < choose . length ; i ++ ) { choose [ i ] = new Array ( 505 ) . fill ( 0 ) ; }