-
Notifications
You must be signed in to change notification settings - Fork 0
/
monk and magical candy.txt
62 lines (52 loc) · 1.67 KB
/
monk and magical candy.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.
Input:
First line contains an integer T. T test cases follow.
First line of each test case contains two space-separated integers N and K.
Second line of each test case contains N space-separated integers,the number of candies in the bags.
Output:
Print the answer to each test case in a new line.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ K ≤ 105
0 ≤ Ai ≤ 1010
Sample Input
1
5 3
2 1 7 4 2
Sample Output
14
solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
long long n , k;
cin >> n >> k;
multiset <long long > bags;
for(long long i = 0 ; i < n ;i++)
{
long long count_candy;
cin >> count_candy;
bags.insert(count_candy);
}
long long total = 0;
for(long long i = 0 ; i < k ; i++)
{
auto last = (--bags.end());
long long count_candy = *last;
total = total + count_candy;
bags.erase(last);
bags.insert(count_candy/2);
}
cout << total << endl;
}
}