Given an array **arr[]** of size **N** which represents the initial number on each card and given a two dimensional array **B[][]** of size **M** where **M** represents the number of operations that need to be performed. At each operation, choose at most **B[j][0]** cards (possibly zero) and replace the integer written on each chosen card with **B[j][1]**. The task is to find the maximum possible sum after **M** operations.

**Examples:**

Input:arr[] = {5, 1, 4}, B[][] = {{2, 3}, {1, 5}}

Output:14

Replacing 1 with 5 and the sum becomes

5 + 5 + 4 = 14 which is the maximum possible.

Input:arr[] = {100, 100}, B[][] = {{2, 99}}

Output:200

**Approach:** A greedy approach is applicable here. Sort the array **arr[]** in increasing order and sort the array **B[][]** in decreasing order of the number to be replaced. Then try to replace last non-replaced card of **arr[]** with one of the non-used cards of **B[][]**. Finally, print the maximized sum.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum ` `// possible sum after M operations ` `int` `max_sum(` `int` `a[], ` `int` `n, ` `int` `b[][2], ` `int` `m) ` `{ ` ` ` ` ` `// Sort the array a in ` ` ` `// increasing order ` ` ` `sort(a, a + n); ` ` ` ` ` `// Place all replacable cards in B ` ` ` `vector<pair<` `int` `, ` `int` `> > B; ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `B.push_back({ b[i][1], b[i][0] }); ` ` ` ` ` `// Sort vector B in decreasing order ` ` ` `sort(B.rbegin(), B.rend()); ` ` ` ` ` `// To store last unused card of a ` ` ` `int` `left = 0; ` ` ` ` ` `// Try to apply all m operations ` ` ` `for` `(` `int` `i = 0; i < m; i++) { ` ` ` `int` `x = B[i].first, y = B[i].second; ` ` ` ` ` `// Try for all applicable cards ` ` ` `for` `(` `int` `j = 0; j < y; j++) { ` ` ` ` ` `// If current number on card is ` ` ` `// less than applicable card ` ` ` `if` `(a[left] < x) { ` ` ` `a[left] = x; ` ` ` `left++; ` ` ` ` ` `if` `(left == n) ` ` ` `break` `; ` ` ` `} ` ` ` `else` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the maximum ` ` ` `// possible sum ` ` ` `int` `ans = 0; ` ` ` ` ` `// Calculate the maximum ` ` ` `// possible sum ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans += a[i]; ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 5, 1, 4 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `int` `b[][2] = { { 2, 3 }, { 1, 5 } }; ` ` ` `int` `m = ` `sizeof` `(b) / ` `sizeof` `(b[0]); ` ` ` ` ` `cout << max_sum(a, n, b, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum ` `# possible sum after M operations ` `def` `max_sum(a, n, b, m) : ` ` ` ` ` `# Sort the array a in ` ` ` `# increasing order ` ` ` `a.sort(); ` ` ` ` ` `# Place all replacable cards in B ` ` ` `B ` `=` `[]; ` ` ` `for` `i ` `in` `range` `(m) : ` ` ` `B.append([b[i][` `1` `], b[i][` `0` `]]); ` ` ` ` ` `# Sort vector B in decreasing order ` ` ` `B.sort(reverse ` `=` `True` `) ` ` ` ` ` `# To store last unused card of a ` ` ` `left ` `=` `0` `; ` ` ` ` ` `# Try to apply all m operations ` ` ` `for` `i ` `in` `range` `(m) : ` ` ` `x ` `=` `B[i][` `0` `]; ` ` ` `y ` `=` `B[i][` `1` `]; ` ` ` ` ` `# Try for all applicable cards ` ` ` `for` `j ` `in` `range` `(y) : ` ` ` ` ` `# If current number on card is ` ` ` `# less than applicable card ` ` ` `if` `(a[left] < x) : ` ` ` `a[left] ` `=` `x; ` ` ` `left ` `+` `=` `1` `; ` ` ` ` ` `if` `(left ` `=` `=` `n) : ` ` ` `break` `; ` ` ` `else` `: ` ` ` `break` `; ` ` ` ` ` `# To store the maximum ` ` ` `# possible sum ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `# Calculate the maximum ` ` ` `# possible sum ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `ans ` `+` `=` `a[i]; ` ` ` ` ` `# Return the required answer ` ` ` `return` `ans; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[` `5` `, ` `1` `, ` `4` `]; ` ` ` `n ` `=` `len` `(a); ` ` ` `b ` `=` `[[` `2` `, ` `3` `], [` `1` `, ` `5` `]]; ` ` ` `m ` `=` `len` `(b); ` ` ` ` ` `print` `(max_sum(a, n, b, m)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

14

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:

- Maximum possible Array sum after performing given operations
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Maximum sum of all elements of array after performing given operations
- Maximum possible sub-array sum after at most X swaps
- Find the player who wins the game by removing the last of given N cards
- Find maximum value of the last element after reducing the array with given operations
- Maximum number of unique values in the array after performing given operations
- Find the maximum difference after applying the given operations two times on a number
- Find maximum sum possible equal sum of three stacks
- Minimum element left from the array after performing given operations
- Find the modified array after performing k operations of given type
- Lexicographically smallest string after M operations
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Maximum array sum that can be obtained after exactly k changes
- Maximum sum after rearranging the array for K queries
- Subarray with largest sum after excluding its maximum element
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Find the Maximum possible Sum for the given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Check if it is possible to sort the array after rotating it

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.