fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. bool isPossible(int n, int k, int total, int mid){
  33.  
  34. if(total - mid <= k) return true;
  35.  
  36. int revSum = 0;
  37. for(int i=n-1; i>=max(1LL, n-mid); i--){
  38. revSum += a[i];
  39.  
  40. int sum = total - a[0] - revSum + (n-i+1)*(a[0] - (mid-(n-i)));
  41. if(sum <= k) return true;
  42. }
  43.  
  44. return false;
  45.  
  46. }
  47.  
  48. int consistency(int n, int k){
  49. if(n==1){
  50. if(a[0] > k) return a[0]-k;
  51. }
  52.  
  53. sort(begin(a), end(a));
  54.  
  55.  
  56. int total = 0;
  57. for(auto& it : a) total += it;
  58. if(k>=total) return 0;
  59.  
  60. int s = 0;
  61. int e = INF;
  62.  
  63. while(s<e){
  64. int mid = s + (e-s)/2;
  65. if(isPossible(n, k, total, mid)){
  66. e = mid;
  67. }
  68. else s = mid+1;
  69. }
  70.  
  71. return e;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. int practice(int n, int k){
  89.  
  90.  
  91. return 0;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. void solve() {
  99.  
  100. int n, k;
  101. cin>> n >> k;
  102.  
  103. a.resize(n);
  104. for(int i=0; i<n; i++) cin >> a[i];
  105.  
  106. cout << consistency(n, k) << endl;
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. int32_t main() {
  116. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  117.  
  118. int t = 1;
  119. cin >> t;
  120. while (t--) {
  121. solve();
  122. }
  123.  
  124. return 0;
  125. }
Success #stdin #stdout 0s 5316KB
stdin
4
1 10
20
2 69
6 9
7 8
1 2 1 3 1 2 1
10 1
1 2 3 1 2 6 1 6 8 10
stdout
10
0
2
7