Find the maximum cost of an array of pairs choosing at most K pairs

Given an array of pairs arr[], the task is to find the maximum cost choosing at most K pairs. The cost of an array of pairs is defined as product of the sum of first elements of the selected pair and the minimum among the second elements of the selected pairs. For example, if the following pairs are selected (3, 7), (9, 2) and (2, 5), then the cost will be (3+9+2)*(2) = 28.

Examples:

Input: arr[] = { {4, 7}, {15, 1}, {3, 6}, {6, 8} }, K = 3
Output: 78
The pairs 1, 3 and 4 are selected, therefore the cost = (4 + 3 + 6) * 6 = 78.



Input: arr[] = { {62, 21}, {31, 16}, {19, 2}, {32, 19}, {12, 17} }, K = 4
Output: 2192

Approach: If the second element of a pair is fixed in the answer, then K-1(or less) other pairs are to be selected from those pairs whose second element is greater or equal to the fixed second element and the answer will be maximum if those are chosen such that the sum of first elements is maximum. So, sort the array according to second element and then iterate in descending order taking maximum sum of the first element of K pairs(or less). The maximum sum of first element K pairs can be taken with the help of Set data structure.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Driver function to sort the array elements
// by second element of pairs
bool sortbysec(const pair<int, int>& a,
               const pair<int, int>& b)
{
    return (a.second < b.second);
}
  
// Function that returns the maximum cost of
// an array of pairs choosing at most K pairs.
int maxCost(pair<int, int> a[], int N, int K)
{
    // Initialize result and temporary sum variables
    int res = 0, sum = 0;
  
    // Initialize Set to store K greatest
    // element for maximum sum
    set<pair<int, int> > s;
  
    // Sort array by second element
    sort(a, a + N, sortbysec);
  
    // Iterate in descending order
    for (int i = N - 1; i >= 0; --i) {
        s.insert(make_pair(a[i].first, i));
        sum += a[i].first;
        while (s.size() > K) {
            auto it = s.begin();
            sum -= it->first;
            s.erase(it);
        }
  
        res = max(res, sum * a[i].second);
    }
  
    return res;
}
  
// Driver Code
int main()
{
    pair<int, int> arr[] = { { 12, 3 }, { 62, 21 }, { 31, 16 }, 
                             { 19, 2 }, { 32, 19 }, { 12, 17 }, 
                             { 1, 7 } };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    int K = 3;
  
    cout << maxCost(arr, N, K);
  
    return 0;
}

chevron_right


Output:

2000


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.