fork download
  1. #include <bits/stdc++.h>
  2. #define VuDucNam ios_base::sync_with_stdio(false);
  3. #define Lop9a6 cin.tie(NULL);
  4. #define THCSPhamVanDong cout.tie(NULL);
  5. #define ll long long
  6. #define el cout << '\n'
  7. #define sz(a) (ll) a.size()
  8. #define all(a) a.begin(), a.end()
  9. #define fi first
  10. #define se second
  11. #define fill(a, x) memset(a, x, sizeof(a))
  12. #define file(name) \
  13.   if (fopen(name ".inp", "r")) \
  14.   { \
  15.   freopen(name ".inp", "r", stdin); \
  16.   freopen(name ".out", "w", stdout); \
  17.   }
  18. #define TIME (1.0 * clock() / CLOCKS_PER_SEC)
  19. #define RUNTIME cerr << "\nRuntime: " << TIME << "s.\n"
  20.  
  21. using namespace std;
  22.  
  23. const int N5 = 1e5 + 5;
  24. const ll MOD = 1e9 + 7;
  25. const ll inf = LLONG_MAX;
  26.  
  27. int n;
  28. ll s,a[N5];
  29.  
  30. bool check(ll m)
  31. {
  32. ll ans=0;
  33. for(int i=1;i<=n;++i)
  34. if(a[i]>=m)
  35. ans+=a[i]-m;
  36. return (ans>=s);
  37. }
  38.  
  39. ll tknp()
  40. {
  41. ll l=0,r=*max_element(a+1,a+n+1);
  42. ll ans=0;
  43. while(l<=r){
  44. ll m=(l+r)/2;
  45. if(check(m)){
  46. ans=m;
  47. l=m+1;
  48. }else r=m-1;
  49. }
  50. return ans;
  51. }
  52.  
  53. void solve()
  54. {
  55. cin >> n >> s;
  56. for(int i=1;i<=n;++i)
  57. cin >> a[i];
  58. cout << tknp();
  59. el;
  60. }
  61.  
  62. int main()
  63. {
  64. VuDucNam Lop9a6 THCSPhamVanDong
  65. // file();
  66. solve();
  67. RUNTIME;
  68. return 0;
  69. }
Success #stdin #stdout #stderr 0.01s 5280KB
stdin
4 10
5 3 7 8
stdout
3
stderr
Runtime: 0.005423s.