fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,k,c=0;
  6. cin>>n>>k;
  7. int arr[n];
  8. long long sum=0;
  9. for(int i=0;i<n;i++)
  10. {
  11. cin>>arr[i];
  12. sum+=arr[i];
  13. }
  14. priority_queue< pair< long long, pair<int, int> > > q;
  15. q.push(make_pair(sum,make_pair(0,n-1)));
  16. while(k-- && !q.empty())
  17. {
  18.  
  19. long long ans=q.top().first;
  20. int i=q.top().second.first,j=q.top().second.second;
  21. q.pop();
  22. cout<<ans<<" ";
  23. if(i!=j)
  24. {
  25. q.push(make_pair(ans-arr[i],make_pair(i+1,j)));
  26. q.push(make_pair(ans-arr[j],make_pair(i,j-1)));
  27. }
  28. }
  29. cout<<endl;
  30. }
Success #stdin #stdout 0s 3420KB
stdin
3 3
10 2 7
stdout
19 12 10