← Home
#include<bits/stdc++.h>
using namespace std;const int N=5e5+7,S=1<<20,B=8;typedef long long ll;
int n,i;ll ps[N];
template<bool dir>
struct ConvexHullTree{
	vector<int> t[S/B/2];
	bool cmp(int a,int b,ll c){return (ps[c]-ps[b])*(c-a)<(ps[c]-ps[a])*(c-b);}
	void build(int i,int L,int R) {
		if(R<=L+B)return;int st=L,ed=R;
		if(!dir)swap(--st,--ed);
		for(int j=st,d=(ed>st)-(ed<st);j!=ed;j+=d){
			auto&ch=t[i];
			while(ch.size()>=2){
				if(cmp(ch.end()[-2],ch.back(),j))break;
				ch.pop_back();
			}ch.push_back(j);
		}build(2*i+1,L,(L+R)/2);build(2*i+2,(L+R)/2,R);
	}
	void build(){return build(0,0,n);}
	int tangent(int i,int L,int R,int l,int r,int j){
		if(R<=l||r<=L)return l;
		if(R==L+1)return L;
		if(l<=L&&R<=r){
			if(R<=L+B){
				int x=L;
				for(int i=L;++i<R;)if(cmp(x,i,j))x=i;
				return x;
			}
			int l=0,r=t[i].size();
			while(r!=l+1){
				int m=(l+r)/2;
				(cmp(t[i][m-1],t[i][m],j)?l:r)=m;
			}return t[i][l];
		}
		auto al = tangent(2*i+1,L,(L+R)/2,l,r,j);
		auto ar = tangent(2*i+2,(L+R)/2,R,l,r,j);
		return cmp(al,ar,j)?ar:al;
	}
	int tangent(int l,int r,int j){return tangent(0,0,n,l,r,j);}
};
ConvexHullTree<0> low;
ConvexHullTree<1> high;
ll get_cost(int l,int r,int paid){
	while(r>l+1&&ps[l]+paid>ps[l+1])++l;
	while(r>l+1&&ps[r]-paid<ps[r-1])--r;
	if(r<=l+1)return max({},ps[r]-ps[l]+1-paid);
	int x0=low.tangent(l+1,r+1,l);
	if(ps[x0]<ps[l]+paid*ll(x0-l)){
		return get_cost(l,x0,paid)+get_cost(x0,r,paid);
	}
	int x1=high.tangent(l,r,r);
	if(ps[x1]>ps[r]-paid*ll(r-x1)){
		return get_cost(l,x1,paid)+get_cost(x1,r,paid);
	}
	int cur=min((ps[x0]-ps[l])/(x0-l),(ps[r]-ps[x1])/(r-x1))+1-paid;
	return cur+get_cost(l,r,paid+cur);
}
int main(){
	for(scanf("%d",&n),i=1;i<=n;++i)scanf("%lld",ps+i),ps[i]+=ps[i-1];++n;
	low.build(0,0,n);high.build(0,0,n);printf("%lld\n",get_cost(0,n-1,0));
}