fill the table in bottom - up fashion
for ( int i = 1 ; i <= k ; i ++ ) { int prevDiff = INT_MIN ; for ( int j = 1 ; j < n ; j ++ ) { prevDiff = max ( prevDiff , profit [ i - 1 ] [ j - 1 ] - price [ j - 1 ] ) ; profit [ i ] [ j ] = max ( profit [ i ] [ j - 1 ] , price [ j ] + prevDiff ) ; } } return profit [ k ] [ n - 1 ] ; }