#include <bits/stdc++.h>
   
#define int long long
#define ld long double

using namespace std;

pair<int, int> a[200005];
int c[200005];

int ans(int x, int l, int r) {
	int y = x;
	while(y > 0 && (a[y].second >= a[x].second || a[y].second < l)) {
		y --;
	}
	int z = x;
	while(z > 0 && (a[z].second <= a[x].second || a[z].second > r)) {
		z --;
	}
	if(y == 0 || z == 0) {
		return c[x];
	} else {
		return min({ans(y, l, a[x].second - 1), ans(z, a[x].second + 1, r), c[x]}) + c[x];
	}
}

main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
	cin >> n;
    for(int i = 1;i <= n; ++i){
    	cin >> a[i].first;
    	a[i].second = i;
    }
    sort(a + 1, a + n + 1);
    int b[n + 5];
    for(int i = 1; i <= n; ++i) {
    	cin >> b[i];
    }
    for(int i = 1; i <= n; ++i) {
    	c[i] = b[a[i].second];
    }
    cout << ans(n, 1, n);
}
