#include <bits/stdc++.h>
using namespace std;
long long arr[200100], arr2[200100], ans = 0;
void f(long long l, long long r)
{
    if(r - l < 0)
        return;
    long long mx = -1, ind = 0;
    for(long long i = l; i <= r; i++)
    {
        if(arr[i] > mx)
            ind = i;
        mx = max(mx, arr[i]);
    }
    ans += arr2[ind] - arr2[ind - 1];
    long long s1 = arr2[ind - 1] - arr2[l - 1];
    long long s2 = arr2[r] - arr2[ind];
    if(s1 > s2)
        l = ind + 1;
    else if(s2 > s1)
        r = ind - 1;
    else
    {
        if(ind > (l + r) / 2)
            l = ind + 1;
        else
            r = ind - 1;
    }    
    f(l, r);
}
void solve()
{
    long long n;
    cin >>n;
    for(long long i = 1; i <= n; i++)
        cin >>arr[i];
    for(long long i = 1; i <= n; i++)
        cin >>arr2[i], arr2[i] += arr2[i - 1];
    f(1, n);
    cout <<ans;

}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
    for(t = 1; t--; solve());
}