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);
}
; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/D/Source_19376/source.d'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/D/Source_19376/source.d"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

%0 = type { i32, i32, [7 x i8] }
%object.TypeInfo_Array = type { [19 x i8*]*, i8*, %object.TypeInfo* }
%object.TypeInfo = type { [19 x i8*]*, i8* }
%object.ModuleInfo = type { i32, i32 }
%nest.main = type { i64, { i64, i32* }, i32 }

@.str = private unnamed_addr constant [3 x i8] c"%d\00" ; [#uses = 1]
@_D11TypeInfo_Ai6__initZ = external global %object.TypeInfo_Array ; [#uses = 1]
@.str.1 = private unnamed_addr constant [62 x i8] c"/data/TheStack_IR/OJ_Samples/Collated/D/Source_19376/source.d\00" ; [#uses = 1]
@.str.2 = private unnamed_addr constant [6 x i8] c"%lld\0A\00" ; [#uses = 1]
@_D6source12__ModuleInfoZ = global %0 { i32 -2147483644, i32 0, [7 x i8] c"source\00" } ; [#uses = 1]
@_D6source11__moduleRefZ = linkonce_odr hidden global %object.ModuleInfo* bitcast (%0* @_D6source12__ModuleInfoZ to %object.ModuleInfo*), section "__minfo" ; [#uses = 1]
@__start___minfo = external hidden global %object.ModuleInfo* ; [#uses = 1]
@__stop___minfo = external hidden global %object.ModuleInfo* ; [#uses = 1]
@ldc.dso_slot = linkonce_odr hidden global i8* null ; [#uses = 1]
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ldc.register_dso, i8* bitcast (void ()* @ldc.register_dso to i8*) }] ; [#uses = 0]
@llvm.global_dtors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ldc.register_dso, i8* bitcast (void ()* @ldc.register_dso to i8*) }] ; [#uses = 0]
@llvm.used = appending global [1 x i8*] [i8* bitcast (%object.ModuleInfo** @_D6source11__moduleRefZ to i8*)], section "llvm.metadata" ; [#uses = 0]

; [#uses = 1]
; Function Attrs: uwtable
define i32 @_Dmain({ i64, { i64, i8* }* } %unnamed) #0 {
  %.frame = alloca %nest.main, align 8            ; [#uses = 5, size/byte = 32]
  %sum = getelementptr inbounds %nest.main, %nest.main* %.frame, i64 0, i32 0 ; [#uses = 3, type = i64*]
  %n = getelementptr inbounds %nest.main, %nest.main* %.frame, i64 0, i32 2 ; [#uses = 3, type = i32*]
  store i32 0, i32* %n, align 8
  %1 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* nonnull %n) #5 ; [#uses = 0]
  %2 = load i32, i32* %n, align 8                 ; [#uses = 1]
  %3 = sext i32 %2 to i64                         ; [#uses = 3]
  %.gc_mem = call { i64, i8* } @_d_newarrayT(%object.TypeInfo* bitcast (%object.TypeInfo_Array* @_D11TypeInfo_Ai6__initZ to %object.TypeInfo*), i64 %3) #2 ; [#uses = 1]
  %.ptr = extractvalue { i64, i8* } %.gc_mem, 1   ; [#uses = 2]
  %4 = getelementptr inbounds %nest.main, %nest.main* %.frame, i64 0, i32 1, i32 0 ; [#uses = 1, type = i64*]
  store i64 %3, i64* %4, align 8
  %5 = getelementptr inbounds %nest.main, %nest.main* %.frame, i64 0, i32 1, i32 1 ; [#uses = 1, type = i32**]
  %6 = bitcast i32** %5 to i8**                   ; [#uses = 1]
  store i8* %.ptr, i8** %6, align 8
  store i64 0, i64* %sum, align 8
  %.ptr1.cast = bitcast i8* %.ptr to i32*         ; [#uses = 1]
  br label %forcond

forcond:                                          ; preds = %forbody, %0
  %7 = phi i64 [ 1, %0 ], [ %phi.bo14, %forbody ] ; [#uses = 1, type = i64]
  %__key103.0 = phi i64 [ 0, %0 ], [ %14, %forbody ] ; [#uses = 3, type = i64]
  %exitcond.not = icmp eq i64 %__key103.0, %3     ; [#uses = 1]
  br i1 %exitcond.not, label %endfor, label %forbody

forbody:                                          ; preds = %forcond
  %8 = getelementptr inbounds i32, i32* %.ptr1.cast, i64 %__key103.0 ; [#uses = 2, type = i32*]
  %9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8) #5 ; [#uses = 0]
  %10 = load i32, i32* %8, align 4                ; [#uses = 1]
  %11 = sext i32 %10 to i64                       ; [#uses = 1]
  %12 = load i64, i64* %sum, align 8              ; [#uses = 1]
  %13 = add i64 %12, %11                          ; [#uses = 2]
  store i64 %13, i64* %sum, align 8
  %14 = add i64 %__key103.0, 1                    ; [#uses = 1]
  %phi.bo = sdiv i64 %13, 3                       ; [#uses = 1]
  %phi.bo14 = add nsw i64 %phi.bo, 1              ; [#uses = 1]
  br label %forcond

endfor:                                           ; preds = %forcond
  %15 = bitcast %nest.main* %.frame to i8*        ; [#uses = 1]
  br label %forcond4

forcond4:                                         ; preds = %forbody5, %endfor
  %l.0 = phi i64 [ 0, %endfor ], [ %.l.0, %forbody5 ] ; [#uses = 4, type = i64]
  %r.0 = phi i64 [ %7, %endfor ], [ %r.0., %forbody5 ] ; [#uses = 3, type = i64]
  %16 = sub i64 %r.0, %l.0                        ; [#uses = 1]
  %17 = icmp sgt i64 %16, 1                       ; [#uses = 1]
  br i1 %17, label %forbody5, label %endfor7

forbody5:                                         ; preds = %forcond4
  %18 = add i64 %r.0, %l.0                        ; [#uses = 1]
  %19 = sdiv i64 %18, 2                           ; [#uses = 3]
  %20 = call zeroext i1 @_D6source4mainFZ5SolveMFNaNbNiNflZb(i8* nonnull %15, i64 %19) #3 ; [#uses = 2]
  %.l.0 = select i1 %20, i64 %19, i64 %l.0        ; [#uses = 1]
  %r.0. = select i1 %20, i64 %r.0, i64 %19        ; [#uses = 1]
  br label %forcond4

endfor7:                                          ; preds = %forcond4
  %21 = call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %l.0) #5 ; [#uses = 0]
  ret i32 0
}

; [#uses = 2]
; Function Attrs: nofree nounwind
declare i32 @scanf(i8* nocapture readonly, ...) local_unnamed_addr #1

; [#uses = 1]
; Function Attrs: uwtable
declare { i64, i8* } @_d_newarrayT(%object.TypeInfo*, i64) local_unnamed_addr #2

; [#uses = 1]
; Function Attrs: uwtable
define zeroext i1 @_D6source4mainFZ5SolveMFNaNbNiNflZb(i8* nocapture nonnull readonly %.nest_arg, i64 %m_arg) local_unnamed_addr #3 {
  %sum = bitcast i8* %.nest_arg to i64*           ; [#uses = 1]
  %1 = load i64, i64* %sum, align 8               ; [#uses = 1]
  %a = getelementptr inbounds i8, i8* %.nest_arg, i64 8 ; [#uses = 1, type = i8*]
  %2 = bitcast i8* %a to i64*                     ; [#uses = 3]
  %3 = getelementptr inbounds i8, i8* %.nest_arg, i64 16 ; [#uses = 1, type = i8*]
  %4 = bitcast i8* %3 to i32**                    ; [#uses = 3]
  %n = getelementptr inbounds i8, i8* %.nest_arg, i64 24 ; [#uses = 1, type = i8*]
  %5 = bitcast i8* %n to i32*                     ; [#uses = 2]
  br label %forcond

forcond:                                          ; preds = %bounds.ok41, %0
  %g.0 = phi i32 [ 114514, %0 ], [ %b_arg.a_arg.i, %bounds.ok41 ] ; [#uses = 3, type = i32]
  %c.0 = phi i32 [ 0, %0 ], [ %38, %bounds.ok41 ] ; [#uses = 5, type = i32]
  %e.0 = phi i32 [ 0, %0 ], [ %e.1, %bounds.ok41 ] ; [#uses = 1, type = i32]
  %f.0 = phi i32 [ 0, %0 ], [ %f.2, %bounds.ok41 ] ; [#uses = 2, type = i32]
  %cs.0 = phi i64 [ 0, %0 ], [ %37, %bounds.ok41 ] ; [#uses = 1, type = i64]
  %es.0 = phi i64 [ 0, %0 ], [ %es.3, %bounds.ok41 ] ; [#uses = 1, type = i64]
  %fs.0 = phi i64 [ %1, %0 ], [ %36, %bounds.ok41 ] ; [#uses = 2, type = i64]
  %6 = icmp slt i32 %c.0, %g.0                    ; [#uses = 1]
  br i1 %6, label %forcond1, label %if36

forcond1:                                         ; preds = %forcond, %bounds.ok
  %e.1 = phi i32 [ %16, %bounds.ok ], [ %e.0, %forcond ] ; [#uses = 5, type = i32]
  %cs.1 = phi i64 [ %12, %bounds.ok ], [ %cs.0, %forcond ] ; [#uses = 3, type = i64]
  %es.1 = phi i64 [ %13, %bounds.ok ], [ %es.0, %forcond ] ; [#uses = 3, type = i64]
  %ok.0 = phi i8 [ %ok.1, %bounds.ok ], [ 1, %forcond ] ; [#uses = 3, type = i8]
  %7 = icmp slt i64 %cs.1, %m_arg                 ; [#uses = 1]
  br i1 %7, label %forbody2, label %endfor4

forbody2:                                         ; preds = %forcond1
  %8 = sext i32 %e.1 to i64                       ; [#uses = 3]
  %.len = load i64, i64* %2, align 8              ; [#uses = 2]
  %bounds.cmp = icmp ugt i64 %.len, %8            ; [#uses = 1]
  br i1 %bounds.cmp, label %bounds.ok, label %bounds.fail

bounds.ok:                                        ; preds = %forbody2
  %.ptr = load i32*, i32** %4, align 8            ; [#uses = 1]
  %9 = getelementptr inbounds i32, i32* %.ptr, i64 %8 ; [#uses = 1, type = i32*]
  %10 = load i32, i32* %9, align 4                ; [#uses = 1]
  %11 = sext i32 %10 to i64                       ; [#uses = 2]
  %12 = add i64 %cs.1, %11                        ; [#uses = 1]
  %13 = sub i64 %es.1, %11                        ; [#uses = 1]
  %14 = add i32 %e.1, 1                           ; [#uses = 1]
  %15 = load i32, i32* %5, align 4                ; [#uses = 1]
  %16 = srem i32 %14, %15                         ; [#uses = 2]
  %17 = icmp eq i32 %16, %c.0                     ; [#uses = 1]
  %ok.1 = select i1 %17, i8 0, i8 %ok.0           ; [#uses = 1]
  br label %forcond1

bounds.fail:                                      ; preds = %forbody2
  tail call void @_d_arraybounds_index({ i64, i8* } { i64 61, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i32 0, i32 0) }, i32 20, i64 %8, i64 %.len) #4
  unreachable

endfor4:                                          ; preds = %forcond1
  %18 = and i8 %ok.0, 1                           ; [#uses = 1]
  %.not = icmp eq i8 %18, 0                       ; [#uses = 1]
  br i1 %.not, label %label.next, label %forcond13

forcond13:                                        ; preds = %endfor4, %bounds.ok20
  %f.1 = phi i32 [ %28, %bounds.ok20 ], [ %f.0, %endfor4 ] ; [#uses = 3, type = i32]
  %es.2 = phi i64 [ %24, %bounds.ok20 ], [ %es.1, %endfor4 ] ; [#uses = 3, type = i64]
  %fs.1 = phi i64 [ %25, %bounds.ok20 ], [ %fs.0, %endfor4 ] ; [#uses = 3, type = i64]
  %ok.2 = phi i8 [ %ok.3, %bounds.ok20 ], [ %ok.0, %endfor4 ] ; [#uses = 2, type = i8]
  %19 = icmp slt i64 %es.2, %m_arg                ; [#uses = 1]
  br i1 %19, label %forbody14, label %endfor16

forbody14:                                        ; preds = %forcond13
  %20 = sext i32 %f.1 to i64                      ; [#uses = 3]
  %.len18 = load i64, i64* %2, align 8            ; [#uses = 2]
  %bounds.cmp19 = icmp ugt i64 %.len18, %20       ; [#uses = 1]
  br i1 %bounds.cmp19, label %bounds.ok20, label %bounds.fail21

bounds.ok20:                                      ; preds = %forbody14
  %.ptr22 = load i32*, i32** %4, align 8          ; [#uses = 1]
  %21 = getelementptr inbounds i32, i32* %.ptr22, i64 %20 ; [#uses = 1, type = i32*]
  %22 = load i32, i32* %21, align 4               ; [#uses = 1]
  %23 = sext i32 %22 to i64                       ; [#uses = 2]
  %24 = add i64 %es.2, %23                        ; [#uses = 1]
  %25 = sub i64 %fs.1, %23                        ; [#uses = 1]
  %26 = add i32 %f.1, 1                           ; [#uses = 1]
  %27 = load i32, i32* %5, align 4                ; [#uses = 1]
  %28 = srem i32 %26, %27                         ; [#uses = 2]
  %29 = icmp eq i32 %28, %c.0                     ; [#uses = 1]
  %ok.3 = select i1 %29, i8 0, i8 %ok.2           ; [#uses = 1]
  br label %forcond13

bounds.fail21:                                    ; preds = %forbody14
  tail call void @_d_arraybounds_index({ i64, i8* } { i64 61, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i32 0, i32 0) }, i32 29, i64 %20, i64 %.len18) #4
  unreachable

endfor16:                                         ; preds = %forcond13
  %30 = and i8 %ok.2, 1                           ; [#uses = 1]
  %.not77 = icmp eq i8 %30, 0                     ; [#uses = 1]
  %.not78 = icmp slt i64 %fs.1, %m_arg            ; [#uses = 1]
  %or.cond = or i1 %.not78, %.not77               ; [#uses = 1]
  br i1 %or.cond, label %label.next, label %if36

if36:                                             ; preds = %endfor16, %forcond
  %merge = phi i1 [ false, %forcond ], [ true, %endfor16 ] ; [#uses = 1, type = i1]
  ret i1 %merge

label.next:                                       ; preds = %endfor16, %endfor4
  %f.2 = phi i32 [ %f.0, %endfor4 ], [ %f.1, %endfor16 ] ; [#uses = 1, type = i32]
  %es.3 = phi i64 [ %es.1, %endfor4 ], [ %es.2, %endfor16 ] ; [#uses = 1, type = i64]
  %fs.2 = phi i64 [ %fs.0, %endfor4 ], [ %fs.1, %endfor16 ] ; [#uses = 1, type = i64]
  %31 = sext i32 %c.0 to i64                      ; [#uses = 3]
  %.len39 = load i64, i64* %2, align 8            ; [#uses = 2]
  %bounds.cmp40 = icmp ugt i64 %.len39, %31       ; [#uses = 1]
  br i1 %bounds.cmp40, label %bounds.ok41, label %bounds.fail42

bounds.ok41:                                      ; preds = %label.next
  %32 = icmp slt i32 %e.1, %g.0                   ; [#uses = 1]
  %b_arg.a_arg.i = select i1 %32, i32 %e.1, i32 %g.0 ; [#uses = 1]
  %.ptr43 = load i32*, i32** %4, align 8          ; [#uses = 1]
  %33 = getelementptr inbounds i32, i32* %.ptr43, i64 %31 ; [#uses = 1, type = i32*]
  %34 = load i32, i32* %33, align 4               ; [#uses = 1]
  %35 = sext i32 %34 to i64                       ; [#uses = 2]
  %36 = add i64 %fs.2, %35                        ; [#uses = 1]
  %37 = sub i64 %cs.1, %35                        ; [#uses = 1]
  %38 = add nuw i32 %c.0, 1                       ; [#uses = 1]
  br label %forcond

bounds.fail42:                                    ; preds = %label.next
  tail call void @_d_arraybounds_index({ i64, i8* } { i64 61, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i32 0, i32 0) }, i32 41, i64 %31, i64 %.len39) #4
  unreachable
}

; [#uses = 3]
; Function Attrs: cold noreturn uwtable
declare void @_d_arraybounds_index({ i64, i8* }, i32, i64, i64) local_unnamed_addr #4

; [#uses = 0]
; Function Attrs: uwtable
define weak_odr i32 @_D3std9algorithm10comparison__T3minTiTiZQjFNaNbNiNfiiZi(i32 %b_arg, i32 %a_arg) local_unnamed_addr #3 {
condend:
  %0 = icmp slt i32 %b_arg, %a_arg                ; [#uses = 1]
  %b_arg.a_arg = select i1 %0, i32 %b_arg, i32 %a_arg ; [#uses = 1]
  ret i32 %b_arg.a_arg
}

; [#uses = 1]
; Function Attrs: nofree nounwind
declare i32 @printf(i8* nocapture readonly, ...) local_unnamed_addr #1

; [#uses = 1]
declare i32 @_d_run_main(i32, i8**, i8*) local_unnamed_addr #5

; [#uses = 0]
; Function Attrs: uwtable
define i32 @main(i32 %argc_arg, i8** %argv_arg) local_unnamed_addr #3 {
  %1 = tail call i32 @_d_run_main(i32 %argc_arg, i8** %argv_arg, i8* bitcast (i32 ({ i64, { i64, i8* }* })* @_Dmain to i8*)) #5 ; [#uses = 1]
  ret i32 %1
}

; [#uses = 2]
define linkonce_odr hidden void @ldc.register_dso() {
  %1 = alloca { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }, align 8 ; [#uses = 5, size/byte = 32]
  %2 = getelementptr inbounds { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }, { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }* %1, i64 0, i32 0 ; [#uses = 1, type = i64*]
  store i64 1, i64* %2, align 8
  %3 = getelementptr inbounds { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }, { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }* %1, i64 0, i32 1 ; [#uses = 1, type = i8***]
  store i8** @ldc.dso_slot, i8*** %3, align 8
  %4 = getelementptr inbounds { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }, { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }* %1, i64 0, i32 2 ; [#uses = 1, type = %object.ModuleInfo***]
  store %object.ModuleInfo** @__start___minfo, %object.ModuleInfo*** %4, align 8
  %5 = getelementptr inbounds { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }, { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }* %1, i64 0, i32 3 ; [#uses = 1, type = %object.ModuleInfo***]
  store %object.ModuleInfo** @__stop___minfo, %object.ModuleInfo*** %5, align 8
  %6 = bitcast { i64, i8**, %object.ModuleInfo**, %object.ModuleInfo** }* %1 to i8* ; [#uses = 1]
  call void @_d_dso_registry(i8* nonnull %6)
  ret void
}

; [#uses = 1]
; Function Attrs: uwtable
declare void @_d_dso_registry(i8*) local_unnamed_addr #2

attributes #0 = { uwtable "frame-pointer"="none" "target-cpu"="x86-64" "target-features"="+cx16,+cx16" }
attributes #1 = { nofree nounwind "frame-pointer"="none" "target-cpu"="x86-64" "target-features"="+cx16" }
attributes #2 = { uwtable }
attributes #3 = { uwtable "frame-pointer"="none" "target-cpu"="x86-64" "target-features"="+cx16" }
attributes #4 = { cold noreturn uwtable }
attributes #5 = { "frame-pointer"="none" "target-cpu"="x86-64" "target-features"="+cx16" }

!llvm.ldc.typeinfo._D11TypeInfo_Ai6__initZ = !{!0}
!llvm.ident = !{!1}

!0 = !{{ i64, i32* } undef}
!1 = !{!"ldc version 1.28.0"}
