Iterate over all possible selections from current row
for x in range ( 1 , min ( j , m ) + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - x ] + prefsum [ i ] [ x - 1 ] ) NEW_LINE DEDENT