Maximum profit such that total stolen value is less than K to get bonus | C ++ implementation to find the maximum stolen value such that total stolen value is less than K ; Function to find the maximum profit from the given values ; Iterating over every possible permutation ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( vector < int > value , int N , int K ) { sort ( value . begin ( ) , value . end ( ) ) ; int maxval = value [ N - 1 ] ; int maxProfit = 0 ; int curr_val ; do { curr_val = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_val += value [ i ] ; if ( curr_val <= K ) { maxProfit = max ( curr_val + maxval * ( i + 1 ) , maxProfit ) ; } } } while ( next_permutation ( value . begin ( ) , value . end ( ) ) ) ; return maxProfit ; } int main ( ) { int N = 4 , K = 6 ; vector < int > values { 5 , 2 , 7 , 3 } ; cout << maxProfit ( values , N , K ) ; }