#include <bits/stdc++.h>

using namespace std;

int a[200005], b[200005], st, n;
void req(int l, int r) {
    if (l > r) {
        cout << st;
        exit(0);
    }
    int ind = l;
    int count = 0;
    for (int i = l; i <= r; i++) {
        if (a[ind] == a[i]) {
            count++;
        }
        if (a[ind] < a[i]) {
            ind = i;
            count = 1;
        }
    }
    int j = ind - 1;
    int le = 0;
    while (a[j] < a[ind] && j >= l) {
        le += b[j];
        j--;
    }
    int ri = 0;
    j = ind + 1;
    while (a[j] < a[ind] && j <= r) {
        ri += b[j];
        j++;
    }
    st += b[ind];
    if (max(le, ri) == ri) {
        if (ind == l) {
            j = ind + 1;
            while (a[j] < a[ind] && j <= r) {
                ri += b[j];
                j++;
            }
            req(j, r);
        }
        else {
            req(l, ind - 1);
        }
    }
    else {
        if (ind == r) {
            int j = ind - 1;
            while (a[j] < a[ind] && j >= l) {
                le += b[j];
                j--;
            }
            req(l, j);
        }
        else {
            req(ind + 1, r);
        }
    }
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    req(0, n - 1);
    cout << st;
}