#include #include #include #include #include #include #include #include #include #define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1; #define fi first #define se second #define pb push_back #define pii pair #define ll long long #define pll pair #define rep(i,n) for(int i=0;i=0;i--) #define inf 1000000001 #define inf1 1000000000000000001 #define mod 1000000007 #define pie 3.14159265358979323846 #define N 1000005 #define mid(l,r) l+(r-l)/2 using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;} ll gcd(ll a,ll b){ if(a>b)swap(a,b);if(!a)return b;return gcd(b%a,a);} void solve(){ ll int n; cin>>n; ll zc=0,ex=0,ans=0; ll a[n]; rep(i,n)cin>>a[i]; rep(i,n){ ans+=zc+ex; if(a[i]>1){ ll d=1; if(zc)d=max(1ll,a[i]-zc); d=a[i]-d; if(d>zc){ex+=d-zc;zc=0;} else zc-=d; } else if(!a[i]){ if(ex)ex--; else zc++; } } cout<>t; t=1; while(t--){ solve(); } }