Initialize dp array with - 1
int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; } Console . WriteLine ( maximizeSum ( a , n , 0 , k , dp ) ) ; } }