fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #define maxN 1000001
  5. using namespace std;
  6. int n , k , a[maxN];
  7. long long total;
  8.  
  9. int cuttree(int tree)
  10. {
  11. return total - (tree*n);
  12. }
  13.  
  14. int Solve(int h)
  15. {
  16. int i = 1 , j = a[n] , mid , x;
  17. while(i <= j)
  18. {
  19. mid = (i+j)/2;
  20. x = cuttree(a[mid]);
  21. if (x > h)
  22. j = mid-1;
  23. else i = mid+1;
  24. }
  25. return x;
  26. }
  27.  
  28. void ReadData()
  29. {
  30. int h;
  31. cin >> n >> k;
  32. for (int i = 1; i <= n; ++i){
  33. cin >> a[i];
  34. total = total + a[i];
  35. }
  36. sort(a+1 , a+n+1);
  37. while(--k){
  38. cin >> h;
  39. cout << Solve(h) << "\n";
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45. ReadData();
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5264KB
stdin
4 2
20 15 10 17
7 4
stdout
-18