#include <bits/stdc++.h>

using namespace std;

long long n, a[2000001], b[2000001];
long long pr[2000001];

int main(){
    cin >> n;
    long long mx = 0;
    int pos;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        if(a[i] == max(mx, a[i])){
            mx = a[i];
            pos = i;
        }
    }
    pr[0] = 0;
    for(int i = 1; i <= n; i ++){
        cin >> b[i];
        pr[i] = pr[i - 1] + b[i];
    }
    int l = 1, r = n;
    long long sum = 0;
    while(true){
        if(pos == l || pos == r){
            sum += b[pos];
            cout << sum;
            return 0;
        }
        if(pr[pos - 1] <= pr[r] - pr[pos]) r = pos - 1;
        if(pr[pos - 1] > pr[r] - pr[pos]) l = pos + 1;
        sum += b[pos];
        mx = 0;
        for(int i = l; i <= r; i ++){
            if(a[i] == max(mx, a[i])){
                mx = a[i];
                pos = i;
            }
        }
    }
}
