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:

`// 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*

*filter_none*

**Output:**

2000

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize cost of choosing and skipping array elements to reach end of the given array
- Minimum cost of choosing 3 increasing elements in an array of size N
- Minimum cost of choosing the array element
- Minimum sum by choosing minimum of pairs from array
- Probability of choosing a random pair with maximum sum in an array
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Minimum sum obtained by choosing N number from given N pairs
- Count ways of choosing a pair with maximum difference
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Minimize the sum after choosing elements from the given three arrays
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum cost to make array size 1 by removing larger of pairs
- Maximum items that can be bought from the cost Array based on given conditions
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Maximum subarray sum by flipping signs of at most K array elements
- Maximum possible sub-array sum after at most X swaps
- Maximum possible GCD after replacing at most one element in the given array
- Maximum length of Strictly Increasing Sub-array after removing at most one element

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.