Codeforces 954 G Castle Defense

Discription

Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into n sections. At this moment there are exactly ai archers located at the i-th section of this wall. You know that archer who stands at section i can shoot orcs that attack section located at distance not exceeding r, that is all such sections jthat |i - j| ≤ r. In particular, r = 0 means that archers are only capable of shooting at orcs who attack section i.

Denote as defense level of section i the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.

There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of k archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan.

Input

The first line of the input contains three integers nr and k (1 ≤ n ≤ 500 000, 0 ≤ r ≤ n0 ≤ k ≤ 1018) — the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the current number of archers at each section.

Output

Print one integer — the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute k additional archers optimally.

Example

Input
5 0 6
5 4 3 4 9
Output
5
Input
4 2 0
1 2 3 4
Output
6
Input
5 1 1
2 1 2 1 2
Output
3


NOIP水平的二分答案,,二分了一个值之后直接贪心

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000005;
ll a[maxn],b[maxn],k,now;
ll n,l=1<<30,r,mid,ans,R,alr;

inline bool calc(){
	memcpy(b,a,sizeof(a)),now=0,alr=0;
	for(int i=0;i<=R;i++) now+=b[i+1];
	for(int i=1;i<=n;i++,now+=b[i+R]){
		if(now<mid){
			alr+=mid-now;
			b[i+R]+=mid-now;
			now=mid;
			if(alr>k) return 0;
		}
		if(i>R) now-=b[i-R];
	}
	return 1;
}

int main(){
	scanf("%I64d%I64d%I64d",&n,&R,&k);
	for(int i=1;i<=n;i++){
		scanf("%I64d",a+i);
		l=min(l,a[i]);
	}
	r=2ll*1e18;
	while(l<=r){
		mid=l+r>>1;
		if(calc()) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%I64d\n",ans);
	return 0;
}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8672758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值