import core.stdc.stdio;
import std.algorithm;

void main(){
	int n;
	scanf("%d",&n);
	int[] a = new int[n];
	long sum;
	foreach(ref v;a){
		scanf("%d",&v);
		sum+=v;
	}
	bool Solve(long m){
		int g=114514;
		int c,e,f;
		long cs,es,fs=sum;
		while(c<g){
			bool ok = true;
			while(cs<m){
				cs+=a[e];
				es-=a[e];
				e=(e+1)%n;
				if(e==c)
					ok=false;
			}
			if(!ok)
				goto next;
			while(es<m){
				es+=a[f];
				fs-=a[f];
				f=(f+1)%n;
				if(f==c)
					ok=false;
			}
			if(!ok)
				goto next;
			if(fs>=m)
				return true;
			next:;
			g=min(g,e);
			fs+=a[c];
			cs-=a[c];
			c++;
		}
		return false;
	}
	long l=0;
	long r=sum/3+1;
	while(r-l>1){
		long m=(l+r)/2;
		if(Solve(m))
			l=m;
		else
			r=m;
	}
	printf("%lld\n",l);
}