-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.cpp
More file actions
52 lines (40 loc) · 1.01 KB
/
Copy path28.cpp
File metadata and controls
52 lines (40 loc) · 1.01 KB
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
#include <bits/stdc++.h>
using namespace std;
int main() {
/*
https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/practice-problems/algorithm/monk-and-the-magical-candy-bags/?update=google&purpose=login&source=problem-page&fbclid=IwAR2kDiVkEaxu9dkCTCUhzXLuIccNn0Gz3dSfkaSUjlDE6Nb9UHMzt8HNDo4
input
1
5 3
2 1 7 4 2
output
14
*/
int t;
cin>>t;
while (t--)
{
int n, k;
cin>>n>>k;
multiset<long long> bags;
for (int i = 0; i < n; i++)
{
long long candy_ct;
cin>>candy_ct;
bags.insert(candy_ct);
}
// for this loop log(n)
long long total_candies = 0;
for (int i = 0; i < k; i++)
{
auto last_it = (--bags.end());
long long candy_ct = *last_it;
total_candies += candy_ct;
bags.erase(last_it); // --> O(1)
bags.insert(candy_ct/2); // --> O(log(n))
}
// for this loop k * log(n)
cout<<total_candies<<"\n";
}
return 0;
}