///Shapo4ka
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vii;
typedef vector<ll> vll;

//int a[1220][120];

int main() {

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
//    cin.tie(0);
//    cout

    int n;
    cin >> n;
    int a[n + 1], b[n + 1];
    int mx = -1, pos = 0;
    for(int i = 0; i < n; i++ ) {
        cin >> a[i];
    }
    for(int i = 0; i < n; i++) {
        cin >> b[i];
    }
    ll ans = 0;
    int h = -1;
    for(int i = 0; i < n; i++) {
        if(a[i] > h)h = a[i], ans += b[i];
    }
    ll ans1=0;
    h = -1;
    for(int i=n-1;i>=0;i--)
    {
        if(a[i]>h)h=a[i], ans1+=b[i];
    }
    cout << min(ans, ans1);
    return 0;
}
