Advertisement
Ankit_132

Warrior Chef

Sep 20th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n, h;
  14.         cin>>n>>h;
  15.  
  16.         vector<int> a(n);
  17.         for(auto &e: a)     cin>>e;
  18.  
  19.         int ans=0, low=0, high=1e9;
  20.  
  21.         auto check = [&](int x)
  22.         {
  23.             int tmp = h;
  24.  
  25.             for(auto e: a)
  26.             {
  27.                 if(e <= x)      continue;
  28.  
  29.                 tmp -= e;
  30.  
  31.                 if(tmp <= 0) return 0;
  32.             }
  33.  
  34.             return 1;
  35.         };
  36.  
  37.         while(low <= high)
  38.         {
  39.             int mid = (low + high)/2;
  40.  
  41.             if(check(mid))
  42.                 ans=mid, high=mid-1;
  43.             else
  44.                 low=mid+1;
  45.         }
  46.  
  47.         cout<<ans<<"\n";
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement