Adrita

task4 ( ds lab 5) 3rd sem

Feb 17th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, k, operations=0;
  6. cout<<"Enter value of k & n"<<endl;
  7. cin >> n >> k;
  8. cout<<"Enter values"<<endl;
  9. priority_queue<int, vector<int>, greater<int> > pq;
  10. for (int i=0; i<n; i++)
  11. {
  12. int a;
  13. cin>>a;
  14. pq.push(a);
  15. }
  16. while (pq.top()<k && pq.size()>=2)
  17. {
  18. int cookie1=pq.top();
  19. pq.pop();
  20. int cookie2=pq.top();
  21. pq.pop();
  22. pq.push(cookie1+2*cookie2);
  23. operations++;
  24. }
  25. if(pq.top()>=k)
  26. cout<<operations;
  27. else
  28. cout<<"-1";
  29. }
Advertisement
Add Comment
Please, Sign In to add comment