1 #include<cstdio>
2
3 #define maxn 100001
4
5 int min(int a,int b){return a<b?a:b;}
6 int max(int a,int b){return a>b?a:b;}
7
8 inline int in()
9 {
10 int x=0;char ch=getchar();
11 while(ch<‘0‘||ch>‘9‘)ch=getchar();
12 while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-‘0‘,ch=getchar();
13 return x;
14 }
15
16 int a[maxn+1],f[maxn],g[maxn],n;
17
18 bool judge(int ans)
19 {
20 f[1]=a[1];g[1]=a[1];
21 for(int i=2;i<=n;i++)
22 {
23 if(a[i]+a[i-1]>ans)return 0;
24 f[i]=min(a[1]-g[i-1],a[i]);
25 g[i]=max(0,a[1]-(ans-a[i-1]-(a[i]-f[i-1])));
26 if(f[i]<g[i])return 0;
27 }
28 return g[n]==0;
29 }
30
31 int main()
32 {
33 int l=1,r=0,ans;
34 n=in();
35 for(int i=1;i<=n;i++)a[i]=in(),r+=a[i],r=min(r,250000);
36 while(l<=r)
37 {
38 int mid=(l+r)>>1;
39 if(judge(mid))r=mid-1,ans=mid;
40 else l=mid+1;
41 }
42 printf("%d",ans);
43 return 0;
44 }