// binary search solution: // for a fixed start level m, go through each level from left to right // - if we can currently beat that level gain the x levels. // after this try to beat as many levels as possible from the todo // stack (beat the first value and gain the second as level) // - add a pair {x, x+current level} on a todo stack. // if we can clear the todo stack we can beat all levels. #include using namespace std; #define all(x) begin(x), end(x) #define sz(x) (ll)(x).size() using ll = long long; using ld = long double; constexpr ll inf = 10000000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector in(2*n); for (ll i = 0; i < n; i++) { cin >> in[i]; in[i+n] = in[i]; } auto ok = [&](ll m){ vector> todo; ll cur = m; for (ll x : in) { if (x <= cur) { cur += x; while (!todo.empty() && todo.back().first <= cur) { cur += todo.back().second; todo.pop_back(); } } else { todo.push_back({x, cur - m + x}); cur = m; } } return todo.empty(); }; ll l = -1; ll r = inf; while (l + 1 < r) { ll m = (l+r)/2; if (ok(m)) { r = m; } else { l = m; } } cout << r << endl; }