1 /**************************************************************
2 Problem: 1863
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:116 ms
7 Memory:3616 kb
8 ****************************************************************/
9
10 //BZOJ 1863
11 #include<cstdio>
12 #include<cstring>
13 #include<cstdlib>
14 #include<iostream>
15 #include<algorithm>
16 #define rep(i,n) for(int i=0;i<n;++i)
17 #define F(i,j,n) for(int i=j;i<=n;++i)
18 #define D(i,j,n) for(int i=j;i>=n;--i)
19 #define pb push_back
20 using namespace std;
21 typedef long long LL;
22 inline int getint(){
23 int r=1,v=0; char ch=getchar();
24 for(;!isdigit(ch);ch=getchar()) if (ch==‘-‘) r=-1;
25 for(; isdigit(ch);ch=getchar()) v=v*10-‘0‘+ch;
26 return r*v;
27 }
28 const int N=100010;
29 /*******************template********************/
30
31 int n;
32 LL a[N],f[N],g[N];
33 LL l,r,mid;
34 int main(){
35 #ifndef ONLINE_JUDGE
36 freopen("1863.in","r",stdin);
37 freopen("1863.out","w",stdout);
38 #endif
39 n=getint();
40 F(i,1,n) a[i]=getint();
41 F(i,1,n-1) l=max(l,a[i]+a[i+1]);
42 l=max(l,a[n]+a[1]),r=1e10;
43 if (n%2==0){ printf("%lld\n",l); return 0;}
44 while(l<r){
45 mid=l+r>>1;
46 f[1]=g[1]=a[1];
47 F(i,2,n){
48 f[i]=min(a[i],a[1]-g[i-1]);
49 g[i]=max(0LL,a[i]-(mid-a[i-1]-(a[1]-f[i-1])));
50 }
51 if (g[n]==0) r=mid;
52 else l=mid+1;
53 }
54 printf("%lld\n",l);
55 return 0;
56 }
57