Samkit5025

Untitled

Jul 13th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int minimumTransactions(int N, int X, vector<int> &B)
  5. {
  6.  
  7.     priority_queue<int> pq;
  8.     for (int i = 0; i < N; i++)
  9.     {
  10.         pq.push(B[i]);
  11.     }
  12.  
  13.     int res = 0;
  14.  
  15.     while (pq.top() != 0 && X > 0)
  16.     {
  17.         int temp = pq.top();
  18.         pq.pop();
  19.         X -= temp;
  20.         res++;
  21.         pq.push(temp / 2);
  22.     }
  23.  
  24.     if (X > 0)
  25.     {
  26.         return -1;
  27.     }
  28.     return res;
  29. }
  30.  
  31. int main()
  32. {
  33.     int N, X;
  34.     cin >> N >> X;
  35.  
  36.     vector<int> B(N);
  37.     for (int i = 0; i < N; i++)
  38.     {
  39.         cin >> B[i];
  40.     }
  41.     cout << minimumTransactions(N, X, B) << endl;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment