fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int arr[1000000],woodscut[1000000];
  5. int main() {
  6. int n,m,maxheight=-10,wctd,minheight=0,mid;
  7. cin >> n >> m;
  8. for (int i=0;i<n;i++){
  9. cin >> arr[i];
  10. maxheight=max(arr[i],maxheight);
  11. }
  12. while (minheight<maxheight){
  13. mid=(maxheight+minheight)/2;
  14. for (int i=0;i<n;i++){
  15. wctd+=max(0,arr[i]-mid);
  16. }
  17. if (wctd<=m){
  18. maxheight=mid;
  19. wctd=0;
  20. }
  21. else{
  22. minheight=mid+1;
  23. wctd=0;
  24. }
  25. }
  26. cout << minheight;
  27. }
Success #stdin #stdout 0.01s 5700KB
stdin
5 20
4 42 40 26 46
stdout
36