This question can be directly divided into two parts. Pay attention to the return value of the second part. It is best to manually simulate it in the future.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; typedef long int int64; int64 n, m, seq [1000005]; int64 sum (int64 h) {int64 ret = 0; For (INT I = 1; I <= N; ++ I) {If (SEQ [I]> H) {RET + = (SEQ [I]-h) ;}} return ret;} int64 bsearch (int64 L, int64 R) {int64 mid, TOT; while (L <= r) {mid = (L + r)> 1; Tot = sum (MID); If (TOT> m) {L = Mid + 1 ;} else if (TOT <m) {r = mid-1;} else return mid;} return r;} int main () {int64 Max; while (scanf ("% i64d % i64d", & N, & M) = 2) {max =-1; for (INT I = 1; I <= N; ++ I) {scanf ("% i64d", & seq [I]); max = max (max, seq [I]);} printf ("% i64d \ n", bsearch (0, max);} return 0 ;}