Find the Maximum possible Sum for the given conditions | Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; print profit ; Driver code
def maxProfit ( arr ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] - ( 1 * i ) ) > 0 : NEW_LINE INDENT ans += ( arr [ i ] - ( 1 * i ) ) NEW_LINE DEDENT if ( arr [ i ] - ( 1 * i ) ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 6 , 6 ] NEW_LINE print ( maxProfit ( arr ) ) NEW_LINE DEDENT