Given an array **arr[]** of **N** pairs of integers **(A, B)** where **N** is even, the task is to find the minimum sum of choosing **N** elements such that value **A** and **B** from all the pairs are chosen exactly **(N/2)** times.

**Examples:**

Input:N = 4, arr[][] = { {7, 20}, {300, 50}, {30, 200}, {30, 20} }

Output:107

Explanation:

Choose value-A from 1st pair = 7.

Choose value-B from 2nd pair = 50.

Choose value-A from 3rd pair = 30.

Choose value-B from 4th pair = 20.

The minimum sum is 7 + 50 + 30 + 20 = 107.

Input:N = 4, arr[][] = { {10, 20}, {400, 50}, {30, 200}, {30, 20} }

Output:110

Explanation:

Choose value-A from 1st pair = 10.

Choose value-B from 2nd pair = 50.

Choose value-A from 3rd pair = 30.

Choose value-B from 4th pair = 20.

The minimum sum is 10 + 50 + 30 + 20 = 110.

**Approach:** This problem can be solved using Greedy Approach. Below are the steps:

- For each pair
**(A, B)**in the given array, store the value of**(B – A)**with the corresponding index in temporary array(say**temp[]**). The value**(B – A)**actually defines how much cost is minimized if A is chosen over B for each element. - The objective is to minimize the total cost. Hence, sort the array
**temp[]**in decreasing order. - Pick the first
**N/2**elements from the array**temp[]**by choosing**A**as first**N/2**elements will have the maximum sum when**A**is chosen over**B**. - For remaining
**N/2**elements choose**B**as the sum of values can be minimized.

Below is the implementation of the above approach:

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to choose the elements A ` `// and B such the sum of all elements ` `// is minimum ` `int` `minSum(` `int` `arr[][2], ` `int` `n) ` `{ ` ` ` ` ` `// Create an array of pair to ` ` ` `// store Savings and index ` ` ` `pair<` `int` `, ` `int` `> temp[n]; ` ` ` ` ` `// Traverse the given array of pairs ` ` ` `for` `(` `int` `i = 0; i < 2 * n; i++) { ` ` ` ` ` `// Sum minimized when A ` ` ` `// is chosen over B for ` ` ` `// i-th element. ` ` ` `temp[i].first = arr[i][1] ` ` ` `- arr[i][0]; ` ` ` ` ` `// Store index for the ` ` ` `// future reference. ` ` ` `temp[i].second = i; ` ` ` `} ` ` ` ` ` `// Sort savings array in ` ` ` `// non-increasing order. ` ` ` `sort(temp, temp + 2 * n, ` ` ` `greater<pair<` `int` `, ` `int` `> >()); ` ` ` ` ` `// Storing result ` ` ` `int` `res = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < 2 * n; i++) { ` ` ` ` ` `// First n elements choose ` ` ` `// A and rest choose B ` ` ` `if` `(i < n) ` ` ` `res += arr[temp[i].second][0]; ` ` ` `else` ` ` `res += arr[temp[i].second][1]; ` ` ` `} ` ` ` ` ` `// Return the final Sum ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array of pairs ` ` ` `int` `arr[4][2] = { { 7, 20 }, ` ` ` `{ 300, 50 }, ` ` ` `{ 30, 200 }, ` ` ` `{ 30, 20 } }; ` ` ` ` ` `// Function Call ` ` ` `cout << minSum(arr, 2); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

107

**Time Complexity:** *O(N*log(N))*

**Auxiliary Space Complexity:** *O(N)*

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:

- Minimum sum by choosing minimum of pairs from array
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find minimum value of the expression by choosing K elements from given array
- Minimum sum obtained from groups of four elements from the given array
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Minimize the sum after choosing elements from the given three arrays
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Minimum cost of choosing 3 increasing elements in an array of size N
- Minimum cost of choosing the array element
- Probability of choosing a random pair with maximum sum in an array
- Check if sum Y can be obtained from the Array by the given operations
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Find the final number obtained after performing the given operation
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Count ways of choosing a pair with maximum difference
- Maximum array sum that can be obtained after exactly k changes
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Find the total marks obtained according to given marking scheme
- Count of distinct sums that can be obtained by adding prime numbers from given arrays

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.