Maximum possible sum after M operations on N cards
Last Updated :
19 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int max_sum( int a[], int n, int b[][2], int m)
{
sort(a, a + n);
vector<pair< int , int > > B;
for ( int i = 0; i < m; i++)
B.push_back({ b[i][1], b[i][0] });
sort(B.rbegin(), B.rend());
int left = 0;
for ( int i = 0; i < m; i++) {
int x = B[i].first, y = B[i].second;
for ( int j = 0; j < y; j++) {
if (a[left] < x) {
a[left] = x;
left++;
if (left == n)
break ;
}
else
break ;
}
}
int ans = 0;
for ( int i = 0; i < n; i++)
ans += a[i];
return ans;
}
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
import java.util.*;
class Pair {
int x;
int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
class Sort {
static void sort(Pair arr[], int n)
{
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.x - p2.x;
}
});
}
}
public class Main
{
static int max_sum( int [] a, int n,
int [][] b, int m)
{
Arrays.sort(a);
Pair B[] = new Pair[m];
for ( int i = 0 ; i < m; i++)
B[i] = new Pair(b[i][ 1 ], b[i][ 0 ]);
Sort obj = new Sort();
obj.sort(B, m);
int left = 0 ;
for ( int i = m- 1 ; i >= 0 ; i--)
{
int x = B[i].x, y = B[i].y;
for ( int j = 0 ; j < y; j++)
{
if (a[left] < x)
{
a[left] = x;
left++;
if (left == n)
break ;
}
else
break ;
}
}
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
ans += a[i];
return ans;
}
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));
}
}
|
Python3
def max_sum(a, n, b, m) :
a.sort();
B = [];
for i in range (m) :
B.append([b[i][ 1 ], b[i][ 0 ]]);
B.sort(reverse = True )
left = 0 ;
for i in range (m) :
x = B[i][ 0 ];
y = B[i][ 1 ];
for j in range (y) :
if (a[left] < x) :
a[left] = x;
left + = 1 ;
if (left = = n) :
break ;
else :
break ;
ans = 0 ;
for i in range (n) :
ans + = a[i];
return ans;
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));
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int max_sum( int [] a, int n,
int [,] b, int m)
{
Array.Sort(a);
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]));
B.Sort();
B.Reverse();
int left = 0;
for ( int i = 0; i < m; i++)
{
int x = B[i].Item1, y = B[i].Item2;
for ( int j = 0; j < y; j++)
{
if (a[left] < x)
{
a[left] = x;
left++;
if (left == n)
break ;
}
else
break ;
}
}
int ans = 0;
for ( int i = 0; i < n; i++)
ans += a[i];
return ans;
}
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));
}
}
|
Javascript
<script>
function max_sum(a, n, b, m) {
a.sort();
let B = new Array();
for (let i = 0; i < m; i++)
B.push([b[i][1], b[i][0]]);
B.sort().reverse();
let left = 0;
for (let i = 0; i < m; i++) {
let x = B[i][0], y = B[i][1];
for (let j = 0; j < y; j++) {
if (a[left] < x) {
a[left] = x;
left++;
if (left == n)
break ;
}
else
break ;
}
}
let ans = 0;
for (let i = 0; i < n; i++)
ans += a[i];
return ans;
}
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>
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...