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++ 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 replaceable 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;
} |
// Java implementation of the approach import java.util.*;
// User defined Pair class class Pair {
int x;
int y;
// Constructor
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
} // class to define user defined conparator class Sort {
static void sort(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.x - p2.x;
}
});
}
} public class Main
{ // Function to return the maximum
// possible sum after M operations
static int max_sum( int [] a, int n,
int [][] b, int m)
{
// Sort the array a in
// increasing order
Arrays.sort(a);
// Place all replaceable cards in B
Pair B[] = new Pair[m];
for ( int i = 0 ; i < m; i++)
B[i] = new Pair(b[i][ 1 ], b[i][ 0 ]);
// Sort vector B in decreasing order
Sort obj = new Sort();
obj.sort(B, m);
// To store last unused card of a
int left = 0 ;
// Try to apply all m operations
for ( int i = m- 1 ; i >= 0 ; i--)
{
int x = B[i].x, y = B[i].y;
// 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
public static void main(String[] args) {
int [] a = { 5 , 1 , 4 };
int n = a.length;
int [][] b = { { 2 , 3 }, { 1 , 5 } };
int m = 2 ;
System.out.println(max_sum(a, n, b, m));
}
} // This code is contributed by divyesh072019. |
# 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 replaceable 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 |
// C# implementation of the approach using System;
using System.Collections.Generic;
class GFG{
// Function to return the maximum // possible sum after M operations static int max_sum( int [] a, int n,
int [,] b, int m)
{ // Sort the array a in
// increasing order
Array.Sort(a);
// Place all replaceable cards in B
List<Tuple< int ,
int >> B = new List<Tuple< int ,
int >>();
for ( int i = 0; i < m; i++)
B.Add( new Tuple< int , int >(b[i, 1], b[i, 0]));
// Sort vector B in decreasing order
B.Sort();
B.Reverse();
// 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].Item1, y = B[i].Item2;
// 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 static void Main()
{ int [] a = { 5, 1, 4 };
int n = a.Length;
int [,] b = { { 2, 3 }, { 1, 5 } };
int m = 2;
Console.WriteLine(max_sum(a, n, b, m));
} } // This code is contributed by divyeshrabadiya07 |
<script> // JavaScript implementation of the approach // Function to return the maximum // possible sum after M operations function max_sum(a, n, b, m) {
// Sort the array a in
// increasing order
a.sort();
// Place all replaceable cards in B
let B = new Array();
for (let i = 0; i < m; i++)
B.push([b[i][1], b[i][0]]);
// Sort vector B in decreasing order
B.sort().reverse();
// To store last unused card of a
let left = 0;
// Try to apply all m operations
for (let i = 0; i < m; i++) {
let x = B[i][0], y = B[i][1];
// Try for all applicable cards
for (let 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
let ans = 0;
// Calculate the maximum
// possible sum
for (let i = 0; i < n; i++)
ans += a[i];
// Return the required answer
return ans;
} // Driver code let a = [5, 1, 4]; let n = a.length; let b = [[2, 3], [1, 5]]; let m = b.length; document.write(max_sum(a, n, b, m)); </script> |
14
Time Complexity: O(m*m), as nested loops are used where m is the size of a given 2d array
Auxiliary Space: O(m), as extra space of size m is used