Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | C ++ implementation to find the minimum cost of removing all elements from the array ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int removeElements ( ll arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; ll ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] * pow ( 2 , i ) ; } return ans ; } int main ( ) { int n = 4 ; ll arr [ n ] = { 3 , 1 , 2 , 3 } ; cout << removeElements ( arr , n ) ; }