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 ; S . reverse ( ) ; 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
function maximumSum ( arr , S , N , K ) { var ans = 0 ; var i ; arr . sort ( ( a , b ) => b - a ) ; for ( i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; S . sort ( ) ; for ( i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -= 1 ; } var counter = K - 1 ; for ( i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } var arr = [ 1 , 13 , 7 , 17 ] ; var S = [ 1 , 3 ] ; var N = arr . length ; var K = S . length ; document . write ( maximumSum ( arr , S , N , K ) ) ;