答案满足单调性,所以二分答案去搞就行
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
#define ll long long
#define maxn 100005
ll num[maxn];
ll M, K;
ll work(ll tmp)
{
ll sum = 0;
for (int i = 0; i < M; ++i)
{
if (num[i] > tmp)
sum += num[i] - tmp;
}
return sum;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%I64d%I64d", &M, &K);
for (int i = 0; i < M; ++i)
scanf("%I64d", &num[i]);
ll sum = 0, mx = 0;
for (int i = 0; i < M; ++i)
{
sum += num[i];
if (num[i] > mx)
mx = num[i];
}
ll x, y, m;
x = sum / M;
if (sum%M)
++x;
y = mx;
while (x<y)
{
m = (x + y) >> 1;
if (work(m)<=K)
y = m;
else
x = m + 1;
}
ll ans = x;
printf("%I64d\n", ans);
//system("pause");
//while (1);
return 0;
}