Given an array of integers **arr[]** consisting of **N** integers, the task is to minimize the sum of the given array by performing at most **K** operations, where each operation involves reducing an array element **arr[i]** to **floor(arr[i]/2)**.

**Examples :**

Input:N = 4, a[] = {20, 7, 5, 4}, K = 3Output:17Explanation:

Operation 1: {20, 7, 5, 4} -> {10, 7, 5, 4}

Operation 2: {10, 7, 5, 4} -> {5, 7, 5, 4}

Operation 3: {5, 7, 5, 4} -> {5, 3, 5, 4}

No further operation can be performed. Therefore, sum of the array = 17.

Input:N = 4, a[] = {10, 4, 6, 16}, K = 2Output:23

**Approach: **To obtain the minimum possible sum, the main idea for every operation is to reduce the maximum element in the array before each operation. This can be implemented using **MaxHeap**. Follow the steps below to solve the problem:

- Insert all the array elements into
**MaxHeap**. - Pop the root of the
**MaxHeap**and insert**(popped element) / 2**into the**MaxHeap** - After repeating the above step
**K**times, pop the elements of the**MaxHeap**one by one and keep adding their values. Finally, print the sum.

**Below is the implementation of above approach:**

## C++

`// C++ program to implement the ` `// above approach ` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to obtain the minimum possible ` `// sum from the array by K reductions ` `int` `minSum(` `int` `a[], ` `int` `n, ` `int` `k)` `{` ` ` `priority_queue <` `int` `> q;` ` ` ` ` `// Insert elements into the MaxHeap ` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `q.push(a[i]);` ` ` `}` ` ` ` ` `while` `(!q.empty() && k > 0)` ` ` `{` ` ` `int` `top = q.top() / 2;` ` ` ` ` `// Remove the maximum ` ` ` `q.pop();` ` ` ` ` `// Insert maximum / 2 ` ` ` `q.push(top);` ` ` `k -= 1;` ` ` `}` ` ` ` ` `// Stores the sum of remaining elements` ` ` `int` `sum = 0;` ` ` `while` `(!q.empty())` ` ` `{` ` ` `sum += q.top();` ` ` `q.pop();` ` ` `}` ` ` `return` `sum;` `}` `// Driver code` `int` `main() ` `{` ` ` `int` `n = 4; ` ` ` `int` `k = 3; ` ` ` `int` `a[] = { 20, 7, 5, 4 }; ` ` ` ` ` `cout << (minSum(a, n, k));` ` ` ` ` `return` `0;` `}` `// This code is contributed by jojo9911` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement the ` `// above approach ` `import` `java.io.*; ` `import` `java.util.*; ` `class` `GFG { ` ` ` `// Function to obtain the minimum possible ` ` ` `// sum from the array by K reductions ` ` ` `public` `static` `int` `minSum(` `int` `a[], ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Implements the MaxHeap ` ` ` `PriorityQueue<Integer> maxheap ` ` ` `= ` `new` `PriorityQueue<>((one, two) -> two - one); ` ` ` `// Insert elements into the MaxHeap ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `maxheap.add(a[i]); ` ` ` `while` `(maxheap.size() > ` `0` `&& k > ` `0` `) { ` ` ` `// Remove the maximum ` ` ` `int` `max_ele = maxheap.poll(); ` ` ` `// Insert maximum / 2 ` ` ` `maxheap.add(max_ele / ` `2` `); ` ` ` `k -= ` `1` `; ` ` ` `} ` ` ` `// Stores the sum of remaining elements ` ` ` `int` `sum = ` `0` `; ` ` ` `while` `(maxheap.size() > ` `0` `) ` ` ` `sum += maxheap.poll(); ` ` ` `return` `sum; ` ` ` `} ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `int` `k = ` `3` `; ` ` ` `int` `a[] = { ` `20` `, ` `7` `, ` `5` `, ` `4` `}; ` ` ` `System.out.println(minSum(a, n, k)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement the` `# above approach` `# Function to obtain the minimum possible` `# sum from the array by K reductions` `def` `minSum(a, n, k):` ` ` ` ` `q ` `=` `[]` ` ` ` ` `# Insert elements into the MaxHeap` ` ` `for` `i ` `in` `range` `(n):` ` ` `q.append(a[i])` ` ` ` ` `q ` `=` `sorted` `(q) ` ` ` `while` `(` `len` `(q) > ` `0` `and` `k > ` `0` `):` ` ` `top ` `=` `q[` `-` `1` `] ` `/` `/` `2` ` ` `# Remove the maximum` ` ` `del` `q[` `-` `1` `]` ` ` `# Insert maximum / 2` ` ` `q.append(top)` ` ` `k ` `-` `=` `1` ` ` `q ` `=` `sorted` `(q)` ` ` `# Stores the sum of remaining elements` ` ` `sum` `=` `0` ` ` `while` `(` `len` `(q) > ` `0` `):` ` ` `sum` `+` `=` `q[` `-` `1` `]` ` ` `del` `q[` `-` `1` `]` ` ` ` ` `return` `sum` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `n ` `=` `4` ` ` `k ` `=` `3` ` ` ` ` `a ` `=` `[ ` `20` `, ` `7` `, ` `5` `, ` `4` `]` ` ` `print` `(minSum(a, n, k))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

**Output:**

17

**Time Complexity:** O(Klog(N)) **Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of minimum reductions required to get the required sum K
- Number of continuous reductions of A from B or B from A to make them (1, 1)
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Minimize difference between maximum and minimum of Array by at most K replacements
- Minimize the number by changing at most K digits
- Minimize the maximum difference of adjacent elements after at most K insertions
- Minimize positive product of two given numbers by at most N decrements
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Maximize Array sum by swapping at most K elements with another array
- Minimize the sum of squares of sum of N/2 paired formed by N numbers
- Rearrange an array to minimize sum of product of consecutive pair elements
- Minimize sum of adjacent difference with removal of one element from array
- Minimize the sum of the array according the given condition
- Reverse a subarray of the given array to minimize the sum of elements at even position
- Minimize sum of prime numbers added to make an array non-decreasing
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize K whose XOR with given array elements leaves array unchanged
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.