Maximize sum of array elements removed by performing the given operations | C ++ program for the above approach ; Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver Code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxSum ( vector < int > arr , int n , vector < int > min , int k , int & S ) { vector < pair < int , int > > A ; for ( int i = 0 ; i < n ; i ++ ) { A . push_back ( { arr [ i ] , min [ i ] } ) ; } sort ( A . begin ( ) , A . end ( ) , greater < pair < int , int > > ( ) ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += max ( A [ i ] . first - K , A [ i ] . second ) ; K += k ; } } int main ( ) { vector < int > arr , min ; arr = { 3 , 5 , 2 , 1 } ; min = { 3 , 2 , 1 , 3 } ; int N = arr . size ( ) ; int K = 3 ; int S = 0 ; findMaxSum ( arr , N , min , K , S ) ; cout << S ; return 0 ; }