If we made the ( n - 1 ) th element equal to j we required dp ( n - 1 , j ) operations We choose the minimum among all possible dp ( n - 1 , j ) where j goes from small to large
int ans = INT_MAX ; for ( int j = small ; j <= large ; j ++ ) { ans = min ( ans , dp [ n - 1 ] [ j ] ) ; } return ans ; }