Maximum possible sum after M operations on N cards

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

14


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.



Improved By : AnkitRai01



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.