#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
    ll n;
    cin >> n;
    vector <ll> heights(n);
    vector <ll> price(n);

    for(ll& h : heights){
        cin >> h;
    }

    for(ll& p : price){
        cin >> p;
    }

    vector <bool> check(n, false);
    ll ans = 0;

    for(int i = 0; i < n; i++){
        if(!check[i]){
            ll maxh = heights[i];
            ll j = i;
            while(j < n && !check[j]){
                if(heights[j] > maxh){
                    maxh = heights[j];
                }
                j++;
            }

            ll temp = j - 1;
            if(heights[i] >= heights[temp]){
                ans += price[i];
                check[i] = true;
            } 
            else{
                ans += price[temp];
                check[temp] = true;
            }
        }
    }
    cout << ans << endl;

    return 0;
}