Given an integer **K** and an array **arr[]** which denotes the amount that can be stolen, the task is to choose a subset of items such that their total value is less than K to get the bonus amount.

Bonus Amount:The bonus amount will be the maximum value that can be stolen from the set of items for each item stolen.Bonus Amount = (Max of arr[]) * (No of Items stolen)

**Examples:**

Input:arr[] = {1, 2, 3, 4, 5}, K = 7Output:22Explanation:

Maximum value that can stolen is – 5.

If the items were stolen are 1, 2, and 4. Then the total sum of stolen value will be less than K.

Therefore, Total Profit

=> Each Item value + Maximum value that can be stolen

=> 1 + 5 + 2 + 5 + 4 + 5 = 22Input:arr[] = {5, 2, 7, 3}, K = 6Output:19Explanation:

Maximum value that can stolen is – 7

If the items stolen are 2 and 3. Then the total sum of stolen value will be less than K.

Therefore, Total Profit

=> Each Item value + Maximum value that can be stolen

=> 2 + 7 + 3 + 7 = 19

**Approach:** The idea is to use permutation & combinations to choose the elements such that their total sum is less than K. Therefore, considering every possible will result in the maximum profit that can be possible.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum stolen value such that` `// total stolen value is less than K` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum` `// profit from the given values` `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;` ` ` ` ` `// Iterating over every ` ` ` `// possible permutation` ` ` `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;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 4, K = 6;` ` ` `vector<` `int` `> values{5, 2, 7, 3};` ` ` ` ` `// Function Call` ` ` `cout << maxProfit(values, N, K);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum stolen value such that ` `// total stolen value is less than K ` `import` `java.util.*;` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// profit from the given values ` `static` `int` `maxProfit(` `int` `[]value, ` ` ` `int` `N, ` `int` `K) ` `{ ` ` ` `Arrays.sort(value); ` ` ` `int` `maxval = value[N - ` `1` `]; ` ` ` `int` `maxProfit = ` `0` `; ` ` ` `int` `curr_val; ` ` ` ` ` `// Iterating over every ` ` ` `// possible permutation ` ` ` `do` `{ ` ` ` `curr_val = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `curr_val += value[i]; ` ` ` `if` `(curr_val <= K) { ` ` ` `maxProfit = Math.max(curr_val + ` ` ` `maxval * (i + ` `1` `), ` ` ` `maxProfit); ` ` ` `} ` ` ` `} ` ` ` `} ` `while` `(next_permutation(value)); ` ` ` `return` `maxProfit; ` `} ` `static` `boolean` `next_permutation(` `int` `[] p) {` ` ` `for` `(` `int` `a = p.length - ` `2` `; a >= ` `0` `; --a)` ` ` `if` `(p[a] < p[a + ` `1` `])` ` ` `for` `(` `int` `b = p.length - ` `1` `;; --b)` ` ` `if` `(p[b] > p[a]) {` ` ` `int` `t = p[a];` ` ` `p[a] = p[b];` ` ` `p[b] = t;` ` ` `for` `(++a, b = p.length - ` `1` `; a < b; ++a, --b) {` ` ` `t = p[a];` ` ` `p[a] = p[b];` ` ` `p[b] = t;` ` ` `}` ` ` `return` `true` `;` ` ` `}` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `, K = ` `6` `; ` ` ` `int` `[]values = {` `5` `, ` `2` `, ` `7` `, ` `3` `}; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(maxProfit(values, N, K)); ` `}` `} ` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

19

**Time Complexity:** O(N^{2}) **Auxiliary space:** O(N)

## Recommended Posts:

- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Maximum value of B less than A such that A ^ B = A + B
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Maximize the total profit of all the persons
- Find three integers less than or equal to N such that their LCM is maximum
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum size of square such that all submatrices of that size have sum less than K
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count of elements having Euler's Totient value one less than itself
- Find unique pairs such that each element is less than or equal to N
- Sum of elements of all partitions of number such that no element is less than K
- Print a number strictly less than a given number such that all its digits are distinct.
- Find largest factor of N such that N/F is less than K
- Largest number M less than N such that XOR of M and N is even
- Get maximum items when other items of total cost of an item are free
- Total ways of choosing X men and Y women from a total of M men and W women
- Find maximum product of digits among numbers less than or equal to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.