#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int INF=2e14+7;
int n,a[N];
vector <int> dp;
bool check(int mid){
for(int i=0;i<n;i++){
//cout << i << " : ";
int tmp1=dp[i];
if(lower_bound(dp.begin(),dp.end(),tmp1+mid)==dp.end()) continue;
int idx1=lower_bound(dp.begin(),dp.end(),tmp1+mid)-dp.begin();
int tmp2=dp[idx1];
if(lower_bound(dp.begin(),dp.end(),tmp2+mid)==dp.end()) continue;
int idx2=lower_bound(dp.begin(),dp.end(),tmp2+mid)-dp.begin();
int tmp3=dp[idx2];
if(lower_bound(dp.begin(),dp.end(),tmp3+mid)==dp.end()) continue;
int idx3=lower_bound(dp.begin(),dp.end(),tmp3+mid)-dp.begin();
//cout << idx1 << " , " << idx2 << " , " << idx3 << "\n";
if(idx3-i<=n) return true;
}
return false;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) a[i+n]=a[i];
dp.push_back(0);
for(int i=1;i<=2*n;i++) dp.push_back(dp.back()+a[i]);
int l=0,r=INF;
while(l<r){
int mid=(l+r+1)/2;
if(check(mid)) l=mid;
else r=mid-1;
}
cout << l;
//if(check(500)) cout << "OKAY\n";
}