#include <iostream>
#include <vector>
using namespace std;
int max_in_v(vector <pair<long long, int>> &h){
    int max = -1, maxi = -1;
    for(int i = 0; i < h.size(); i++){
        if(h[i].second == 0 && 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;
    while(yra(h) == false && max != 0){
        max = max_in_v(h);
        ans += c[max];
        h[max].second = 1;
        for(int i = max+1; i < n-1 && h[i].second != 1 && h[i] < h[i+1]; 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;
}

/*
int main(){
    int n,q,k;
    cin>>n>>g>>k;
    int qq[n+1];
    vector<pair>v;
    for(int i=0;i<n;i++){
        cin>>qq[i];
        v=make_pair(qq[i],i)
    }
    int newq[n+1];
    int l,r;
    //sort(v.begin(), v.end());

    while(q--){
        cin>>l>>r;
        sort(, v.end());

    }
}*/


/*#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<pair<int, int>>nu;
int main()
{
    int n;
    cin>>n;
    int a[n+5];
    for (int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1, a+1+n);
    int ch=a[1], ind = 1;
    for (int i=1;i<=n+1;i++)
    {
        if (a[i] != ch)
        {
            nu.push_back(make_pair(i - ind, ch));
            ch = a[i];
            ind = i;
        }
    }
    sort(nu.begin(), nu.end());
    int siz = nu.size();
    /*for (int i=0;i<siz;i++)
    {
        cout<<nu[i].first<<" ";
    }
    cout<<endl;
    for (int i=0;i<siz;i++)
    {
        cout<<nu[i].second<<" ";
    }
    cout<<endl;
    int ogh=0;
    for (int i=0;i<siz - 1;i+=2)
    {
        if (nu[i].first == nu[i+1].first)
        {
            ogh ++;
        }
    }
    if (ogh == nu.size() - 1)
    {
        cout<<-1;
        return 0;
    }
    vector <int> ans;
    for (int i=0;i<siz/2;i++)
    {
        ans.push_back(nu[i].second);
        ans.push_back(nu[siz-i-1].second);
    }
    for (int i=0;i<ans.size();i+=2)
    {
        if (ans[i] <= ans[i+1])
        {
            cout<<-1;
            return 0;
        }
    }
    for (int i=0;i<ans.size()-1;i++)
    {
        if (ans[i] == ans[i+1])
        {
            cout<<-1;
            return 0;
        }
    }
    reverse(nu.begin(), nu.end())
}*/
