def d(p, q):

    return pos[p] - pos[q]



def solve():

    p0, p1, p2 = 0, 0, 0

    best = 1

    while p0 < N:



        if p1 <= p0: p1 = p0 + 1

        while d(p1, p0) <= best and p1 - p0 < N:

            p1 += 1

        if d(p1, p0) <= best:

            p0 += 1

            while d(p2, p1) > d(p1, p0) and p2 > p1:

                p2 -= 1

            continue





        if p2 <= p1: p2 = p1 + 1

        while d(p2,p1) < d(p1, p0) and p2 - p0 < N:

            p2 += 1

        if d(p2, p1) < d(p1, p0):

            p0 += 1

            while d(p2, p1) > d(p1, p0) and p2 > p1:

                p2 -= 1

            continue



        if L - d(p2, p0) >= d(p1, p0):

            best = d(p1, p0)

            if best >= L//3:

                return best

        else:

            p0 += 1

            while d(p2, p1) > d(p1, p0) and p2 > p1:

                p2 -= 1

    return best



N = int(eval(input()))

A = [0] * N

L = 0

for i in range(N):

    A[i] = int(eval(input()))

    L += A[i]

pos = [0] * (2*N)

for i in range(1, 2*N):

    pos[i] = pos[i-1] + A[(i-1)%N]



print((solve()))
def solve():

    p0, p1, p2 = 0, 1, 2

    best = 1

    succ = True

    while p0 < N-1:

        if not succ:

            p0 += 1

            while d[p2]- d[p1] > d[p1]- d[p0]:

                p2 -= 1



        succ = False



        while p1 - p0 < N - 1 and d[p1] - d[p0] <= best:

            p1 += 1

        d0 = d[p1] - d[p0]

        if d0 <= best:

            continue



        while p2 - p0 < N and d[p2] - d[p1] < d0:

            p2 += 1

        if d[p2] - d[p1] < d0:

            continue



        if L - (d[p2] - d[p0]) >= d0:

            best = d0

            if best >= L//3:

                return best

            succ = True



    return best



N = int(eval(input()))

A = [0] * N

L = 0

for i in range(N):

    A[i] = int(eval(input()))

    L += A[i]

d = [0] * (2*N)

for i in range(1, 2*N):

    d[i] = d[i-1] + A[(i-1)%N]



print((solve()))
def d(p, q):
    return pos[p] - pos[q]


def solve():
    p0, p1, p2 = 0, 0, 0
    best = 1
    while p0 < N:
        if p1 <= p0:
            p1 = p0 + 1
        while d(p1, p0) <= best and p1 - p0 < N:
            p1 += 1
        if d(p1, p0) <= best:
            p0 += 1
            while d(p2, p1) > d(p1, p0) and p2 > p1:
                p2 -= 1
            continue
        if p2 <= p1:
            p2 = p1 + 1
        while d(p2, p1) < d(p1, p0) and p2 - p0 < N:
            p2 += 1
        if d(p2, p1) < d(p1, p0):
            p0 += 1
            while d(p2, p1) > d(p1, p0) and p2 > p1:
                p2 -= 1
            continue
        if L - d(p2, p0) >= d(p1, p0):
            best = d(p1, p0)
            if best >= L // 3:
                return best
        else:
            p0 += 1
            while d(p2, p1) > d(p1, p0) and p2 > p1:
                p2 -= 1
    return best


N = int(eval(input()))
A = [0] * N
L = 0
for i in range(N):
    A[i] = int(eval(input()))
    L += A[i]
pos = [0] * (2 * N)
for i in range(1, 2 * N):
    pos[i] = pos[i - 1] + A[(i - 1) % N]
print((solve()))

def solve():
    p0, p1, p2 = 0, 1, 2
    best = 1
    succ = True
    while p0 < N - 1:
        if not succ:
            p0 += 1
            while d[p2] - d[p1] > d[p1] - d[p0]:
                p2 -= 1
        succ = False
        while p1 - p0 < N - 1 and d[p1] - d[p0] <= best:
            p1 += 1
        d0 = d[p1] - d[p0]
        if d0 <= best:
            continue
        while p2 - p0 < N and d[p2] - d[p1] < d0:
            p2 += 1
        if d[p2] - d[p1] < d0:
            continue
        if L - (d[p2] - d[p0]) >= d0:
            best = d0
            if best >= L // 3:
                return best
            succ = True
    return best


N = int(eval(input()))
A = [0] * N
L = 0
for i in range(N):
    A[i] = int(eval(input()))
    L += A[i]
d = [0] * (2 * N)
for i in range(1, 2 * N):
    d[i] = d[i - 1] + A[(i - 1) % N]
print((solve()))

-def d(p, q): -    return pos[p] - pos[q] - - -    p0, p1, p2 = 0, 0, 0 +    p0, p1, p2 = 0, 1, 2 -    while p0 < N: -        if p1 <= p0: -            p1 = p0 + 1 -        while d(p1, p0) <= best and p1 - p0 < N: +    succ = True +    while p0 < N - 1: +        if not succ: +            p0 += 1 +            while d[p2] - d[p1] > d[p1] - d[p0]: +                p2 -= 1 +        succ = False +        while p1 - p0 < N - 1 and d[p1] - d[p0] <= best: -        if d(p1, p0) <= best: -            p0 += 1 -            while d(p2, p1) > d(p1, p0) and p2 > p1: -                p2 -= 1 +        d0 = d[p1] - d[p0] +        if d0 <= best: -        if p2 <= p1: -            p2 = p1 + 1 -        while d(p2, p1) < d(p1, p0) and p2 - p0 < N: +        while p2 - p0 < N and d[p2] - d[p1] < d0: -        if d(p2, p1) < d(p1, p0): -            p0 += 1 -            while d(p2, p1) > d(p1, p0) and p2 > p1: -                p2 -= 1 +        if d[p2] - d[p1] < d0: -        if L - d(p2, p0) >= d(p1, p0): -            best = d(p1, p0) +        if L - (d[p2] - d[p0]) >= d0: +            best = d0 -        else: -            p0 += 1 -            while d(p2, p1) > d(p1, p0) and p2 > p1: -                p2 -= 1 +            succ = True -pos = [0] * (2 * N) +d = [0] * (2 * N) -    pos[i] = pos[i - 1] + A[(i - 1) % N] +    d[i] = d[i - 1] + A[(i - 1) % N]