Saleh127

CF 1369C

Jul 3rd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define test int t; cin>>t; while (t--)
  4. #define ll long long int
  5. int main()
  6. {
  7. test
  8. {
  9. ll d,e=0,f,n,k,i,j;
  10. cin>>n>>k;
  11. ll a[n];
  12. for(i=0;i<n;i++)
  13. {
  14. cin>>a[i];
  15. }
  16. ll c[k];
  17. for(i=0;i<k;i++)
  18. {
  19. cin>>c[i];
  20. }
  21. sort(c,c+k);
  22. sort(a,a+n);
  23. d=n-1;
  24. f=n-1-k;
  25. for(j=0;j<k;j++)
  26. {
  27. if(c[j]==1)
  28. {
  29. e+=(2*a[d]);
  30. d--;
  31. }
  32. else
  33. {
  34. e+=a[d];
  35. d--;
  36. f=f-c[j]+2;
  37. e+=a[f];
  38. f--;
  39. }
  40. }
  41. cout<<e<<endl;
  42. }
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment