Advertisement
nikunjsoni

1962

Aug 15th, 2021
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minStoneSum(vector<int>& piles, int k) {
  4.         priority_queue<int> pq;
  5.         for(auto pile: piles){
  6.             pq.push(pile);
  7.         }
  8.        
  9.         long long int sum = accumulate(piles.begin(), piles.end(), 0LL);
  10.         long long int ans = 0;
  11.         while(k && !pq.empty()){
  12.             int t = pq.top(); pq.pop();
  13.             int half = t/2;
  14.             ans += half;
  15.             t -= half;
  16.             if(t)
  17.                 pq.push(t);
  18.             k--;
  19.         }
  20.         return sum-ans;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement