#include <bits/stdc++.h>
using namespace std;
struct C {
    int h;
    int c;
    int ind;
};
bool cbh(const C &a, const C &b) {
    return a.h > b.h;
}
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    vector<C> cp(n);
    for (int i = 0; i < n; i++) {
        cin >> cp[i].h;
        cp[i].ind = i;
    }
    for (int i = 0; i < n; i++) {
        cin >> cp[i].c;
    }
    sort(cp.begin(), cp.end(), cbh);
    long long ans = 0;
    vector<int> r(n, 0);
    int l = -1, right = -1;
    for (int i = 0; i < n; i++) {
        if (l == -1 || cp[i].h > cp[l].h) {
            r[cp[i].ind] = 1;
            ans += cp[i].c;
            l = i;
        } 
		else if (right == -1 || cp[i].h > cp[right].h) {
            r[cp[i].ind] = 1;
            ans += cp[i].c;
            right = i;
        } 
		else {
            r[cp[i].ind] = 0;
        }
    }
    cout << ans << "\n";
}
