#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> slots(n), need(n);
for (int &i : slots) {
cin >> i;
}
for (int &i : need) {
cin >> i;
}
int sum_slots = accumulate(slots.begin(), slots.end(), 0);
int sum_need = accumulate(need.begin(), need.end(), 0);
while (sum_slots > sum_need) {
int x = min(sum_slots - sum_need, slots.back());
sum_slots -= x;
int rem = slots.back() - x;
slots.pop_back();
if (rem != 0) {
slots.push_back(rem);
}
}
int m = need.size();
deque<pair<int, int>> need_pairs;
for (int i = 0; i < m; ++i) {
need_pairs.push_back({i, need[i]});
}
int ans = -inf;
for (int ii = 0; ii < m; ++ii) {
for (int i = 0; i < m; ++i) {
need_pairs[i].second = need[need_pairs[i].first];
}
int cur_ans = 0;
int cur_slots = slots[0];
for (int i = 0, j = 0; i < n;) {
int get = min(cur_slots, need_pairs[i].second);
if (j < need_pairs[i].first) {
cur_ans += get;
} else if (j > need_pairs[i].first) {
cur_ans -= get;
}
need_pairs[i].second -= get, cur_slots -= get;
if (need_pairs[i].second == 0) {
i++;
}
if (cur_slots == 0) {
cur_slots = slots[++j];
}
}
ans = max(ans, cur_ans);
auto e = need_pairs.front();
need_pairs.pop_front();
need_pairs.push_back(e);
}
cout << ans << '\n';
}