Stores overlapping subproblems
int [ , ] dp = new int [ N + 1 , total ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < total ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( smallestLeft ( arr , total , 0 , N , dp ) ) ; }