import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;
 
void main() {
    int N; scanf("%d\n", &N);
    auto A = new ulong[N];
    foreach (i; 0 .. N) {
        scanf("%lld\n", &A[i]);
    }

    auto B = new ulong[5 * N + 3];
    B[0] = 0;
    foreach (i; 1 .. 5 * N + 3) {
        B[i] = B[i - 1] + A[(i - 1) % N];
    }

    bool C(ulong x) {
        int end(int begin) {
            int lb = begin, ub = begin + N;
            while (lb + 1 < ub) {
                int mid = (lb + ub) / 2;
                if (x <= B[mid] - B[begin]) {
                    ub = mid;
                } else {
                    lb = mid;
                }
            }
            return ub;
        }
        foreach (int i; 0 .. N) {
            int j = end(i);
            int k = end(j);
            if (end(k) - i <= N) return true;
        }
        return false;
    }


    ulong lb = 0, ub = B[N];
    while (lb + 1 < ub) {
        ulong mid = (lb + ub) / 2;
        if (C(mid)) {
            lb = mid;
        } else {
            ub = mid;
        }
    }

    writeln(lb);
}