Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water wcontiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the maximum final height of the smallest flower.
6 2 3 2 2 2 2 1 1
2
2 5 1 5 8
9
二分枚举最小值,判断是否能达到
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 100005
#define MOD 1000000007
#define INF 1E9
using namespace std;
typedef long long ll;
int n, m, w;
ll num[maxn];
int p[maxn];
bool judge(ll k){
int d = m;
for(int i = 1; i <= n; i++){
p[i] += p[i-1];
if(num[i] + p[i] < k){
ll e = k - num[i] - p[i];
if(e > d)
return false;
p[i] += e;
int h = i + w;
if(h > n + 1)
h = n + 1;
p[h] = -e;
d -= e;
}
}
return true;
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &w);
for(int i = 1; i <= n; i++)
scanf("%I64d", num+i);
ll l = 0, r = 1e10;
while(l < r){
memset(p, 0, sizeof(p));
ll mid = (l + r) >> 1;
if(judge(mid))
l = mid + 1;
else
r = mid;
}
cout << l - 1 << endl;
return 0;
}