text: Maximum number of diamonds that can be gained in K minutes | C # program for the above approach ; Function to find the maximum number of diamonds that can be gained in exactly K minutes ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Pop it from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
code: using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void maxDiamonds ( int [ ] A , int N , int K ) { var pq = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( A [ i ] ) ; } int ans = 0 ; while ( pq . Count != 0 && K -- > 0 ) { pq . Sort ( ) ; int top = pq [ pq . Count - 1 ] ; pq . RemoveAt ( pq . Count - 1 ) ; ans += top ; top = top / 2 ; pq . Add ( top ) ; } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] A = { 2 , 1 , 7 , 4 , 2 } ; int K = 3 ; int N = A . Length ; maxDiamonds ( A , N , K ) ; } }