Maximize sum of array elements removed by performing the given operations | Javascript 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
var S = 0 ; function findMaxSum ( arr , n , min , k ) { var A = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { A . push ( [ arr [ i ] , min [ i ] ] ) ; } A . sort ( ( a , b ) => b [ 0 ] - a [ 0 ] ) ; var K = 0 ; for ( var i = 0 ; i < n ; i ++ ) { S += Math . max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) ; K += k ; } } var arr = [ ] , min = [ ] ; arr = [ 3 , 5 , 2 , 1 ] ; min = [ 3 , 2 , 1 , 3 ] ; var N = arr . length ; var K = 3 ; findMaxSum ( arr , N , min , K , S ) ; document . write ( S ) ;