Premium Only Content
2558. Take Gifts From the Richest Pile
You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:
Choose the pile with the maximum number of gifts.
If there is more than one pile with the maximum number of gifts, choose any.
Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
Return the number of gifts remaining after k seconds.
Example 1:
Input: gifts = [25,64,9,4,100], k = 4
Output: 29
Explanation:
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
Example 2:
Input: gifts = [1,1,1,1], k = 4
Output: 4
Explanation:
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
That is, you can't take any pile with you.
So, the total gifts remaining are 4.
Constraints:
1 <= gifts.length <= 103
1 <= gifts[i] <= 109
1 <= k <= 103
#define ll long long
class Solution {
public:
long long pickGifts(vector<int>& gifts, int k) {
priority_queue<int> pq(gifts.begin(), gifts.end());
ll sum {};
while(k--){
int temp = sqrt(pq.top()); pq.pop();
pq.push(temp);
}
while(!pq.empty()){
sum += pq.top(); pq.pop();
}
return sum;
}
};
-
23:20
Lady Decade
5 hours ago $12.89 earnedI Spent The Night With Alex Jones
21.6K26 -
LIVE
SavageJayGatsby
4 hours agoSpicy Saturday – Goblin Cleanup Chaos! 💀🌶
165 watching -
16:47
Robbi On The Record
2 days ago $12.42 earnedThe Day Seeing Stopped Meaning Believing | Sora, AI and the Uncanny Valley
36.3K33 -
4:07:36
GamerGril
5 hours agoIt's The Zombie Apocalypse, Bring Your Friends 💞Scream Queens💞
9.7K1 -
LIVE
Phyxicx
5 hours agoHalo Tournament! - 10/25/2025
74 watching -
4:10:42
megimu32
6 hours agoOFF THE SUBJECT: Zombie Apocalypse with GamerGril & Friends 🧟 ♀ (Send Help)
105K5 -
22:08
MYLUNCHBREAK CHANNEL PAGE
1 day agoUnder Prague
49.2K20 -
1:19:20
Jeff Ahern
7 hours ago $15.39 earnedThe Saturday Show with Jeff Ahern
28.4K9 -
13:26
Cash Jordan
1 day ago“PORTLAND MOB” Storms ICE HQ… ‘COMBAT’ Troops Respond With EXTREME FORCE
77K208 -
4:32:52
ttvglamourx
9 hours ago $5.66 earnedFINSIHING YEAR 1 ON STARDEW !DISCORD
21.6K