Maximize sum of array elements removed by performing the given operations | 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
def findMaxSum ( arr , n , min , k , S ) : NEW_LINE INDENT A = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT A . append ( ( arr [ i ] , min [ i ] ) ) NEW_LINE DEDENT A = sorted ( A ) NEW_LINE A = A [ : : - 1 ] NEW_LINE K = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT S += max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) NEW_LINE K += k NEW_LINE DEDENT return S NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr , min = [ ] , [ ] NEW_LINE arr = [ 3 , 5 , 2 , 1 ] NEW_LINE min = [ 3 , 2 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE S = 0 NEW_LINE S = findMaxSum ( arr , N , min , K , S ) NEW_LINE print ( S ) NEW_LINE DEDENT