#include <bits/stdc++.h>
#define ll long long
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ull unsigned long long
#define ld long double
#define files freopen("HelpMe.in", "r", stdin), freopen("HelpMe.out", "w", stdout)
using namespace std;
const ll mod = 1e9+7;
const ll N = 2e5 + 5;
const ll P = 337ll;
const ld EPS = 1e-9;
const ll block = 400;
ll n;
ll h[N], c[N], lg[N];
ll st[18][N];
void build() {
	lg[0] = -1;
	for(ll i = 1; i <= n; i++) {
		lg[i] = lg[i >> 1] + 1;
		st[0][i] = h[i];
	}
	for(ll k = 1; k <= 17; k++) {
		for(ll i = 1; i + (1 << (k - 1)) <= n; i++) {
			st[k][i] = max(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
		}
	}
}
ll get(ll l, ll r) {
	ll k = lg[r - l + 1];
	return max(st[k][l], st[k][r - (1 << k) + 1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(ll i = 1; i <= n; i++) cin >> h[i];
build();
for(ll i = 1; i <= n; i++) cin >> c[i];
ll ans = 0;
for(ll i = 1, j = i - 1; i <= n; i++) {
    while(j + 1 <= n && get(i, j + 1) == h[i]) j++;
    ans += c[i];
    if(j == n) break;
}
ll tmp = 0;
for(ll j = n, i = j + 1; j >= 1; j--) {
    while(i - 1 >= 1 && get(i - 1, j) == h[j]) i--;
    tmp += c[j];
    if(i == 1) break;
}
cout << min(ans, tmp);
return 0;
}
// equal, min, max, 1, random
/*

*/
