#include <bits/stdc++.h>
 
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define int long long

const int maxn = 2e5+7;
const int inf = 1e18;
const int mod = 1e9+7;
  
using namespace std;

int n;
int h[maxn], c[maxn];
int t[4 * maxn];

void build(int tl = 1, int tr = n, int v = 1){
	if(tl == tr){
		t[v] = h[tl];
	}else{
		int md = (tl + tr) / 2;
		build(tl, md, v * 2);
		build(md + 1, tr, v * 2 + 1);
		t[v] = max(t[v * 2], t[v * 2 + 1]);
	}
}

int get(int l, int r, int tl = 1, int tr = n, int v = 1){
	if(l > tr || tl > r) return -1e18;
	else if(l <= tl && tr <= r) return t[v];
	else{
		int md = (tl + tr) / 2;
		return max(get(l, r, tl, md, v * 2), get(l, r, md + 1, tr, v * 2 + 1));
	}
}

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> h[i];
	}
	for(int i = 1; i <= n; i++){
		cin >> c[i];
	}
	build();
	int ans = 0;
	for(int i = 1; i <= n; i++){
		ans += c[i];
		int l = i, r = n, res = 0;
		while(l <= r){
			int md = (l + r) / 2;
			if(get(i, md) <= h[i]){
				res = md;
				l = md + 1;
			}else{
				r = md - 1;
			}
		}
		// cout << res << ' ';
		i = i + res - 1;
	}
	// cout << '\n';
	cout << ans;
}
 
signed main(){
    speed
    int test = 1;
    // cin >> test;
    while(test--){
        solve();
    }
}