given an array of integers, perform atmost K operations so that the sum of elements of final array is minimum. An operation is defined as follows - Consider any 1 element from the array, arr[i]. Replace arr[i] by floor(arr[i]/2). Perform next operations on updated array. The task is to minimize the sum after atmost K operations. Constraints 1 Select 20. Replace it by 10. New array = [10, 7, 5, 4] Operation 2 -> Select 10. Replace it by 5. New array = [5, 7, 5, 4]. Operation 3 -> Select 7. Replace it by 3. New array = [5,3,5,4]. Sum = 17.
Answers
Answer:
Problem Description
Given an array of integers, perform atmost K operations so that the sum of elements of final array is minimum. An operation is defined as follows -
Consider any 1 element from the array, arr[i].
Replace arr[i] by floor(arr[i]/2).
Perform next operations on updated array.
The task is to minimize the sum after atmost K operations.
Constraints
1 <= N, K <= 10^5.
Input
First line contains two integers N and K representing size of array and maximum numbers of operations that can be performed on the array respectively.
Second line contains N space separated integers denoting the elements of the array, arr.
Output
Print a single integer denoting the minimum sum of the final array.
Time Limit
1
Examples
Example 1
Input
4 3
20 7 5 4
Output
17
Explanation
Operation 1 -> Select 20. Replace it by 10.
New array = [10, 7, 5, 4]
Operation 2 -> Select 10. Replace it by 5.
New array = [5, 7, 5, 4].
Operation 3 -> Select 7. Replace it by 3.
New array = [5,3,5,4].
Sum = 17.
Explanation:
Answer:
Explanation:[22:08, 08/08/2020] +91 95816 50200: Diet Plan
Problem Description
Arnold is planning to follow a diet suggested by his Nutritionist. The Nutritionist prescribed him the total protein, carbohydrates and fats, he should take daily. Arnold searches on an online food store and makes a list of protein, carbohydrates and fats contained in a single unit of various food items.
His target is to have the maximum protein, carbohydrates and fats in his diet without exceeding the prescribed limit. He also wants to have as much diverse food items as much as possible. That is, he does not want to have many units of one food item and 0 of others. Multiple combinations of 'units of food items' are possible to achieve the target. Mathematically speaking, diversity is more if the difference between the number of units of food item chosen the most and the number of units of another food item chosen the least, is as small as possible.
To solve this problem, he uses maximum possible number of units of all the items so that total amount of protein, carbohydrates and fats is not more than prescribed limit. For example - if total nutrition required is 100P, 130C and 130F (where P is Protein, C is Carbohydrates and F is Fats) and 2 different food items, viz. Item A and Item B, have following amount of nutrition:
Item A - 10P, 20C, 30F
Item B - 20P, 30C, 20F
then, he can have (maximum possible) 2 units of all the items as having 3 units will exceed the prescribed amount of Carbohydrates and fats.
Next, he chooses food items to fulfill the remaining nutrients. He chooses one more units of maximum number of food items. He continues this process till he cannot add a unit of any food item to his diet without exceeding the prescribed limit. In this example, he can choose one more unit of item B or one more unit of item A. In case he has two sets of food items then the priority is given to fulfill the requirements of Protein, Carbohydrates, and Fats in that order. So he chooses item B.
You will be provided the maximum nutrients required and the nutrients available in various food items. You need to find the amount of nutrients for which there is a shortfall as compared to the prescription, after making his selection using the process described above. In the example he still needs 20P, 0C, 10F to achieve his target.
Constraints
Number of Food Items <= 10
Maximum amount of Nutrients is less than 1500 i.e. x +y + z <= 1500
Amount of P, C, F in two food items will not be same
P, C, F values in input can be in any order
Output should be in order - P, C, F.
Input
First line contains the maximum limit of nutrients in the following format.
xP yC zF, where x, y and z are integers
Second line contains nutrient composition of different food items separated by pipe (|).
Output
Print the shortfall for each nutrient type, fulfilled separated by space.
E.g. If the output is 10P, 20 C, 30 F, then print "10 20 30" (without quotes).
Time Limit
1
Examples
Example 1
Input
100P 130C 130F
10P 20C 30F|20P 30C 20F
Output
20 0 10
Explanation
Having 2 units of item A and 3 units of item B provides - 2 * [10P 20C 30F] + 3 * [20P 30C 20F] = 100P, 130C, 120F. This is the best combination that can reduce the shortfall [20P, 0C, 10F] without exceeding his prescription. In contrast, if 3 units of A and 2 units of B are chosen then 3 * [10P 20C 30F] + 2 * [20P 30C 20F] = 70P, 120C, 130F produces a shortfall of [30P, 10C, 0F]. However, since protein shortfall in this case is more than the previous combination, this is not the right combination to follow.
[22:14, 08/08/2020] +91 95816 50200: Minimize The Sum
Problem Description
Given an array of integers, perform atmost K operations so that the sum of elements of final array is minimum. An operation is defined as follows -
Consider any 1 element from the array, arr[i].
Replace arr[i] by floor(arr[i]/2).
Perform next operations on updated array.
The task is to minimize the sum after atmost K operations.
Constraints
1 <= N, K <= 10^5.
Input
First line contains two integers N and K representing size of array and maximum numbers of operations that can be performed on the array respectively.
Second line contains N space separated integers denoting the elements of the array, arr.
Output
Print a single integer denoting the minimum sum of the final array.
Time Limit
1
Examples
Example 1
Input
4 3
20 7 5 4
Output
17
Explanation
Operation 1 -> Select 20. Replace it by 10.
New array = [10, 7, 5, 4]
Operation 2 -> Select 10. Replace it by 5.
New array = [5, 7, 5, 4].
Operation 3 -> Select 7. Replace it by 3.
New array = [5,3,5,4].
Sum = 17.
Upload Solution