Our Monk loves candy!
While taking a stroll in the park, he stumbled upon N Bags with candies. The i'th of these bags contains Ai candies.
He picks up a bag, eats all the candies in it, and drops it on the ground. But as soon as he drops the bag, the number of candies in the bag increases magically! Say the bag that used to contain X candies (before eating), now contains [X/2] candies! , where [x] is the greatest integer less than x (Greatest Integer Function).
Amazed by the magical spell, Monk can now have a lot more candies! But he has to return home in K minutes. In a single minute, Monk can consume all the candies in a single bag, regardless of the number of candies in it.
Find the maximum number of candies that Monk can consume.
Example:
Input: n = 5, k = 3, a = [2,1,7,4,2]
Output: 14
Approach
C++
#include <bits/stdc++.h>using namespace std;long long maximumCandies(long long n,long long k, long long a[]){priority_queue<long long> q;for (long long i = 0; i < n; i++){q.push(a[i]);}long long ans = 0;while (!q.empty() && k--){ans += q.top();long long x = q.top();q.pop();if (x / 2 > 0)q.push(x / 2);}return ans;}int main(){long long n = 5, k = 3;long long a[n] = {2, 1, 7, 4, 2};cout << maximumCandies(n, k, a) << "\n";return 0;}
No comments:
Post a Comment