#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n;
    cin >> n;
    long long a[n],b[n], p = 0;
    for (long long i =0 ; i < n ; i ++){
        cin >> a[i];
    }
    for (long long i =0 ; i < n ; i ++){
        cin >> b[i];
    }
    long long l=0,r = n-1;
    while (l<=r) {
        long long mx = -1,v = 0;
        for (long long i = l; i <= r; i ++) {
            if (mx< a[i]){
                mx = a[i];
                v = i;
            }
        }
        if (v==l)r = v-1;
        else l = v+1;
        p += b[v];
    }
    cout << p;
}
