#include <iostream>
#include <vector>
using namespace std;
int max_in_v(vector <pair<long long, int>> &h, int s){
    int max = -1, maxi = -1;
    for(int i = s; i < h.size() && h[i].second == 0; i++){
        if(max < h[i].first){
            max = h[i].first;
            maxi = i;
        }
    }
    return maxi;
}
bool yra(vector <pair<long long, int>> h){
    for(int i = 0; i < h.size(); i++){
        if(h[i].second == 0){
            return false;
        }
    }
    return true;
}
int main(){
    long long n, ans = 0;
    cin >> n;
    vector <pair<long long, int>> h(n,{0, 0});
    vector <long long> c(n);
    for(int i = 0; i < n; i++){
        cin >> h[i].first;
    }
    for(int i = 0; i < n; i++){
        cin >> c[i];
    }
    int max = -1, max2 = -1;
    while(yra(h) == false && max != 0){
        max = max_in_v(h, 0);
        max2 = max_in_v(h, max+1);
        ans += c[max];
        h[max].second = 1;
        for(int i = max+1; i <= max2; i++){
            h[i].second = 1;
        }
        if(n > 1 && h[n-2].second == 1 && (n-2 == max || h[n-2].first < h[n-1].first)){
            h[n-1].second = 1;
        }
    }
    if(h[n-1].second == 0){
        ans += c[n-1];
    }
    cout << ans;
}
