# Maximize product of sum of speeds of K workers and their minimum efficiency

• Last Updated : 19 May, 2022

Given an integer N, representing the number of workers, an array speed[ ], where speed[i] represents the speed of the ith worker, and an array efficiency[ ], where efficiency[i] represents the efficiency of the ith worker, and an integer K, the task is to select K workers such that the ( Sum of speeds of all the workers ) * ( Minimum efficiency of among K workers ) is maximum possible.

Examples:

Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 2
Output: 60
Explanation:
Selecting 2nd worker (Speed = 10 and Efficiency = 4) and 5th worker ( Speed = 5 and Efficiency = 7).
Therefore, the maximum sum possible = (10 + 5) * min(4, 7) = 60

Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 3
Output: 68

Approach: Follow the steps below to solve the problem:

• Initialize a vector of pairs arr[ ] where arr[i] equals {efficiency[i], speed[i]} of size N.
• Sort the arr[ ] in decreasing order of efficiency.
• Initialize a min priority_queue that stores the speed of workers.
• Initialize variables say, SumOfSpeed = 0 and Ans = 0.
• Iterate over arr[ ] and do the following:
• Add arr[i].first to the SumOfSpeed
• Push speed[i] in priority_queue.
• If size of the priority_queue exceeds K, the pop the front of the priority_queue and subtract from SumOfSpeed.
• Update Ans as max of Ans and SumOfSpeed * efficiency[i].
• Finally, return the Ans.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to generate array of pairs``void` `generateArrayofPairs(``int` `n, vector<``int``>& speed,``                          ``vector<``int``>& efficiency,``                          ``vector >& arr)``{` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``arr[i] = { efficiency[i], speed[i] };``    ``}` `    ``sort(arr.rbegin(), arr.rend());``}` `// Function to find the maximum``// product of worker speeds and``// their minimum efficiency``int` `maximizePerformance(vector<``int``>& speed, ``int` `K,``                        ``vector<``int``>& efficiency)``{` `    ``int` `n = speed.size();``    ``vector > arr(n);` `    ``// Function to generate``    ``// sorted array of pairs``    ``generateArrayofPairs(n, speed,``                         ``efficiency, arr);` `    ``// Initialize priority queue``    ``priority_queue<``int``, vector<``int``>,``                   ``greater<``int``> >``        ``pq;` `    ``// Initialize ans and sumofspeed``    ``int` `ans = 0;``    ``int` `SumOfSpeed = 0;` `    ``// Traversing the arr of pairs``    ``for` `(``auto``& it : arr) {` `        ``int` `e = it.first;``        ``int` `s = it.second;` `        ``// Updating sum of speed``        ``SumOfSpeed += s;` `        ``// Pushing in priority queue``        ``pq.push(s);` `        ``// If team consists of more than``        ``// K workers``        ``if` `(pq.size() > K) {` `            ``int` `temp = pq.top();``            ``SumOfSpeed -= temp;``            ``pq.pop();``        ``}` `        ``// Taking the maximum performance``        ``// that can be formed``        ``ans = max(ans, SumOfSpeed * e);``    ``}` `    ``// Finally return the ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``vector<``int``> speed = { 2, 10, 3, 1, 5, 8 };``    ``vector<``int``> efficiency = { 5, 4, 3, 9, 7, 2 };``    ``int` `K = 2;` `    ``// Function Call``    ``cout << maximizePerformance(``        ``speed, K, efficiency);``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `static` `class` `pair``{``    ``int` `first, second;``     ` `    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}  ``}` `// Function to generate array of pairs``static` `void` `generateArrayofPairs(``int` `n, Vector speed,``                          ``Vector efficiency,``                          ``Vector arr)``{` `    ``for` `(``int` `i = ``0``; i < n; i++) {` `        ``arr.insertElementAt(``new` `pair(efficiency.elementAt(i), speed.elementAt(i)), i);``    ``}` `    ``Collections.sort(arr, ``new` `Comparator() {``            ``@Override` `public` `int` `compare(pair p1, pair p2)``            ``{``                  ``if` `(p1.first != p2.first)``                    ``return` `(p2.first - p1.first);``                  ``return` `p2.second - p1.second;``            ``}``        ``});``}` `// Function to find the maximum``// product of worker speeds and``// their minimum efficiency``static` `int` `maximizePerformance(Vector speed, ``int` `K,``                        ``Vector efficiency)``{` `    ``int` `n = speed.size();``    ``Vector arr = ``new` `Vector<>();` `    ``// Function to generate``    ``// sorted array of pairs``    ``generateArrayofPairs(n, speed,``                         ``efficiency, arr);` `    ``// Initialize priority queue``      ``PriorityQueue pq = ``new` `PriorityQueue();` `    ``// Initialize ans and sumofspeed``    ``int` `ans = ``0``;``    ``int` `SumOfSpeed = ``0``;` `    ``// Traversing the arr of pairs``    ``for` `(``int` `i = ``0``; i < arr.size(); i++) {` `        ``int` `e = arr.elementAt(i).first;``        ``int` `s = arr.elementAt(i).second;` `        ``// Updating sum of speed``        ``SumOfSpeed += s;` `        ``// Pushing in priority queue``        ``pq.add(s);` `        ``// If team consists of more than``        ``// K workers``        ``if` `(pq.size() > K) {` `            ``int` `temp = pq.peek();``            ``SumOfSpeed -= temp;``            ``pq.poll();``        ``}` `        ``// Taking the maximum performance``        ``// that can be formed``        ``ans = Math.max(ans, SumOfSpeed * e);``    ``}` `    ``// Finally return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main (String[] args) {``    ` `      ``// Given Input``    ``Vector speed = ``new` `Vector();``    ``speed.add(``2``);``      ``speed.add(``10``);``      ``speed.add(``3``);``      ``speed.add(``1``);``      ``speed.add(``5``);``      ``speed.add(``8``);``  ` `    ``Vector efficiency = ``new` `Vector();``      ``efficiency.add(``5``);``      ``efficiency.add(``4``);``      ``efficiency.add(``3``);``      ``efficiency.add(``9``);``      ``efficiency.add(``7``);``      ``efficiency.add(``2``);``  ` `      ``int` `K = ``2``;` `    ``// Function Call``    ``System.out.println(maximizePerformance(``        ``speed, K, efficiency));``}``}` `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python program for the above approach` `# Function to generate array of pairs``from` `functools ``import` `cmp_to_key` `def` `mycmp1(a, b):``    ``if``(b[``0``] ``=``=` `a[``0``]):``        ``return` `b[``1``] ``-` `a[``1``]``    ``return` `b[``0``] ``-` `a[``0``]` `def` `mycmp2(a,b):``    ``return` `b``-``a` `def` `generateArrayofPairs(n, speed, efficiency, arr):` `    ``for` `i ``in` `range``(n):``        ``arr[i] ``=` `[ efficiency[i], speed[i] ]` `    ``arr.sort(key ``=``cmp_to_key(mycmp1))` `    ``return` `arr` `# Function to find the maximum``# product of worker speeds and``# their minimum efficiency``def` `maximizePerformance(speed, K, efficiency):` `    ``n ``=` `len``(speed)``    ``arr ``=` `[[``0` `for` `j ``in` `range``(``2``)]``for` `i ``in` `range``(n)]` `    ``# Function to generate``    ``# sorted array of pairs``    ``arr ``=` `generateArrayofPairs(n, speed,efficiency, arr)` `    ``# Initialize priority queue``    ``pq ``=` `[]` `    ``# Initialize ans and sumofspeed``    ``ans ``=` `0``    ``SumOfSpeed ``=` `0` `    ``# Traversing the arr of pairs``    ``for` `it ``in` `arr:` `        ``e ``=` `it[``0``]``        ``s ``=` `it[``1``]` `        ``# Updating sum of speed``        ``SumOfSpeed ``+``=` `s` `        ``# Pushing in priority queue``        ``pq.append(s)` `        ``pq.sort(key ``=` `cmp_to_key(mycmp2))` `        ``# If team consists of more than``        ``# K workers``        ``if` `(``len``(pq) > K):` `            ``temp ``=` `pq[``len``(pq)``-``1``]``            ``SumOfSpeed ``-``=` `temp``            ``pq.pop()``        ` `        ``# Taking the maximum performance``        ``# that can be formed``        ``ans ``=` `max``(ans, SumOfSpeed ``*` `e)` `    ``# Finally return the ans``    ``return` `ans` `# Driver Code``# Given Input``speed ``=` `[``2``, ``10``, ``3``, ``1``, ``5``, ``8` `]``efficiency ``=` `[``5``, ``4``, ``3``, ``9``, ``7``, ``2` `]``K ``=` `2` `# Function Call``print``(maximizePerformance(speed, K, efficiency))``  ` `# This code is contributed by shinjanpatra`

## Javascript

 ``

Output:

`60`

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

My Personal Notes arrow_drop_up