///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;

ll n,m,x,y, i, a[200001], b[200001];

pll d[2000001];
void build(ll l, ll r, ll k)
{
    if(l>r)return;
    ll mx=-1, I=-1;
    for(ll i=l;i<=r;i++)
    {
        if(mx<a[i])mx=a[i], I=i;
    }
    d[k]={mx, I};
    build(l, I-1, k*2);
    build(I+1, r, k*2+1);
}
ll rec(ll k)
{
    if(k>4*n)return 0;
//    cerr << k << " " << d[k].se << endl;
    ll ans=b[d[k].se];
    if(d[k*2].fi!=1e18 and d[k*2+1].fi!=1e18)ans+=min(rec(k*2), rec(k*2+1));
//    cerr << k << " " << ans << endl;
//    min(rec(k*2), rec(k*2+1));
    return ans;
}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    cin >> n;
    for(i=0;i<n;i++)cin >> a[i];
    for(i=0;i<n;i++)cin >> b[i];
    for(i=0;i<1000000;i++)d[i]={1e18, 1e18};
    build(0, n-1, 1);
//    for(i=1;i<15;i++)cout << d[i].se << " ";
//    cout << endl << endl;
    cout << rec(1);
    return 0;
}
/*
3
3 10 7
2 5 2
*/
