# Maximize the product of sum and least power by choosing at most K elements from given value and power Arrays

• Difficulty Level : Hard
• Last Updated : 19 Nov, 2021

Given two arrays arr[] and powr[] of size N and an integer K. Every element arr[i] has its respective power powr[i]. The task is to maximize the value of the given function by choosing at most K elements from the array. The function is defined as :

f(N) = (arr[i1] + arr[i2] + arr[i3]+…..arr[in]) * min(powr[i1], powr[i2], powr[i3], …..powr[in]) where, arr[i1], arr[i2], arr[i3], …..arr[in] are the chosen elements.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Examples:

Input: arr[] = {11, 10, 7, 6, 9}, powr[] = {3, 2, 4, 1, 1}, K = 2, N = 5
Output: 54
Explanation: Choose elements at indices {0, 2} so, f(N) = (11 + 7) * min(3, 4) = 18 * 3 = 54, which is the maximum possible value that can be achieved by choosing at most 2 elements.

Input: arr[] = {5, 12, 11, 9}, powr[] = {2, 1, 10, 1}, K = 3, N = 4
Output: 110

Approach: The idea is to consider every ith element as the minimum power, for this, sort the elements in descending order of power, so the first element will be considered to have the highest power. All times try to maintain a list of elements of size at most K. This list will contain at most K elements with the largest one, not including the current ith element. If already have a list of size K, then remove the element with the smallest length so, size becomes K – 1, then include the current element into the list, size becomes K, and update res with maximum one. In the end, return res which is the answer.

• Initialize a vector of pairs v[] of size N to store elements along with their power.
• Iterate over the range [0, N) using the variable i and perform the following tasks:
• Assign the values power[i] and arr[i] as the first and second values of the array v[].
• Sort the array v[] in ascending order.
• Initialize the variables res and sum as 0 to store the result and the sum.
• Initialize a set of pairs s[].
• Iterate over the range [N-1, 0] using the variable i and perform the following tasks:
• Insert the pair {v[i].second, i} into the set s[].
• Add the value of v[i].second to the variable sum.
• If s.size() is greater than K then perform the following tasks:
• Initialize the variable it as the first element of the set s[].
• Reduce the value it.first from the variable sum.
• Remove the variable it from the set s[].
• Set the value of res as the maximum of res or sum*v[i].first.
• After performing the above steps, print the value of res as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to maximize the value of the``// function by choosing at most K elements``// from the given array``int` `maximumValue(``int` `arr[], ``int` `powr[],``                 ``int` `K, ``int` `N)``{` `    ``// Vector to store the power of elements``    ``// along with the elements in pair``    ``vector > v(N);` `    ``// In a pair, the first position contains``    ``// the power and the second position``    ``// contains the element``    ``for` `(``int` `i = 0; i < N; i++) {``        ``v[i].first = powr[i];``        ``v[i].second = arr[i];``    ``}` `    ``// Sort the vector according to the``    ``// power of the elements``    ``sort(v.begin(), v.end());` `    ``// Variable to store the answer``    ``int` `res = 0;` `    ``// Variable to store the sum of``    ``// elements``    ``int` `sum = 0;` `    ``// Create a set of pairs``    ``set > s;` `    ``// Traverse the vector in reverse order``    ``for` `(``int` `i = N - 1; i >= 0; i--) {` `        ``// Insert the element along with the``        ``// index in pair``        ``s.insert(make_pair(v[i].second, i));``        ``sum += v[i].second;` `        ``// If size of set exceeds K, then``        ``// delete the first pair in the set``        ``// and update the sum by excluding``        ``// the elements value from it``        ``if` `(s.size() > K) {``            ``auto` `it = s.begin();``            ``sum -= it->first;``            ``s.erase(it);``        ``}` `        ``// Store the maximum of all sum``        ``// multiplied by the element's``        ``// power``        ``res = max(res, sum * v[i].first);``    ``}` `    ``// Return res``    ``return` `res;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 11, 10, 7, 6, 9 };``    ``int` `powr[] = { 3, 2, 4, 1, 1 };``    ``int` `K = 2;` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << maximumValue(arr, powr, K, N);` `    ``return` `0;``}`

## Python3

 `# Python 3 program for the above approach` `# Function to maximize the value of the``# function by choosing at most K elements``# from the given array``def` `maximumValue(arr, powr, K, N):` `    ``# Vector to store the power of elements``    ``# along with the elements in pair``    ``v ``=` `[[``0` `for` `x ``in` `range``(``2``)] ``for` `y ``in` `range``(N)]` `    ``# In a pair, the first position contains``    ``# the power and the second position``    ``# contains the element``    ``for` `i ``in` `range``(N):``        ``v[i][``0``] ``=` `powr[i]``        ``v[i][``1``] ``=` `arr[i]` `    ``# Sort the vector according to the``    ``# power of the elements``    ``v.sort()` `    ``# Variable to store the answer``    ``res ``=` `0` `    ``# Variable to store the sum of``    ``# elements``    ``sum` `=` `0` `    ``# Create a set of pairs``    ``s ``=` `set``([])` `    ``# Traverse the vector in reverse order``    ``for` `i ``in` `range``(N ``-` `1``, ``-``1``, ``-``1``):` `        ``# Insert the element along with the``        ``# index in pair``        ``s.add((v[i][``1``], i))``        ``sum` `+``=` `v[i][``1``]` `        ``# If size of set exceeds K, then``        ``# delete the first pair in the set``        ``# and update the sum by excluding``        ``# the elements value from it``        ``if` `(``len``(s) > K):` `            ``sum` `-``=` `list``(s)[``0``][``0``]``            ``list``(s).remove(``list``(s)[``0``])` `        ``# Store the maximum of all sum``        ``# multiplied by the element's``        ``# power``        ``res ``=` `max``(res, ``sum` `*` `v[i][``0``])` `    ``# Return res``    ``return` `res` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``11``, ``10``, ``7``, ``6``, ``9``]``    ``powr ``=` `[``3``, ``2``, ``4``, ``1``, ``1``]``    ``K ``=` `2` `    ``N ``=` `len``(arr)``    ``print``(maximumValue(arr, powr, K, N))` `     ``# This code is contributed by ukasp.`

## Javascript

 ``
Output
`54`

Time Complexity: O(NlogN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up