#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 y = x;
	while(y > 0 && a[y].second >= a[x].second) {
		y --;
	}
	int z = x;
	while(z > 0 && a[z].second <= a[x].second) {
		z --;
	}
	if(y == 0 && z == 0) {
		return c[x];
	} else if(y == 0) {
		return min(c[x], ans(z)) + c[x];
	} else if(z == 0) {
		return min(c[x], ans(y)) + c[x];
	} else {
		return min({ans(y), ans(z), 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);
}
