Minimize Sum of an Array by at most K reductions | C # program to implement the above approach ; Function to obtain the minimum possible sum from the array by K reductions ; Implements the MaxHeap ; Insert elements into the MaxHeap ; Insert maximum / 2 ; Stores the sum of remaining elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int minSum ( int [ ] a , int n , int k ) { List < int > q = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { q . Add ( a [ i ] ) ; } q . Sort ( ) ; while ( q . Count != 0 && k > 0 ) { int top = q [ q . Count - 1 ] / 2 ; q [ q . Count - 1 ] = top ; k -- ; q . Sort ( ) ; } int sum = 0 ; while ( q . Count != 0 ) { sum += q [ 0 ] ; q . RemoveAt ( 0 ) ; } return sum ; } static public void Main ( ) { int n = 4 ; int k = 3 ; int [ ] a = { 20 , 7 , 5 , 4 } ; Console . WriteLine ( minSum ( a , n , k ) ) ; } }