Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | Java implementation to find the minimum cost of removing all elements from the array ; Reverse array in decreasing order ; Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function call
import java . util . * ; class GFG { static long [ ] reverse ( long a [ ] ) { int i , n = a . length ; long t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static long removeElements ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; arr = reverse ( arr ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] * Math . pow ( 2 , i ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; long arr [ ] = { 3 , 1 , 2 , 3 } ; System . out . print ( removeElements ( arr , n ) ) ; } }