fork(3) download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. long N, K;
  6. long* A;
  7.  
  8. long sum_depending_on_mid(int m) {
  9. long summ = 0;
  10. for (int i = 0; i <= N - 1 ; i++) {
  11. if(A[i] > m) summ += A[i] - m;
  12. }
  13. return summ;
  14. }
  15.  
  16. int main() {
  17. cin >> N >> K;
  18. A = new long[N];
  19. for (int i = 0; i < N; i++) {
  20. cin >> A[i];
  21. }
  22. sort(A, A + N);
  23. long low = 0;
  24. long high = A[N - 1];
  25. long mid = 0;
  26. while (low != high and sum_depending_on_mid(mid = (low + high) / 2) != K) {
  27. long past_low = low;
  28. long past_high = high;
  29. if (sum_depending_on_mid(mid) < K) {
  30. high = mid;
  31. } else low = mid;
  32. if (past_low == low and past_high == high) {
  33. cout << mid;
  34. return 0;
  35. }
  36. }
  37. cout << mid;
  38. delete[] A;
  39. }
Success #stdin #stdout 0s 4960KB
stdin
2 1
2 2
stdout
1