Iterate over all possible selections from current row
for ( int x = 1 ; x <= Math . Min ( j , m ) ; x ++ ) { dp [ i , j ] = Math . Max ( dp [ i , j ] , dp [ i - 1 , j - x ] + prefsum [ i , x - 1 ] ) ; } } }