改了半天了,求求大佬帮忙看看吧
#include<bits/stdc++.h>
#define forr(i,a,b) for(int i=(a);i<(b);i++)
#define INIT() do{\
ios::sync_with_stdio(0); \
cin.tie(0);\
cout.tie(0);\
}while(0);
//#define endl '\n'
//typedef unsigned long long ull;
const int N = 1e7;
using namespace std;
int arr[N]{}, n, m;
static bool check(int val){ // 检查是否满足条件
int sum = 0;
forr(i, 0, n) {
if(arr[i]>val)
sum += arr[i] - val;
}
if (sum >= m) return true;
return false;
}
static void function1() {
int max_tree = -1, left = 1, mid = 0;
cin >> n >> m;
forr(i, 0, n) { // 读入数据
cin >> arr[i];
max_tree = max(arr[i], max_tree);
}
while (left<max_tree) { // 二分求答案
mid = (left + max_tree + 1) >> 1;
if (check(mid)) {
left = mid;
}
else {
max_tree = mid - 1;
}
}
cout << left;
}
int main() {
INIT();
function1();
return 0;
}