Split array into K non | Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
def maximumSum ( arr , S , N , K ) : NEW_LINE INDENT ans = 0 NEW_LINE arr = sorted ( arr ) [ : : - 1 ] NEW_LINE for i in range ( K ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT S = sorted ( S ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT if ( S [ i ] == 1 ) : NEW_LINE INDENT ans += arr [ i ] NEW_LINE DEDENT S [ i ] -= 1 NEW_LINE DEDENT counter = K - 1 NEW_LINE for i in range ( K ) : NEW_LINE INDENT counter = counter + S [ i ] NEW_LINE if ( S [ i ] != 0 ) : NEW_LINE INDENT ans += arr [ counter ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 13 , 7 , 17 ] NEW_LINE S = [ 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = len ( S ) NEW_LINE print ( maximumSum ( arr , S , N , K ) ) NEW_LINE DEDENT