//https://codeforces.com/gym/104064/problem/H
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
#define int ll
int n, a[N];
int stk[N], val[N], head;
//将环转化成 2n 数组
//单调栈维护左边无法吃掉的值, 遍历右边
//如果右边的可以吃,则加上贡献并往左边找,左边能吃就吃,不能吃就接着往右边找
//如果右边不可以吃,则加入单调栈,并且在判下个点(右边)能不能吃时初值变成了 x (换起点)
bool check(int x)
{
int res = x;
head = 0;
for(int i = 1; i <= n; i++)
{
if(a[i] <= res)
{
res += a[i];
res = min(res, (ll)1e13 + 10);//防止爆 long long
while(head && a[stk[head]] <= res)
{
res += val[stk[head]];
res = min(res, (ll)1e13 + 10);
head--;
}
}
else
{
stk[++head] = i;
val[i] = res - x + a[i];
res = x;
}
}
return head == 0;
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], a[i + n] = a[i];
n *= 2;
int l = 0, r = 1e13 + 10, ans;
while(l <= r)
{
int mid = (l + r) / 2;
if(check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
cout << ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}